Eventual consistency is a consistency model used in distributed systems that guarantees that, given no new updates to a piece of data, all replicas will eventually return the same value. This means that changes made to data will propagate through the system over time, but there might be a temporary period where different parts of the system have different values.
Example scenarios:
Example 1
Imagine you have a group chat app where messages are stored on multiple servers around the world. When you send a message, it gets stored on the nearest server first and then gradually propagates to other servers. Initially, not all servers will have the new message, so if your friend in another country checks the chat immediately, they might not see your message right away. However, after a short period, all servers will have synchronized, and everyone will see the same message.
In this scenario, the system is eventually consistent because, after some time, all users will see the same chat history, even though there might be temporary discrepancies.
Example 2
Imagine you have an online store with a distributed database system. When a customer places an order, the order information is first recorded on the nearest database server. This server then updates other servers across different regions. Initially, not all servers will have the latest order information, so if another customer checks the stock availability immediately after the first order, they might still see the item as available. However, after a short period, all servers will synchronize, and the stock information will be consistent across the platform.
In this case, the system is eventually consistent because, after some time, all servers will reflect the same stock levels, even though there might be temporary discrepancies.
Example 3
Let’s consider a social media application where you can “like” posts. Suppose a post currently has 100 likes. When you click the like button:
- Immediate Response: The UI immediately shows the like (optimistic UI update), so you see 101 likes right away to provide a good user experience.
- Write Operation: The like is written to the nearest database replica, which now records 101 likes for this post.
- Asynchronous Propagation: This change (from 100 to 101 likes) is then propagated to other database replicas across different regions.
- Temporary Inconsistency: During propagation, different users might see different like counts—some may still see 100 likes while others see 101 likes.
- Eventually Consistent: In this case, the system is eventually consistent because, after a few seconds (or milliseconds), all replicas sync up, and every user sees 101 likes.