Key takeaways
- ✓Last write wins keeps the update with the later timestamp, so when the replicas' wall clocks were 100 ms apart, the copy that was behind stamped its update earlier than the value it had just read, and 359 of 925 updates (39%) were thrown away at every replica, though each client had been told OK.
- ✓With perfect clocks, last write wins still lost 47 of 925 updates (5%), but those were concurrent: made so close together that neither replica had heard of the other, so one side has to be dropped, and it does not matter what the clocks say.
- ✓A hybrid logical clock (HLC: a wall-clock time plus a counter that never goes back) lost 48 of 925 updates (5%) at ±100 ms of skew, and not one update to an older write, because each replica moves its clock past every write it has seen before it stamps its own.
- ✓An HLC fixes the order of updates but not the merge: its 48 losses are concurrent updates, which only a merge rule can keep, such as the conflict-free counters and sets (CRDTs) of part 47.
- ✓A one-second lease was written with margins for a clock within ±500 ppm (parts per million, where 500 ppm is half a millisecond per second); a client whose clock ran 20,000 ppm slow had 38 of its 39 leases followed by a second holder, overlapping by up to 15.27 ms, while 0 handovers overlapped at 2,000 ppm.
- ✓The design wrote down a 1 ms margin, but the real gap between one holder stopping and the next starting was more than 3 ms and typically about 9 ms, which is why the lease held four times past its bound.
- ✓When NTP (the network time protocol that corrects clocks) stepped one client's wall clock back 50 ms every second, all 35 of its leases timed on that clock had a second holder, overlapping by up to 92.8 ms, and the same leases timed on the monotonic clock (which never steps) had none in 38.
Three copies of one value took 925 updates. With their wall clocks 100 ms apart, 39% of them vanished, and no one saw an error. With a hybrid clock, 5% vanished, the same share as with perfect clocks. Then the chapter turns to leases, a lock that expires on its own, and what happens when the clock that times it is wrong. This chapter covers both: last write wins, a hybrid logical clock, and leases with the clock bounds they assume.
The examples come from Lab 22 (Time, Clocks and Ordering) of the system design labs: script 03_lww.py for the replicas and 04_lease.py for the lease. Both are simulations. A seeded program plays the network, the delays and every clock, so it knows the true order of events. The lab runs in Python only, with no Docker and no cloud. Results are recorded on an Apple M4 Max.
Last write wins, and how skew decides it
Data often lives in several copies called replicas. When two replicas disagree, last write wins (LWW) says the one with the later timestamp is kept. Every replica applies the same rule, so they all end up agreeing, which is the point of it. The cost shows up when the timestamps come from clocks that disagree.
Most updates are read-modify-write: read the current value, add one thing, write it back. The new write is supposed to be newer than the value it read. A wall clock, the ordinary time-of-day clock, does not guarantee that. If the replica that makes the write has a clock behind the one that stamped the value it read, its own write is stamped earlier, and LWW keeps the older value everywhere. The client gets its OK; the update is gone.
In the lab, each of three replicas took about five updates a second for a minute, reading, appending its update's id and stamping it, then sending it to the other two. A replica keeps a write only if its stamp is later than the one it holds, and it checks its own writes this way too. Every discarded write was checked against the simulator's knowledge of the true order.
older write thrown away in favour of a newer one, at a replica: 0 times
older write thrown away in favour of a newer one, at a replica: 982 times
Both clocks lose about 5% to concurrent updates, which last-write-wins must choose between. Only the wall clock with skew throws away later updates in favour of earlier ones.
- Perfect clocks: 47 of 925 updates were missing from the final value (5%). None was thrown away for an older write; all 47 were concurrent, made within a network delay of each other, so LWW had to pick one.
- Wall clocks, ±100 ms of skew: 359 of 925 were missing (39%), and 982 rejections were older writes thrown away in favour of a newer stamp. Clients had been told OK for all of them.
The video replay shows which copy paid. The replica whose clock was 100 ms behind lost 78% of its own updates; the one on time lost 41%; the one 100 ms ahead lost none, because its stamps were never earlier than what it read. The per-replica figures come from that replay, not from the committed results file, but the total matches the results.
A hybrid logical clock
A hybrid logical clock (HLC) keeps the look of a time of day and adds a counter to break ties. It has one rule the wall clock lacks: when a replica receives a write, its clock jumps forward to at least that write's time, and the next stamp it makes starts from there. The clock never goes behind anything the replica has seen, so an update made after reading a value is always stamped later than that value.
The lab's HLC takes the largest of three times: the clock's current value, the stamp on the arriving write and the replica's own time of day. Then it keeps that value for its next stamp.
With the same ±100 ms of skew, the HLC lost 48 updates (5%), and none to an older write. The 48 are concurrent losses: the same kind as with perfect clocks. An HLC fixes the order; it does not merge two updates that were made at the same time. Keeping both would need a merge, such as the conflict-free counters and sets (CRDTs) of part 47.
Leases: a right to act that runs out
A lease is a lock that expires on its own. A lock server grants one client the right to act for one second; after that, the server may grant it to someone else. The lease is only safe if the client and the server agree about when a second is over, and each of their clocks may run a little fast or slow.
The design in the lab writes down the assumption. Every clock's rate is within ±500 ppm (parts per million: 500 ppm is half a millisecond per second, 100 ppm is about 8.6 seconds a day). Two margins follow from that:
- The client counts its lease from when it sent its request, which is earlier than the grant, and it stops after 1 s × (1 − 2 × 500 ppm), which is 999 ms on its own clock. The 1 ms is the margin the design wrote down.
- The server grants the lease to someone else only after 1 s on its own clock.
The lab used three clients asking for the lease every 10 ms, with delays of 1 to 5 ms. One client's clock ran slow by 0 to 20,000 ppm, so it believed its lease lasted longer than it did. The simulator checked every handover in true time: did the old holder still believe it held the lease after the new holder got it?
longest overlap: none
longest overlap: none
longest overlap: none
longest overlap: none
longest overlap: none
longest overlap: 0.23 ms
longest overlap: 15.27 ms
Bars show the slow client's leases that had a second holder. The last lease of each run had no next holder to overlap, so 20,000 ppm shows 38 of 39. Each setting ran 120 s (119 leases per run, simulated).
When a lease outlives its bound
Within the bound, no handover ever had two holders. Past it, the result depends on how far:
- At 2,000 ppm (four times the bound): no handover overlapped across the slow client's 38 leases.
- At 5,000 ppm (ten times the bound): 1 handover overlapped, by 0.23 ms.
- At 20,000 ppm (forty times the bound): 38 handovers overlapped, by up to 15.27 ms, and 409 ms in total. The slow client had 39 leases; the last had no next holder to overlap.
Why did it hold so far past its bound? Because the design's margin was not the whole margin. Counting from the request, the lease also absorbed the request's trip to the server, the wait for the next client to ask, and the grant's trip back. In the video's replay with perfect clocks, the gap between one holder stopping and the next starting was more than 3 ms and typically about 9 ms. Only 1 ms of it was written down. At 5,000 ppm, the slow client's lease ran about 5 ms past where it should have stopped, which is more than that smallest gap, and it overlapped once. This hidden margin is arithmetic plus the replay's measurements, not something the lab's results file records. A faster network, or counting from the grant instead of the request, would shrink it, and the design would then fail sooner.
NTP steps: a clock that jumps
The last test had no drift at all. Instead, NTP stepped the client's wall clock back 50 ms once every second. A step is a jump, not a slow drift, and a lease lasts about a second, so every lease met a step.
- Timed on the wall clock: all 35 of the client's leases had a second holder, overlapping by up to 92.8 ms. Two steps can land in one lease, which is why the longest overlap is much larger than 50 ms.
- Timed on the monotonic clock: none of its 38 leases had a second holder. The monotonic clock only counts forward and is never stepped by NTP, though it still drifts.
A lease that is timed correctly still needs a fencing token (part 44): a number that rises every time the lease is granted, which the storage checks, so a holder paused past its lease cannot write with an old number.
The workflow and the results
The 03_lww.py script runs three replicas with skews of 0 and ±100 ms, once with wall clocks and once with HLCs, for 60 seconds each. The 04_lease.py script runs one client through seven clock rates, 120 seconds each, then the two NTP variants at 0 ppm. Each setting uses one seed (seed 1), so each row is a single recorded run.
| Skew | Timestamps | Updates | Missing from final value | Thrown away for an older write | Concurrent rejections |
|---|---|---|---|---|---|
| ±0 ms | wall clock | 925 | 47 (5%) | 0 | 60 |
| ±0 ms | HLC | 925 | 47 (5%) | 0 | 60 |
| ±100 ms | wall clock | 925 | 359 (39%) | 982 | 65 |
| ±100 ms | HLC | 925 | 48 (5%) | 0 | 70 |
Rejections are counted at each replica, so a single update can be rejected at two replicas and the count can exceed the number missing.
| Clock rate of one client, against the ±500 ppm bound | Handovers with two holders | Longest overlap | Its leases |
|---|---|---|---|
| 0, 200, 500 ppm (within the bound) | 0 | none | 38 to 40 |
| 1,000, 2,000 ppm (past it) | 0 | none | 38 |
| 5,000 ppm | 1 | 0.23 ms | 37 |
| 20,000 ppm | 38 | 15.27 ms | 39 |
| 0 ppm, clock stepped back 50 ms every second | Leases with two holders | Longest overlap |
|---|---|---|
| Lease timed on the wall clock | 35 of 35 | 92.8 ms |
| Lease timed on the monotonic clock | 0 of 38 | none |
Caveats
- Everything is simulated. The network, the delays (1 to 10 ms for updates, 1 to 5 ms for leases) and every clock are the lab's model. The lab's own table has no real network or real NTP daemon behind it.
- One seed per setting. Each row is one recorded run. The 5% concurrent loss depends on this workload's rates and delays, and is not a general rate.
- The overlaps are small in number. The single 5,000 ppm overlap is one event. The 20,000 ppm result is the clearer one.
- The hidden margin is not in the results file. The 3 to 9 ms gap and the 78%, 41% and 0% per-replica losses come from the video's replay of the same run, which reproduces the committed totals. The committed JSON has only the totals.
- HLC's zero is by construction. The rule makes an update after a read always later, so zero losses to older writes is guaranteed, not measured. Its 5% concurrent losses are measured.
- Not tested: an HLC maximum-offset rule (refusing a stamp far ahead of the local clock), fencing tokens in the lab's own code, counting the lease from the grant, and the overlaps in a real cluster. The lab lists some of these as exercises.
The checklist
- Don't let the time of day decide. Last write wins on wall clocks drops causally later updates whenever the clock behind stamps an earlier time.
- Stamp with a hybrid clock so an update made after a read is always later than the value it read.
- Merge what's concurrent. A better clock fixes the order, not the choice between two concurrent updates.
- Write the clock bound down, and test past it. A lease is only safe while the clock bound it assumed holds, and the margin you didn't write down is the one you'll remove.
- Time leases and timeouts on the monotonic clock, and add a fencing token for anything a paused holder could still write.
Quiz
1.A replica whose clock is 100 ms behind reads a value stamped by a replica whose clock is 100 ms ahead, adds its own update and stamps it with its own clock. With last write wins on wall clocks, what happens to that update?
2.At ±100 ms of skew, the hybrid logical clock lost no update to an older write. Why, when its replicas' clocks were still 100 ms apart?
3.A lease lasts one second and is written for clocks within 500 ppm. A client's clock runs 20,000 ppm slow, so its lease lasts longer than it should. What happens in the lab?
Check your understanding
Try each question before opening its answer.
Why did last write wins lose 982 later writes at ±100 ms of skew, when the replicas' clocks were just 100 ms apart?
A replica whose clock was behind read a value stamped by a clock that was ahead, then stamped its own update with its earlier time. Every replica keeps the value with the later stamp, so it threw the update away in favour of the older write it had already held. The client had been told OK. The count of 982 is at each replica, so one update can be counted more than once.
What does the hybrid logical clock fix, and what does it still lose?
It fixes the order: a replica's clock moves past every write it has seen, so an update made after reading a value is always stamped later, and the lab recorded no losses to older writes. It still loses concurrent updates, the 48 of 925 (5%) at ±100 ms, because two writes made within a network delay of each other have no order to keep. Only a merge rule can keep both.
A lease counts from when the request was sent and stops 1 ms early. Why did it hold to 2,000 ppm, four times its bound?
The design's margin was 1 ms, but the real gap between one holder stopping and the next starting was more than 3 ms and typically about 9 ms. The extra came from the request's trip to the server, the wait for the next client to ask, and the grant's trip back. Only the 1 ms was written in the design, so the lease held well past the bound the design assumed.
Why did the monotonic clock avoid all overlaps when the wall clock had 35 of 35 leases overlapped by an NTP step?
The monotonic clock only counts forward and is never stepped by NTP, so a 50 ms step in the wall clock doesn't change how long the client thinks its lease has run. It still drifts at its own rate, which is why the design still has a bound. The steps are the part a lease can't survive on a wall clock.
A client has been granted a lease and then pauses for longer than the lease. Why does a correct lease still need a fencing token?
The lease tells the client when it may act, but it cannot stop a paused client from acting after the lease has ended, because the pause is outside the lease's control. A fencing token is a number that rises with each grant. The storage refuses writes with an older number, so a paused old holder's late write is rejected.