Let's discuss different scenarios possible in a real world.
👇🏻
For the discussion we will take this distributed system as an example which has two components C1 & C2.
They are connected to each other and are always in sync with respect to the data they store.
👇🏻
1. Availability - Partition Tolerance (AP):
Let's assume the connection b/w the two components is broken. So we say that there's a partition in the system.
Now based on the theorem only either consistency or availability can be achieved. Not both.
Let's understand why.
👇🏻
Let's say we have a data variable a = 10 in both C1 and C2.
Since there's a partition in the system, the question is whether we want the system to be available until the partition is fixed or not.
Now if the user interacts with the component C1 and updates a to 20.
👇🏻
(Since availability is our goal, we will still allow the users to use the system by making consistency take a hit)
Since the connection between C1 & C2 is broken, `a` will still contain the value 10 in C2.
👇🏻
This means system was available(A) during this transaction but consistency(C) is not achieved.
👇🏻
2. Consistency - Partition Tolerance (CP):
In the same example if we want to keep the system consistent, we will not allow users to use the system until the partition is fixed.
This means that the system is not available all the time.
👇🏻
Once the partition is fixed and the system is up again, users can interact with the system and data will be consistent since the connection between the two systems would be working again ( partition is fixed ).
👇🏻
3. Consistency - Availability (CA):
Now in case of CA, we want the system to be both available and consistent.
So in this case if there's any partition in the system we will not allow users to interact with a part of the system, which means there's no partition tolerance.
👇🏻
Let's assume C2 has some issues. Now we will take C2 out of the working system until it is fixed, which means we will not allow users to interact with C2 and thus no partition tolerance in the system.
In our example only C1 would be functional.
👇🏻
C1 now is a single component in the distributed system which is available and consistent.
In real world we will have so many more components. So if 5% of the components go down, the rest 95% of the components will still be available and consistent.
I hope the CAP theorem is now clearer to you with the real world examples.
I'll be writing an articles with more detailed examples and explanations on my blog. Subscribe to stay updated 🙂