Aller au contenu principal

Concurrent, Not Just Newer: Version Vectors and the Writes LWW Hides

· 11 minutes de lecture
Clustron Team
Ingénierie des systèmes distribués

Two versions of the same key arrive at a replica, and it has to pick a survivor. The cheapest way to decide is a single timestamp or a scalar counter: whichever version carries the larger value wins, and the loser is discarded. That is last-write-wins, it is what a lone logical clock gives you, and most of the time it is quietly throwing away data you meant to keep.

The problem is that "larger" is not "later" in the only sense that matters. Consider a shopping cart stored under one key. A client reads the cart, adds milk, and writes it back; that new version genuinely descends from the one it read, so keeping it is correct — the add built on what came before. Now consider two clients that both read the same cart at the same instant: one adds milk, the other adds eggs, and each writes back without ever having seen the other's change. Neither version descends from the other. They are concurrent — two real edits made against the same starting point — and last-write-wins keeps exactly one of them. The other add is gone, with no error and no log line; the cart just appears to lose an item on its own.

What last-write-wins lacks is any way to tell those two situations apart, because a single scalar can only ever say which value is bigger — it cannot express "these two are incomparable." A version vector adds precisely that missing capability. It is a small map from writer to counter that records who has contributed to a value and how many times, and comparing two of them reveals whether one happened after the other or whether the two happened at once. This post is about how that encoding works, how the comparison detects concurrency, what to do with the conflicts it surfaces, and when plain last-write-wins is still the right answer.

A counter per writer, not one clock for everyone​

A version vector is a map {writer → counter}. Each time a replica accepts a write for a key, it increments its own entry in that key's vector and leaves the others untouched. A value that has only ever been touched by replica A might carry {A:2}; one that A and B have each written once carries {A:1, B:1}. The vector is not a clock reading — it is a summary of the causal history, a record of which writers' updates are folded into this value.

The comparison is where the power is. Given two vectors, compare them component by component:

  • If every component of X is greater than or equal to the matching component of Y, and at least one is strictly greater, then X dominates Y — X's history includes everything in Y's and then some, so X is a true descendant. Keep X; it is the later write and it already subsumes Y.
  • If X dominates Y in some components but Y dominates X in others — each has a counter the other lacks — then neither dominates, and the two versions are concurrent. Each one saw updates the other did not. This is a genuine conflict, and discarding either silently loses a real write.

That partial order is the "happened-before" relation applied per key: a single scalar can only place every version on one line, but a vector can say that two versions sit on different branches and cannot be ordered at all. Detecting that "cannot be ordered" is the entire point.

Descendant — the write saw the base, so it dominatescart · {A:1}basecart+milk · {A:2}dominates {A:1} → keepConcurrent — neither write saw the other, so neither dominatescart · {A:1}base+milk · {A:2}+eggs · {A:1,B:1}cart+milk+eggsmerged · {A:2,B:1}a scalar clock would keep only one branch; the vector shows they are siblings, so both edits surviveA:2 > A:1 but B:1 > B:0 → incomparable → reconcile, don't overwrite

What you do with a sibling​

Detecting concurrency is only useful if you then handle it, and there are two honest options. The first is to keep both concurrent values as siblings under the key. A subsequent read returns the set of siblings rather than a single value, and whoever reads — the application, or a merge function registered with the store — reconciles them: a cart unions the items, a counter takes the sum, a set-valued field unions its members, and a last-resort scalar can still fall back to a timestamp if the domain truly has no better rule. The merge is where replicated data types earn their keep, because a type with a commutative, idempotent merge turns sibling reconciliation into something the store can do automatically and deterministically on every replica.

The reconciled value is written back with a vector that dominates both siblings — the component-wise maximum of the two, plus the writer's own increment — so the conflict is sealed: future reads see one value again, and its vector records that it descends from both branches. The conflict was surfaced, resolved intentionally, and retired, instead of being decided by whichever write happened to carry the larger clock.

// Compare two version vectors for the same key.
Ordering Compare(VV x, VV y) {
bool xDom = false, yDom = false;
foreach (var w in x.Writers.Union(y.Writers)) {
if (x[w] > y[w]) xDom = true; // x has history y lacks
if (y[w] > x[w]) yDom = true; // y has history x lacks
}
if (xDom && yDom) return Concurrent; // neither subsumes the other → siblings
if (xDom) return Descends; // x is the later write → keep x
if (yDom) return Precedes; // y is the later write → keep y
return Equal;
}

// On a conflicting write, don't overwrite — store a sibling and let a merge resolve it.
if (Compare(incoming.VV, stored.VV) == Concurrent)
store.AddSibling(key, incoming); // reconciled later; merged VV = max(both) + 1

The cost, and when last-write-wins is fine​

Version vectors are not free. Each distinct writer adds an entry, so a vector grows with the number of actors that have touched the key, and left unbounded it can bloat — the standard fix is to track writers by replica rather than by client, which caps the vector at the replica count, and to prune entries that every replica has already seen. Siblings carry their own risk: if nobody ever reconciles them, they accumulate, and a key can sprout an ever-growing pile of unresolved versions. The machinery pays for itself only when you will actually act on the conflicts it finds.

So last-write-wins remains the right call in two common cases: when concurrent writes to the same key are genuinely rare, so the hidden loss almost never happens, or when losing one of two concurrent writes is acceptable because the field is a simple overwrite with no merge semantics — a status flag, a cached render, a "last seen" timestamp. The quorum model leans on exactly this escape hatch: two clients can both reach quorum on the same key and the model will not order them for you, so it falls back on last-write-wins unless you supply richer logic — and version vectors are that richer logic, to be spent only where a lost write is a bug rather than a shrug.

Wrap-up​

A single clock can tell you which of two versions is larger, but not whether one was built on the other or whether they were written at the same time against the same base — and that distinction is the difference between a safe update and a silently dropped write. Version vectors restore it by keeping a counter per writer: one vector dominates another when it subsumes its whole history, and when neither dominates, the versions are concurrent siblings that must be reconciled rather than overwritten. Handle the siblings with a merge function — ideally a replicated data type whose merge is automatic — seal the result with a vector that descends from both, and no concurrent edit disappears without someone deciding it should. Keep last-write-wins where conflicts are rare or losses are harmless; reach for version vectors where every write has to survive, and pay their bookkeeping cost on purpose.