Consistency Patterns in Distributed Systems¶
Definition¶
When working with Distributed Systems, we need to ensure components are up to date with recent updates and reflect them to the user.
Example
Suppose we have an application on purchasing books and two users are looking at the same book. If user 1 decides to purchase and before the inventory updating service completes, user 2 decides to buy the book as well, then there will be incorrect stock information. The ordered book may or may not be in stock.
Thus, we need some way of ensuring consistent data across the application.
Consistency Patterns¶
Refers to ways in which data is stored and managed in a distributed system, and how that data is made available to users and applications. There are three main types of consistency patterns:
- Strong consistency
- Weak consistency
- Eventual consistency
Strong Consistency¶
After an update is made to the data, it will be visible to all subsequent read operations. The data is replicated in a synchronous manner, ensuring all copies of that data are updated at the same time.
Example
Financial systems use strong consistency because the transfer of money requires utmost data integrity.
The downside of following strong consistency is that the system is not highly available and has higher latency.
Weak Consistency¶
In a weakly consistent system, updates to the data may not be immediately propagated. This can lead to inconsistencies between different versions of data. However, this allows for a highly available system and lower latency.
Example
Consider an online multiplayer gaming platform. A users actions are immediately visible to other players, but if there is lag or a connection loss, the action may not be seen by some of the users but the game will continue.
Eventual Consistency¶
A form of weak consistency, where after an update is made to the data, it will eventually be visible to any subsequent read operations. This data is replicated in an asynchronous manner (as opposed to Strong Consistency).
Example
Consider a social media platform that houses posts, likes, comments, messages, etc... The platform is designed be highly available and with low latency, so the data is stored in multiple data centers around the world.
When a user posts, it may be immediately available to users in the same region but it may take some time to propagate to data centers in different regions.
References¶
Distributed Systems Availability vs Consistency Latency vs Throughput