Theory
Eventual Consistency
📋 Overview
Eventual Consistency is a consistency model used in distributed computing that guarantees that, if no new updates are made to a given data item, eventually all accesses to that item will return the last updated value. It is the core of AP (Available/Partition-tolerant) systems in the CAP theorem, prioritizing high availability and low latency over immediate global correctness.
🏗️ Core Principles & Characteristics
- Asynchronous Replication: Updates are made to a primary node (or any node) and then propagated to other replicas in the background.
- Convergence: The system ensures that all replicas will eventually arrive at the same state.
- Conflict Resolution: When multiple nodes receive different updates, the system must decide which one wins (e.g., Last-Write-Wins, Vector Clocks, or CRDTs).
- Consistency Window: The period of time between the update and the moment all nodes are in sync.
- Gossip Protocols: Used by nodes to "chat" and exchange data to reach a common state.
⚖️ Trade-offs: Pros & Cons
- Pros:
- High Availability: The system stays up even if some nodes are unreachable or slow.
- Low Latency: Writes don't have to wait for global consensus.
- Horizontal Scalability: Extremely easy to scale by adding more replicas.
- Cons:
- Stale Reads: Users might see "old" data for a short period.
- Complexity: Handling conflicts and out-of-order updates is difficult for developers.
- Not for Finance: Unsuitable for scenarios where immediate correctness is vital (e.g., bank transfers).
🌍 Real-World Implementation
- DNS (Domain Name System): Changes to IP addresses take hours to "propagate" globally.
- Social Media: Likes and follower counts don't need to be identical across the world in the same millisecond.
- Amazon DynamoDB: Offers eventual consistency by default for massive scale.
- CDNs: Content takes time to refresh across all global edge locations.
💡 Interview "Gotchas" & Tips
- CAP Theorem: Eventual consistency is the choice of A over C.
- Quorum (R+W > N): Explain how you can achieve "Strong" consistency on top of an eventually consistent system by reading/writing from a majority of nodes.
- Conflict Resolution: Know the difference between LWW (Last Write Wins) and CRDTs (Conflict-free Replicated Data Types).
- Read-Your-Writes: A specific guarantee where a user always sees their own updates, even if others don't yet.
📐 Suggested Architecture Primitives
- Vector Clocks: For tracking causality and detecting conflicts.
- Merkle Trees: For efficient data synchronization between replicas.
- Gossip Protocols: (e.g., SWIM) for node-to-node communication.
Canvas