distributed systems25 min

Eventual Consistency

Understanding consistency models and how distributed systems converge

0/9Not Started

Why This Matters

When you update your profile picture on a social media platform, some of your friends might still see the old one for a few seconds. That is not a bug -- it is eventual consistency at work. The system chose to be available and fast over being perfectly synchronized across all servers at every instant.

Most large-scale systems use eventual consistency because strong consistency is expensive and slow at global scale. Understanding consistency models lets you design systems that balance correctness and performance for your specific use case.

Define Terms

Visual Model

Node Avalue = 42 (fresh)
Node Bvalue = 37 (stale)
Node Cvalue = 37 (stale)
Clientwrites value = 42
ConvergedAll nodes: 42
Write
Async repl.
Async repl.
Updated
Updated

The full process at a glance. Click Start tour to walk through each step.

Eventual consistency: writes propagate asynchronously, creating a temporary window where nodes disagree.

Code Example

Code
// Simulating eventual consistency with replicas

class EventuallyConsistentStore {
  constructor(replicaCount) {
    // Each replica has its own local copy
    this.replicas = Array.from({ length: replicaCount }, () => new Map());
  }

  // Write goes to one replica first
  write(key, value) {
    this.replicas[0].set(key, value);
    console.log(`Written to primary: ${key} = ${value}`);
    // Async replication happens later
  }

  // Read from a specific replica (may be stale)
  read(key, replicaIndex) {
    const value = this.replicas[replicaIndex].get(key);
    console.log(`Read from replica ${replicaIndex}: ${key} = ${value}`);
    return value;
  }

  // Simulate background replication
  replicate() {
    const primary = this.replicas[0];
    for (let i = 1; i < this.replicas.length; i++) {
      for (const [key, value] of primary) {
        this.replicas[i].set(key, value);
      }
    }
    console.log("Replication complete - all replicas converged");
  }
}

const store = new EventuallyConsistentStore(3);
store.write("user:1", "Alice");
store.read("user:1", 0);  // "Alice" (primary)
store.read("user:1", 1);  // undefined (stale!)
store.replicate();         // background sync
store.read("user:1", 1);  // "Alice" (converged)

Interactive Experiment

Try these thought experiments and code modifications:

  • Add a readYourWrites method that always reads from the replica the client last wrote to. Does this fix the staleness problem?
  • Add timestamps to each write and implement a readLatest method that queries all replicas and returns the newest value.
  • Simulate a network delay by making replication take a random amount of time. How long is your inconsistency window?
  • Think about DNS: when you update a domain record, how long does it take to propagate worldwide?

Quick Quiz

Coding Challenge

Last-Write-Wins Resolver

Write a function called `resolveConflict` that takes an array of replica values, each with a `value` and `timestamp`, and returns the value with the highest timestamp. This implements the last-write-wins conflict resolution strategy used in many eventually consistent systems.

Loading editor...

Real-World Usage

Eventual consistency powers many of the systems you use daily:

  • Social media feeds: Your post may not appear to all followers instantly. Some see it seconds later.
  • Shopping carts: Amazon uses eventual consistency for cart data -- occasionally you might see a slightly outdated cart.
  • DNS: Domain name updates propagate globally over minutes to hours.
  • CDNs: Cached content at edge servers becomes stale until the cache TTL expires and refreshes.
  • CRDTs: Conflict-free Replicated Data Types are data structures designed to merge concurrent updates automatically, powering real-time collaboration tools.

Connections