Build a wide-column store (Cassandra / DynamoDB family) (13 scenes)
Scene 07 · Replicas disagree, then converge
Concurrent writes hit replicas at different times; for a moment they disagree, but they converge under LWW.
Previously
Three copies look safe — until two clients write at the same time and the copies disagree.
Scene 07
Replicas disagree, then converge
Diagram
Two replica boxes (R_A, R_B) above a horizontal timeline. Coloured pills on the timeline are writes from different clients — each write lands on one replica first. The phase chip up top names where we are: concurrent-writes (in flight), diverged (replicas hold different values), or converged (replica-sync ran and last-write-wins picked the higher timestamp). The footer surfaces the LWW winner once it's known.
client-1 → R_A (t=100); client-2 → R_B (t=105) — concurrent writes land on different replicas
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.
Implementation
Replica.write
every replica accepts writes locally — no coordination
1def write(key, value):2 ts = client_clock.now_ms() # client-supplied timestamp3 self.cells[key] = (value, ts)4 gossip.enqueue(key, value, ts)5 return OK # acknowledged before peers have it
Replica.read
a read returns whatever this replica happens to hold
1def read(key):2 # no consensus, no quorum yet — just local state3 value, ts = self.cells[key]4 return value56# R_A returns qty=2 ; R_B returns qty=5 — both 'correct'7# from their local view; the cluster disagrees with itself.
ReplicaSync.resolve (LWW)
argmax over timestamps; silently loses same-ts writes
1def LWW(values):2 # values = [(v1, ts1), (v2, ts2), ...]3 return argmax(values, key=lambda v_ts: v_ts[1])45def reconcile(key, peer):6 mine = self.cells[key]7 theirs = peer.cells[key]8 winner = LWW([mine, theirs])9 self.cells[key] = winner10 peer.cells[key] = winner # converged