Understanding CAP Theorem In Distributed Systems

CAP theorem deals with 3 aspects of the system:-
1. Consistency
2. Availability
3. Partition Tolerance

  1. Consistency:- Consistency says that in a distributed system data on all nodes should be consistent with each other. So, even if a client connects to any node in the distributed system, it should see the same data.

  2. Availability:- Availability says that the data should be available at all times.

  3. Partition Tolerance:- Partition tolerance says that in case of a network outage also a distributed system should operate without any issues.

In case of a network outage, the nodes in the system won’t be able to communicate with each other.

Consider the example in the above image. We have 3 nodes in the system A, B & C. Now consider a scenario where there is a network outage between A-C & B-C.

Now if the system writes to A, it can propagate those changes to B but there is no way to propagate those changes to C. So if there is a client reading from C, it will get inconsistent data. So in this case we need to make a tradeoff between Consistency & Availability, this is what the CAP theorem says, either choose Consistency or Availability.

If we choose Consistency, we need to say that no client can read data from node C & sacrifice availability for the clients which can only read data from C.

If we choose Availability, we need to say that you can read data from any node, but there is no guarantee that data will always be consistent. If you are reading data from C, you will get old data until the network outage is fixed.

Consider the example of a banking application, in this app, we need to choose Consistency over Availability. Because losing Consistency will cause trust issues for the bank.

Now consider the example of a social media platform, here we can lose Consistency (in features like count of likes ) for some time to prioritize Availability.

In modern systems, the world is not black & white. You can actually choose some degree of Consistency & some degree of Availability, instead of completely trading one for the other.

So hope you would have got some idea about the CAP theorem. If you have any suggestions feel free to add them in the comments below. Please clap if you liked the content. Do follow me on my YouTube channel where I am publishing a lot of great content.

Did you find this article valuable?

Support Abhijeet Gurle by becoming a sponsor. Any amount is appreciated!