Consistency refers to how fresh a particular piece of data is. Technically speaking, every read requests we receive to our distrubuted severs, must return same piece of data.
Consistency levels refers to how fresh we want our data to be, when a user sends a read request.
There will be a tradeoff between consistency and efficiency in Consistency Levels. So, it is necessary to choose the consistency level depending upon the requirement.
- Linearizable/Atomic Consistency
For every read request we receive, we want to show fresh data.
To achieve this, every request (read or write) must be in order. And to maintain the order, we use a single threaded single sever.
This will have strong consistency. But the efficiency is lowest.
Usecase: Banking Transactions, Product Availability Update, etc.
- Eventual Consistency
For a read request, we will send stale data, but eventually when the data gets updated, we will send fresh data.
The system is not consistency in the beginning, but eventually, it will have consistency.
To achieve this, we can process requests parallely (using multiple severs) or concurrently (using multiple threads).
The consistency is lowest, but efficiency is highest.
Usecase: Like, Comments, DNS, Sharing Post on social apps, etc.
- Causal Consistency
Based upon the relation of operations, we execute the related operations in order.
We can find the relation of operations by checking the type of data on which the operations are doing read/write operation.
If we have two operations:
- write x = ‘John’
- read x
Notice, that both requests are going to perform operation on same data item. So, we will execute these two requests in order in a server/thread.
But, if we have:
- write x = ‘John’
- write y = ‘Cena’
Notice, that both requests are performing operation on different piece of data item, so we will execute these two requests on seperate server/thread.
Causal Consistency is better than Linearizable interms of efficiency and Eventual Consistency in terms of consistency.
But the drawback is that the Causal Consistency will fail for aggregation, and will showstale data for aggregation operations.
The reason why it fails is because it will not be able to find relationship between aggregation operations, and it might treat the aggregation operation as related.
Usecase: Google Docs (multiple edits), chat systems, multi-player games, etc.