Last writer wins: a deterministic choice with a clock problem
Trace timestamp arbitration, tie-breaking, and clock skew while separating convergence from the user’s intended last edit.
On this page
The question for today
What you’ll understand
- Calculate which value a timestamp-based register retains.
- Construct a skew example where the later real write loses.
- State which guarantees logical or hybrid clocks can and cannot add.
Last-writer-wins sounds like a description of user intent, but the implementation usually selects a maximum ordering tag. That tag may contain a physical timestamp plus a deterministic tie-breaker. The replicas can agree perfectly while discarding the edit a person actually made later. Understanding the ordering policy lets you decide where silent replacement is acceptable and where a richer conflict model is required.
Define the winner before discussing correctness
Model a register state as a value paired with a totally ordered tag. Merge returns the pair with the greatest tag, using a stable tie-breaker when timestamps agree. The tie-breaker must distinguish otherwise competing writes or prescribe the same final value everywhere; merely choosing whichever message arrived last is not deterministic across replicas. A common illustrative tag is (timestamp,actor-ID), but multiple writes by one actor at one timestamp require an additional counter or another uniqueness discipline.
This maximum operation is associative, commutative, and idempotent when tags form a valid total order and identical tags cannot represent conflicting payloads. These properties explain convergence under repeated and reordered state exchange. They do not prove that the greatest timestamp is the latest real event, nor that losing a value is appropriate. A convergent color preference and a convergent audit log have very different requirements. Write down whether discarded alternatives remain available for recovery or disappear permanently.
Clock error can reverse the real order
Let a local physical reading equal real time plus an offset. If two offsets differ by more than the elapsed real time between writes, their timestamp order can reverse. A clock jumping backward can also make a writer’s new timestamp smaller than one it previously emitted. A badly fast clock may dominate updates from otherwise healthy replicas until they catch up. Timestamp validation and monotonic local generation can limit some damage, but each additional rule must be part of the documented protocol.
Logical clocks can respect a happens-before relation when their update and message rules are followed, while still assigning arbitrary order to concurrent events. Hybrid logical clocks combine physical-time information with logical components to preserve causal ordering under their stated algorithm; they do not eliminate uncertainty about real-time order of concurrent edits. None of these tags by itself supplies multi-object transactions, uniqueness constraints, or an inventory floor. Distinguish the mathematical convergence proof from whatever physical-time or business interpretation a product places on the winner.
Evaluate data loss as a product decision
For a display preference, choosing one concurrent update may be an acceptable documented policy. For an address change, retaining alternatives and asking the user can be safer. For an immutable audit event, store both events rather than treating an overwritten register as a history. Applications should expose conflict indicators when needed and test clock offsets, equal timestamps, delayed delivery, and repeated updates from a single actor. Authorized clients also need a controlled tag-generation policy if arbitrary timestamps could suppress other writers.
The modular lab illustrates why a finite wrapping tag alone cannot represent unlimited recency. Reset, set modulus m=8 and additive step a=1, predict the current residue zero, then Step repeatedly. After eight transitions the residue returns to zero. This is not a wall clock or an LWW register: its only model is addition modulo m. The useful counterexample is that repeating labels cannot by themselves distinguish an old event from a new one. Real clock tags need their own ordering and reuse rules.
Work it through
The later real edit loses
Write A occurs at real time 1,000 ms with a clock offset of +100 ms. Write B occurs later at 1,050 ms with offset −50 ms. An LWW register selects the greater physical timestamp.
A receives timestamp 1,000+100=1,100 ms. B receives timestamp 1,050−50=1,000 ms. The real elapsed time is 50 ms, while the offset difference is 150 ms.
Because 1,100 exceeds 1,000, the register retains A despite B occurring later in real time. Replica convergence can still be exact because all replicas apply the same maximum-tag rule.
If offsets were bounded within ±10 ms, the maximum offset difference would be 20 ms, so this particular 50 ms separation could not reverse under those bounds. That conditional conclusion does not make every pair of nearby writes unambiguous or supply transaction semantics.
A wins under the stated tags. Calling it the latest user edit would confuse deterministic arbitration with real-time knowledge.
The living notebook
Explore the model
Interactive lab · modular
A wrapping label repeats
Reset, set modulus m=8 and additive step a=1, predict residue 0, then Step. Eight steps advance eight additions and return to 0. This additive cycle is not a physical clock, logical clock, or replicated register.
- Residue r0
- 0
- Orbit period · m/gcd(m,a)
- 12
- Distinct residues visited
- 1 of 12
Addition modulo m starts at zero: rₙ = na mod m. Playback advances two additions per second. The orbit has period m/gcd(m,a), including period one for a = 0. This is not a cryptographic security test or a primality algorithm. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forA finite repeated label needs additional history to distinguish different events.
Pause & reason
Check your understanding
Your turn
Try it yourself
A real write at 2,000 ms has offset +30 ms, followed at 2,040 ms by a write with offset −20 ms. Which timestamp wins, and how large is the skew difference?
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Compare the tag values rather than the event times, then compare the two offsets.
Compare with the solution
The tags are 2,030 ms and 2,020 ms, so the earlier write wins. The offset difference is 50 ms, greater than the 40 ms real separation. A deterministic tie-breaker would not repair this reversal because the timestamps are unequal.
Keep the idea
Take it with you
- LWW selects a maximum tag, which needs a complete tie rule.
- Clock skew can make an earlier real write defeat a later one.
- Convergence does not make silent loss or cross-object invariants acceptable.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.