Course contents

08. Distributed Systems Lesson 5 of 10

Eventual Consistency

Watch async replicas serve stale reads, converge when writes stop, and drift without bound; then add read-your-writes, monotonic reads and safe merges.

16 minAdvanced8 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • Eventual consistency: if updates stop, all replicas eventually return the same last written value.
  • Until then, reads may return older values; the window is the replication lag, which has no formal bound.
  • Session guarantees (read-your-writes, monotonic reads) remove the anomalies users notice, at little cost.
  • With several writers, replicas converge only through a deterministic merge rule; last write wins silently loses data.
  • Eventual consistency buys low latency and availability during partitions; keep invariants on a strongly consistent store.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

Eventual consistency is the weakest useful guarantee for replicated data: if no new updates are made to an item, eventually all accesses to it return the last updated value (Werner Vogels’ formulation). Replicas may disagree for a while; they must converge once the writes stop.

That is a liveness guarantee only. It says nothing about how long convergence takes or what reads return before then. A read may return any recent value, and two successive reads may even go backwards.

Why it exists

Coordination costs latency and, during partitions, availability (CAP, PACELC). If replicas answer from local state and propagate changes in the background, every read and write is as fast as one node and keeps working when the network splits. For most data (feeds, catalogues, counters, profiles, caches, DNS) a short delay before everyone sees a change is acceptable.

How it works

Asynchronous propagation. A write is confirmed by one node (a primary, or any node in multi-leader and leaderless systems) and shipped to the others afterwards, through a replication log, gossip, or hinted handoff.

The inconsistency window. Between the confirmation and the moment a replica applies the change, a read on that replica returns the previous value. The window is the replication lag: milliseconds on an idle local network, seconds under load, much longer while a replica is down or partitioned. Hot keys, written often, spend most of their time inside a window.

Convergence. Replicas converge when every update has reached every replica and all replicas apply updates with the same outcome. Single-leader replication gets that from the log order. With several writers, mechanisms are needed:

  • Anti-entropy: replicas compare data (e.g. Merkle trees) and exchange what differs; read repair fixes divergent replicas noticed during reads.
  • Conflict detection: version vectors (or dotted version vectors) tell “B replaced A” apart from “A and B were concurrent”.
  • Conflict resolution: last write wins keeps the highest timestamp and discards the other write; CRDTs (counters, sets, maps, registers) merge any concurrent updates deterministically; siblings return all concurrent versions for the application to merge.

Session guarantees (Terry et al., 1994) strengthen what one client sees without global coordination:

  • Read-your-writes: a client always sees its own writes. Route its reads to the primary for a while after a write, or send the write’s log position and read only from replicas that have reached it.
  • Monotonic reads: a client never sees an older version than one it has seen. Stick the session to one replica, or send the newest version seen.
  • Monotonic writes and writes-follow-reads keep a client’s own writes, and writes that depend on what it read, in order.

Together these, and causal consistency more generally, can stay available during partitions while avoiding the anomalies users notice.

When to use it

Read-heavy, latency-sensitive data where a brief delay is harmless: social feeds, like and view counts, product catalogues, search indexes, recommendation results, user profiles (with read-your-writes), shopping carts (with a merge rule), presence, metrics. Multi-region and offline-first systems often have no practical alternative for most of their data.

When not to

Anything protected by an invariant across clients: account balances with limits, inventory that must not oversell, unique usernames, locks and leader election, idempotency records for payments. A stale read there causes wrong decisions, not just old screens. Use a strongly consistent store (or a strongly consistent path) for those few operations.

Trade-offs

Choice You gain You pay
Async replication, read anywhere Low latency, scale-out reads, availability during partitions Stale reads, possible loss of acknowledged writes on failover
Add read-your-writes Users see their own changes Session routing or position tracking
Add monotonic reads Time never goes backwards for a user Sticky sessions or version tokens
Last write wins Simple convergence Silently drops concurrent writes; depends on clocks
CRDTs Convergence without lost updates Limited data types, metadata overhead, harder deletes

Alternatives

  • Strong consistency (linearizability) where correctness needs it.
  • Bounded staleness: reads guaranteed at most t seconds or k versions behind (offered by several cloud databases and distributed SQL follower reads).
  • Causal consistency: preserves cause-before-effect order (a reply never appears before its question) and remains available.
  • Single-region writes with async read replicas: avoids conflicts entirely; staleness remains.

Failure modes

  • “Saved but gone”: a user’s read after a write hits a lagging replica.
  • Flickering data: successive reads alternate between replicas at different positions.
  • Unbounded lag: a slow or recovering replica serves very old data while looking healthy.
  • Silent lost updates: last write wins with concurrent writes or skewed clocks.
  • Resurrected deletes: a replica that missed a delete (tombstone expired before it caught up) reintroduces the item.
  • Lost writes on failover: promoting a lagging replica drops confirmed writes it never received.

In production

Monitor replication lag per replica and alert against an explicit budget; take replicas out of read rotation when they exceed it. Implement read-your-writes for user-facing writes (primary-after-write window or position tokens). Use sticky routing or version tokens where flicker would confuse users. In multi-writer systems choose CRDTs or sibling resolution for data where updates must not be lost, and keep tombstones longer than the longest expected replica outage. Document which data is eventually consistent so product teams can design around it.

Common misconceptions

  • “Eventually means within a second.” There is no bound unless you build and monitor one.
  • “Eventual consistency means data may be wrong.” Reads return values that were written; they may be out of date, not invented (unless merges lose data).
  • “If it converges, no writes were lost.” Last write wins converges by discarding writes.
  • “Strong or eventual, nothing in between.” Session guarantees, causal consistency and bounded staleness sit between.

Interview angle

Say what the guarantee is (convergence when writes stop, no time bound), then show you know its practical faces: replication lag as the inconsistency window, read-your-writes and monotonic reads as cheap fixes, and conflict resolution when more than one node accepts writes (last write wins versus CRDTs or version vectors). Close by scoping: feeds, counters and catalogues eventually consistent; balances, inventory reservations and uniqueness on a strongly consistent path.

Model assumptions

The simulated steps use a simplified model, not a benchmark. The app sends 150 requests per second (15% writes over 40 keys, Zipf 1) through a database proxy that sends writes to the primary and spreads reads over three replicas. Each database has 12 connections, 10 ms reads and 10 ms writes. Replication is asynchronous: replicas apply a change about 300 ms after the primary confirmed it (2 s in the backlog step). A stale read is a replica read that misses a write the primary already confirmed. “Writes stop” switches the app to reads only. The engine routes every read independently (no sessions) and has a single writer, so the read-your-writes, monotonic-reads and conflict steps are scripted illustrations. Numbers show behaviour and orders of magnitude only.

The lesson as textEvery step of the simulation, written out.
  1. 1. Confirm now, copy later

    The primary takes every write and confirms it immediately. Three replicas serve the reads and apply each change about 300 ms later (the purple packets; a realistic figure for replicas under load or in other regions).

    That is the bargain of eventual consistency: writes and reads are fast and never wait for other nodes, and the replicas promise only that they will *eventually* hold the same data.

    Scene: The app writes to the primary through a proxy and reads from three replicas, which apply each change about 300 ms after the primary confirmed it. Requests are fast and succeed.

    Key idea: Eventual consistency: replicas may differ for a while, but converge if updates stop.

  2. 2. Inside the window, reads are old

    For each write there is a window of about 300 ms in which the primary has confirmed it but the replicas have not applied it. A read of that key in the window returns the previous value.

    The stale-reads tile counts them: a sizeable share of all reads, most of them on the few hot keys that are written often. Every individual answer is a value that *was* true recently; none is invented. That is the honest description of eventual consistency: you may read the past.

    Scene: Replicas lag about 300 ms behind the primary, and a sizeable share of reads return a value older than a write the primary already confirmed.

    Key idea: The inconsistency window is the replication lag. Hot keys spend most of their time inside it.

  3. 3. Stop writing, and the copies agree

    One second in, the app stops writing and only reads. The last changes are still on their way; about 300 ms later every replica has applied them.

    From then on no read is stale: all three replicas hold exactly the primary’s data, and any replica gives the same answer. That is the formal promise of eventual consistency: *if no new updates are made, eventually all reads return the last written value.*

    Notice what the promise does not say: how long “eventually” is, or what you read before then.

    Scene: Writes stop one second in. About 300 ms later all replicas have applied the last changes and no further read is stale.

    Key idea: Convergence is guaranteed only once writes stop, and only “eventually”.

  4. 4. “Eventually” has no upper bound

    Writes resume. One second in, the replicas fall behind to 2 seconds of lag: a batch job floods the primary with changes, or a replica’s disk slows down. Nothing fails and latency does not move.

    The stale-read share jumps: the window is now seven times longer. The system is still “eventually consistent”: the definition promises convergence, not *when*. If you depend on a bound, measure lag, alert on it, and treat it as an SLO, or use a store that offers bounded staleness explicitly.

    Scene: Replication lag grows to two seconds one second in. Requests still succeed with the same latency, but the share of stale reads rises sharply.

    Key idea: Eventual consistency promises convergence, not a deadline. Lag must be measured and bounded operationally.

  5. 5. Session guarantee: read your own writes

    Traffic is paused for a scripted illustration (the simulation routes each read independently, without sessions).

    Ada changes her display name. The primary confirms. The page reloads and the read goes to Replica 2, which has not applied the change: Ada sees her old name and assumes the save failed. Other users seeing the old name for a second is harmless; Ada seeing it is a bug.

    Read-your-writes fixes exactly that, for her session only: after a write, route that user’s reads to the primary for a short while, or remember the write’s log position and only read from a replica that has reached it. The last request in the animation does the former.

    Scene: Scripted: Ada’s new name is confirmed by the primary; the reload is served by Replica 2, which has not applied it, and shows the old name. A third request from her session is routed to the primary and returns the new name.

    Key idea: Read-your-writes: a user always sees their own confirmed writes, even if others briefly do not.

  6. 6. Session guarantee: time must not go backwards

    Still scripted. A new comment reaches Replica 1 quickly but Replica 3 slowly. Bob refreshes: the read lands on Replica 1 and he sees the comment. He refreshes again: this time Replica 3, and the comment is gone.

    Each answer is individually acceptable under eventual consistency, but together they make time run backwards. Monotonic reads forbids that: once a session has seen a version, it never sees an older one. Typical fixes: pin each session to one replica (sticky routing, until it fails), or have the client send the newest version it has seen and let only replicas at least that fresh answer.

    Scene: Scripted: a change reaches Replica 1 quickly and Replica 3 slowly. A first read served by Replica 1 shows the new comment; a second read served by Replica 3 does not.

    Key idea: Monotonic reads: a session never sees older data than it has already seen.

  7. 7. When two copies accept writes

    Still scripted, and beyond the single-writer setup of the simulation: in multi-leader and leaderless systems (Dynamo-style stores, offline-first apps) any replica accepts writes. Two devices add different items to the same cart at the same time: Replica 1 records + book, Replica 3 records + lamp. Then they exchange changes.

    To converge, both must apply the same rule. Last write wins keeps the one with the later timestamp, so one item silently disappears. A CRDT (here an observed-remove set) merges both: the cart has the book and the lamp on every replica. Version vectors detect that the writes were concurrent rather than one replacing the other.

    Scene: Scripted: the same cart is updated on Replica 1 (add book) and Replica 3 (add lamp) at the same time; the replicas then exchange their changes. Last write wins would keep one item; a CRDT keeps both.

    Key idea: With several writers, convergence needs a deterministic merge rule. LWW loses data; CRDTs and version vectors keep it.

  8. 8. Eventually, and what to add

    Eventual consistency buys low latency and availability (replicas answer without coordinating, even during partitions) and pays with a window in which reads can be stale. The window is the replication lag: usually short, never formally bounded.

    Make it usable: measure and alert on lag; add session guarantees (read-your-writes, monotonic reads) where users notice; pick a merge rule that does not lose data if more than one node accepts writes; and keep the few operations that need a single truth (balances, uniqueness, locks) on a strongly consistent store.

    Scene: Live traffic again with replicas about 300 ms behind the primary: fast, error-free, with some stale reads.

    Key idea: Eventual consistency plus session guarantees and safe merges covers most user-facing data.