Module 5 · Distributed systemsLesson 60 of 887 minAdvanced

Eventual consistency in practice

Eventually consistent systems let copies disagree for a while, spread updates in the background, and need a rule for resolving conflicts.

Try it

Write a new value and watch it spread node to node until all six agree. Then try "Two writes at once" and see which value wins.

In an eventually consistent system, a write is accepted by one copy and spreads to the others later. If writing stops, all copies end up the same. Until then, readers may see different things.

How updates spread

  • Replication streams: the node that took the write sends it to the others.
  • Gossip: every second or so, each node swaps notes with a random peer. Information spreads like a rumour, reaching all nodes in a handful of rounds even in big clusters.
  • Anti-entropy and read repair: background jobs compare copies (often using Merkle trees, to find differences cheaply), and reads that notice an old copy fix it on the spot.

The hard part: conflicts

If two copies accept different writes to the same item at roughly the same time, which one is right?

Last write wins (LWW): keep the write with the latest timestamp. Simple, used by Cassandra. But it silently drops the other write, and clock skew can even make it drop the newer one.

Version vectors: each copy tracks a counter per node. The system can tell whether one write came after another, or whether they were concurrent. Concurrent ones can be kept as siblings and merged by the app (Riak and the original Dynamo did this).

CRDTs (conflict-free replicated data types): data structures designed so that merging always works without losing anything:

  • a counter where each node keeps its own count, summed on read,
  • a set where adds and removes merge sensibly (a shopping cart),
  • text CRDTs that power collaborative editors.

Designing for it

  • Show users their own writes immediately (read-your-writes).
  • Prefer data shapes that merge: sets and counters over single overwritten values.
  • Make the window visible when it matters ("syncing...").
  • Keep strongly consistent storage for the few things that can't be merged, like money and unique names.

Quick check

Two users edit the same shopping cart on two replicas during a network split. With last-write-wins, what happens when it heals?