Wednesday, 5 October 2016


CAP and ACID are two distinct terms that are used often in database world, where ACID is used to define properties of a database and CAP is actually used to determine distributed systems nature.
ACID stands for
  • Atomicity
  • Consistency
  • Isolation
  • Durability
Before going into the meaning of these terms one should be fairly aware of transactions. A transaction is like a unit of work which may comprise of multiple write operations or just a single operation. Treating a set of operations like this actually enables databases to maintain ACID properties.
Atomicity – Either all statements/queries inside a transaction happen or none of it does. In layman terms, if you start a transaction saying I want these 5 queries to be run. The database will make sure that either all of 5 queries are successful or none of them is. So if 5th query fails automatically the previous 4 queries effect will be wiped off.
Consistency – This ensures that whatever data you put into your database does not violate the rules that you have set for that type of data structure. This also includes the rules for related data and so forth.
Isolation – Every transactions run independent of other transactions. No transaction can refer to the data which is being manipulated by other transaction until it finishes.
Durability – Once a transaction has been committed it is stored in a durable medium (hard disk). This ensures even after crashes or restarts the database will have its state preserved.
 CAP stands for
  • Consistency
  • Availability
  • Partition Tolerance
CAP is basically applied to a distributed system architecture where database is not just a single machine but is a set of nodes over a network, say 5 different machines running same database connected through network.
Consistency – Although some confuse CAP’s consistency with ACID’s one, it is entirely of different meaning when it comes to distributed system. It means that all the nodes will always have latest data, so it doesn’t matter from which node you read, you will always get the latest copy.
Availability – This means that a system will always be available i.e. respond to requests even if a node goes down. Although this does not guarantee that other nodes will be in sync with unavailable node.
Partition Tolerance – This means system should be able to tolerate a failure in internal network i.e. communication between nodes. This means that nodes are individually able to serve the clients but are unable to communicate with each other.
This theorem is actually used to describe a system as either CP or AP, there cannot be a practical CA distributed system. In a cluster the nodes transmit the data to each other to maintain its consistency. When does this happen is the question.
In an AP system, the system still functions even if a single node is down. This does not ensure consistency since the failed node might have not updated other nodes with its operations. In this case the running nodes will give outdated data to the clients.
In an CP system, the consistency is of priority. So if a node goes down though the system can run, it chooses to stop responding to requests so that other nodes do not get ahead of failed node in terms of data. When the failed node is restored the system will be back up.

Saturday, 15 February 2014

REST with Apache CXF


Apache CXF is a front-end framework for JAX-WS and JAX-RS services. 
Here in tutorial I am going to show how to develop restful service with Apache CXF.

Building your first REST Application

Though I have used IntelliJ IDEA to write this application, but you can use any other IDE like Eclipse or Netbeans to write it. This application will use Apache Maven for its build and dependency management.

1. Create your project

We will create a project named TodoApplication. Since this will be a maven project, so the project structure is going to be pretty standard as shown below:

 Don't mind the .idea folder since it is IDE specific. Rest should look as is.

2. Defining dependencies in pom.xml

For Apache CXF, there is a bundle jar which contains everything for JAX-RS implementation. Add the following dependency in pom.xml


For logging we will use slf4j over log4j.


And lastly dependencies for Spring


The versions for slf4j, log4j and spring are : 


3. Defining web.xml

In folder src/main/webapp create a new folder with name WEB-INF. Under this folder create a file web.xml which defines your servlets and their mappings. CXF under the hood uses servlet only to map the urls to restful resources. Following is how our web.xml will look

 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">  

CXFServlet is the servlet that dispatches the http call to restful resources. cxf-context.xml has the rest endpoints defined in it. We will get there in a while.

4. Writing a Rest Service

A Rest Service is basically the service class that will be exposed as endpoints. These have JAX-RS annotations that define the service. For starters let's write a Ping service which will simply tell that whether application is running fine or not.

 import org.slf4j.Logger;  
 import org.slf4j.LoggerFactory;  
 public class PingService {  
   Logger logger = LoggerFactory.getLogger(PingService.class);  
   public Response ping(){"Received request for ping service");  
     return Response.ok("Application is running fine").build();  

@Path annotation defines the path at which this service will be exposed.
@GET bind ping method to HTTP GET call. There are similar annotation for PUT, POST, DELETE and OPTIONS.
@Produces defines the Content-Type of the response.

5. Defining cxf-context.xml

This context file contains the configuration for cxf bus i.e. the transport and the endpoints.

 <beans xmlns=""  
   <bean id="pingService" class=""/>  
   <jaxrs:server id="restContainer" address="/">  
       <ref bean="pingService"/>  

<jaxrs:server> contains the service endpoints and the provider entries. Service endpoints goes in the <jaxrs:serviceBeans>

<cxf:bus> contains the configuration for cxf bus. Like logging is one of the features which will log every incoming request and outgoing response.

6. Deploying the application

This application can be deployed on one JEE container like Tomcat, Jetty.

After deployment hit the following url in your browser :


You will see "Application is running fine" message in the browser.

Tuesday, 17 September 2013

Volatile and Synchronized in Java

Volatile is just another modifier like private , protected etc. It modifies the visibility of the variables and is generally more relevant in thread context.

Volatile when used on a variable ensures that every thread that is using it has the latest copy. It is important in multi threaded programming that shared variables which are frequently read and write by the threads are marked as volatile.
JVM is free to do anything with the variables and every thread can, actually does keep its own copy of variable meaning if a thread writes a new value to a variable, the other thread wont get that latest value. Hence the volatile.

private int x  = 5;  
Thread 1 uses it and makes it 8, but thread 2 will have its own copy as 5 and wont see new value 8.
This can of course leads to a lot of problems and i don't think there is any need to explain that.
Primary three reasons to use volatile when needed:

1. Every thread keeps it own copy of a variable, so if you want a shared variable with publish sort of functionality i.e. that every where its latest value is used, make it volatile.
2. CPU caches : if the variable is cached it isn't necessary that that value will be updated which could lead some odd values with some threads. Hence volatile :).
3. Volatile ensures that all the operations happening on that variable are atomic. For eg changing a Long value is actually two write operations than one. If that variable is not volatile it might happen that when one thread was in middle of process of modifying that variable , some other thread accessed it and got some arbitrary value since write operation was not complete. The volatile keyword ensures that before any other thread reads that value, the write operations are completed.

Synchronized is used to achieve locks on variables which are supposed to be accessed by the different threads. Where volatile ensures every thread has the latest value, it fails to maintain the write operation or perhaps the sequence of write operations done on  a variable. This is where synchronized comes into play.
Synchronized should be implemented properly. Instead of making whole class synchronized , the actual data structures should be identified which needs to be synchronized and should be synchronized. However in some cases when that data structure is in use everywhere avoiding synchronizing every class methods is the only option.

Will write more on synchronized after i read more about it ...!! 

Sunday, 15 July 2012

Me the Developer

I know the title does not suits the blog content at all, but trust me actually this is the last line which I wrote in this post... coming up with a good title is usually hard for me. Since this is my first post, I not going to post any java code or any other tech related stuff. Though my future posts will certainly be composed of code stuff. This post is majorly for just the taste of blogging. Any prior suggestions and comments are welcomed.

A little about me ....
I know this belongs more to "About Me" page, but anyways I am going to do it over here only. I am a Java developer, started out in September last year. I m not a "fundoo" geek as if now, but I know one day I will end up in that category since my craving for Java and other related technologies is ever growing.
Recently I have joined a project which comprises of Restful Web Services development. You may be saying whats new in this. This is such a hot topic that everyone is dirtying their hands in this, yes. 

Apart from coding I like to play soccer, read some leisure novels and also I am big fan of video games. But now I like to make games, how small or unattractive they might be I like making, than playing itself.

That's it for now.