CAP Theorem
CAP theorem in System Design
CAP
Consistency
Availability
Partition tolerance
Consistency
Consistency means the data should be consistent to all users for any application
Lets say A and B are the person who are experiencing a application,
but for any circumstances the application is visible only to B user then we can say that it is not consistent
Ex : A is depositing Rs 100 to the bank account and his balance is Rs 0 earlier, so after depositing rs 100, the balance should get updated.
It should not be 0 as it will lead to inconsistent system
Availability
It refers that the system should be available all the time
We have often seen that when the SPPU results are out, the website is always down.
For a good system this should not happen
It has to be available all the time...
A good example can be Google.com (which is available all the time)
Partition Tolerance
In a distributed system the application is divided into components and it is decentralized which means it is deployed on multiple servers not on the single server.
Lets assume one server gets down, we call it is as partition.
We call it is network partition AND
Tolerance means to save this server from getting down and if it gets down we will use the replica of that server
CAP Theorem
For a distributed system, the CAP theorem states that it is possible to attain only two properties from CAP and the third would always be compromised.
The requirement should define the two properties to choose from CAP properties.
As we can see, only two properties can be achieved at a time not all the three.
CP (Consistence and Partition tolerance)
Here we are compromising the availability of system
The system will be consistent and for this we will make the system unavailable for some time
and it is partition tolerance as if one of the node of server gets down, the replica of that node will take its place for functioning
Ex : If a bank application want to schedule a maintenance break, they will make the system unavailable for some time until the data is consistent
AP (Available and Partition tolerance)
Here the consistency of the system is compromised
The system should be available all the time
If we are writing a blog on a website it is not an emergency that the blog which is written by x-user needs to updated to all the readers at the same time. There can be a delay of 1 sec to the system to get consistent
Instead the availability of system is recommended.
System should be available.
CA (Consistent and available)
Here we say that the partition tolerance will be compromised
and if the Partition tolerance is skipped we no more have the distributed system
We will have the system as centralized system means there will be only one node available and that is the only way we can achieve consistence and availability.
And if the server is down, as we do not have partition tolerance the system will be down.
We have seen that for any system we can choose any 2 properties from CAP
But it is recommended that partition tolerance will always be there, so the choice comes to C and A i.e. Consistency or Availability
So the application will either be CP or AP
Let' s look at some examples and decide what to choose for a good system.
Ex :
Blog Website -> It needs to be available -> AP
Gaming platform -> AP
Stock trading -> The prices should be consistent across all the servers -> CP
Netflix -> AP
WhatsApp -> AP
Bank -> CP (we can make the system unavailable for some time as we cannot accept the inconsistent data)
We will choose only two options either CP or AP
This is all about CAP theorem in system design
