VECTOR CLOCKS
Causal ordering without a global clock
Each replica stamps writes with a Lamport counter: one number, bumped on every event and raised past any stamp it receives. R1 accepts cart = milk with stamp 1. R2 has not heard from R1 and accepts cart = eggs, also stamp 1. The replicas sync and resolve with last-writer-wins on (Lamport, replica id). What survives?
There is no shared clock in a cluster, so “which write came last” has no physical answer. A vector clock gives each node one counter per node. A node increments its own entry on every local event. On receive it takes the component-wise max with the incoming clock, then increments its own entry.
Compare two clocks entry by entry. If one is <= the other everywhere and they differ, it happened before. If each is bigger somewhere, the writes are concurrent: neither saw the other. A Lamport timestamp folds all of that into one number, so it always names a winner, even when there is nothing to win.
Vector clock rules (N nodes, one counter per node):
Local event or send at node i:
VC[i] = VC[i] + 1 (attach VC to the message / version)
Receive at node j (message carries VCm):
for each k: VC[k] = max(VC[k], VCm[k])
VC[j] = VC[j] + 1
Compare:
a before b iff a[k] <= b[k] for all k, and a != b
a after b iff b before a
otherwise a || b (CONCURRENT)
Storing a key:
incoming dominates a held version -> replace it
a held version dominates incoming -> drop incoming
concurrent -> keep both (siblings)
reader merges siblings and writes back with the merged clock
Lamport clock (one counter) is consistent with causality
(a before b => L(a) < L(b)) but not the converse:
L(a) < L(b) does NOT mean a happened before b.
LWW on Lamport stamps therefore cannot see concurrency. Watch the ACKNOWLEDGED WRITES box. In LAMPORT LWW the milk write turns LOST after the second sync. In VECTOR CLOCKS the same schedule leaves both versions as siblings, and the reader’s merged write replaces them with a clock that dominates both, so nothing is lost.
One simplification. The sim uses event vector clocks, which bump on receive, so the merged write wins partly because R2’s clock absorbed R1’s on sync. Dynamo-style stores use per-key version vectors instead: a sync does not change a version. The client reads the siblings with their clocks (the context), sends that context back with its merged write, and the store stamps the new version above everything in it.
The replicas run Lamport last-writer-wins. R1 accepts two writes, R2 accepts one, and each ships its versions to the other. Order the steps so the eggs write, acknowledged by R2, disappears from both replicas without any write that saw it replacing it.
GOAL: every step ran and the eggs write is lost
Implement compare(a, b) returning 'before', 'after', 'concurrent' or 'equal', and merge(local, incoming, myIndex), the receive rule: component-wise max, then increment your own entry. Return a new array. The grader also replays two replicas writing concurrently and checks that both versions survive.
Dynamo-style stores (Riak siblings, Voldemort), CRDT libraries and sync engines use version vectors or dotted version vectors to detect conflicts. Cassandra and many caches use last-writer-wins on timestamps and accept the silent loss. Git's commit graph answers the same question: is this a fast-forward or a merge?
“How would you detect conflicting writes in a multi-leader or leaderless store?” Explain the compare rule, why a Lamport or wall-clock timestamp cannot see concurrency, what siblings are and who merges them, and the cost: one counter per writer, which is why real systems use per-replica entries and prune them.
USED IN
Chapters that build on this lesson.