Topic 134 / Advanced
Logical clocks and version vectors: concurrent updates, conflict resolution
Chapter 8.4 · Time, Ordering, and Conflicts
What and why
Multi-leader and leaderless replication (→ 72) let more than one node accept writes to the same data, so two writes to the same key can happen with neither node knowing about the other — a genuine concurrent update, not a race better locking would prevent. Physical clocks can't resolve this safely (→ 135: they drift), so distributed systems order and detect concurrency with logical clocks instead. A Lamport clock gives every event a counter such that if event A causally influenced event B, A's counter is smaller than B's — a "happens-before" relationship — but it cannot by itself tell two genuinely concurrent events apart from mere coincidence. A version vector extends this per replica, tracking each replica's own counter, so comparing two vectors can positively detect when two writes were concurrent (neither vector dominates the other) rather than one simply superseding the other. Builds on: → 72 (replication — multi-leader write conflicts are exactly what this topic resolves).
Diagram
Replica A writes: version {A:1, B:0}
Replica B writes concurrently
(never saw A's write): version {A:0, B:1}
Neither vector dominates → CONCURRENT → conflict must be resolved
Replica A then reads B's write and writes again: {A:2, B:1}
{A:2,B:1} dominates {A:1,B:0} and {A:0,B:1} → supersedes both, no conflict
How it works
- A Lamport clock increments on every local event and, on receiving a message, jumps to max(local, received) + 1 — this guarantees A happens-before B implies clock(A) < clock(B), a total order consistent with causality; the converse doesn't hold.
- A version vector keeps one counter per replica instead of one global counter; a write at replica R increments only R's own position.
- Comparing two vectors: if every position in X is ≤ the corresponding position in Y (and at least one strictly less), X is older than Y — Y supersedes X cleanly. If neither vector is ≤ the other in every position, the writes are concurrent — a genuine conflict.
- Concurrent writes are resolved by policy, because there's no answer the system can derive on its own: last-write-wins picks one by a tiebreaker and discards the other (simple, loses data); surfacing siblings keeps both and returns them for the application or user to merge (Capstone); domain-specific merge applies logic that understands what the data means (a shopping cart union-merges both sets of added items — Project 17's sync-conflict handling).
- The resolution policy is a real design decision, not a detail — last-write-wins is attractive exactly because it's automatic, and exactly because that automation is what silently loses a write.
Practical example
(illustrative). Two replicas, A and B, both start at {A:0, B:0}. A accepts a write without having seen any write from B, producing {A:1, B:0}; concurrently B accepts a different write without having seen A's, producing {A:0, B:1}. Comparing them: position A is higher in the first, position B is higher in the second — neither dominates — so the system correctly flags these as concurrent rather than silently picking one as "the" value.
When to use it
- Use when: more than one node can accept writes to the same key and silently losing one of two concurrent writes is unacceptable — surface siblings or merge, don't default to last-write-wins.
- Avoid when: a single leader already makes every write to a key sequential — there's no concurrent-update problem to solve.
- Failure scenarios: ordering events by plain timestamp instead of a logical clock, then having clock drift (→ 135) silently misorder causally related writes; defaulting to last-write-wins for data where the "losing" write represented something a user did (an added cart item, a comment) that simply vanishes.
- Simpler alternative: a single-leader system sequences all writes to a key naturally — no vectors needed until multi-leader or leaderless writes make concurrency possible.
Practice
(Mechanism kind — trace by hand; 20–25 min, no lab services). Starting from {A:0, B:0, C:0} on three replicas, trace five writes in order: A writes locally; B writes locally without having seen A's write; C syncs from A, then writes; A syncs from C, then writes. After each step, write the resulting vector, and for every pair of vectors produced so far, state whether they're ordered or concurrent. Done when the A/B pair from steps 1–2 is correctly flagged concurrent and the later C and A writes are correctly flagged as dominating earlier ones.
Key takeaway
Lamport clocks give a causally consistent order; version vectors go further and positively detect when two writes were truly concurrent, so conflicts can be resolved by policy instead of silently overwritten. Done when you can: compare two version vectors by hand and correctly classify them as ordered or concurrent. Used next in → 135; Project 17; Capstone.
References
Kleppmann and Riccomini, Designing Data-Intensive Applications — see
references.md.