Replicas disagree, then converge

Eventual consistency is a convergence guarantee: absent new writes, all replicas agree in finite time, and last-write-wins resolves disagreements by timestamp.

Previously

Three copies look safe — until two clients write at the same time and the copies disagree.

Scene 07

Replicas disagree, then converge

  1. Watch
  2. Try it
  3. Predict
  4. Capture
PHASE: concurrent writesR_Areplica A("qty=2", t=100ms)currentR_Breplica B("qty=5", t=105ms)currentTIMELINE (ms)0265379105W1client-1→R_Av=qty=2W2client-2→R_Bv=qty=5Two clients write key cart:42 at nearly the same time; each write lands on a different replica first.
client-1 → R_A (t=100); client-2 → R_B (t=105) — concurrent writes land on different replicas
What to watch for

Two clients write the same key at almost the same instant. The scene auto-advances through three phases: concurrent writes → diverged → converged. Watch the replica boxes — at the end, both hold the same value, picked by last-write-wins.

Continue unlocks when the animation finishes.
Implementation

Highlighted lines are the ones running in the diagram right now.

Replica.write
every replica accepts writes locally — no coordination
def write(key, value):
ts = client_clock.now_ms() # client-supplied timestamp
self.cells[key] = (value, ts)
gossip.enqueue(key, value, ts)
return OK # acknowledged before peers have it
Replica.read
a read returns whatever this replica happens to hold
def read(key):
# no consensus, no quorum yet — just local state
value, ts = self.cells[key]
return value
# R_A returns qty=2 ; R_B returns qty=5 — both 'correct'
# from their local view; the cluster disagrees with itself.
ReplicaSync.resolve (LWW)
argmax over timestamps; silently loses same-ts writes
def LWW(values):
# values = [(v1, ts1), (v2, ts2), ...]
return argmax(values, key=lambda v_ts: v_ts[1])
def reconcile(key, peer):
mine = self.cells[key]
theirs = peer.cells[key]
winner = LWW([mine, theirs])
self.cells[key] = winner
peer.cells[key] = winner # converged

Where this sits in Build a wide-column store (Cassandra / DynamoDB family)

Scene 07 of 13, in the Replication act — Copies on the next RF servers; eventual consistency under concurrent writes.. Concurrent writes hit replicas at different times; for a moment they disagree, but they converge under LWW.

Up next. If we want to be sure a read sees the latest write, we need to count replicas — not trust LWW alone.

All 13 scenes in Build a wide-column store (Cassandra / DynamoDB family) · Every curriculum

Built with Arqly
Every scene in Build a wide-column store (Cassandra / DynamoDB family) builds on the one before it.All 13 Build a wide-column store (Cassandra / DynamoDB family) scenes