Key takeaways
- ✓With each machine's clock up to 10 ms off, 366 of 779 messages (47%) were stamped as received before they were sent, and raising the skew to 100 ms did not raise that share, because once the skew is larger than the 1–10 ms message delay, every message from a fast clock to a slow one already looks early.
- ✓A Lamport clock, a counter that goes up by one on every event and jumps past the sender's number on every message received, reversed 0 of 148,886 causally ordered pairs at every skew from 0 to 100 ms, but it cannot say whether two events were independent.
- ✓A vector clock, one counter per node, flagged all 91,458 independent pairs among the 199,911 sampled at 50 nodes (46%) and got none wrong, at a cost of 357 bytes on average per message at that size, against 8 bytes for a Lamport number.
- ✓Averages hide wall-clock errors: at 100 ms of skew, 956 of 148,886 causal pairs (0.64%) were reversed, but among the 5,989 causal pairs less than 200 ms apart, 1,064 (18%) were reversed, and those are the pairs a system usually has to order.
- ✓A hybrid logical clock (HLC), which keeps the largest wall time it has seen plus a counter, kept its time within 100 ms of true time at 100 ms of skew, its counter never passed 17, and it reversed 0 causal pairs.
Three machines stamped their messages with their own clocks, and by those clocks almost half the messages arrived before they were sent. This chapter takes apart why, then looks at the two counter-based clocks that never made that mistake, and at the one that could also say when two events had no causal link.
The examples come from Lab 22 (Time, Clocks and Ordering) of the system design labs. This chapter covers the first two scripts, 01_ordering.py and 02_vectors.py. The last-write-wins and lease scripts (03_lww.py and 04_lease.py) are in the next episode, 55. Everything runs in one Python process on a simulated network, so the simulator knows the true order of every event. That is the only reason the lab can say a timestamp was wrong.
Wall clocks and skew between machines
Every machine has a wall clock, the one that shows the time of day. Two wall clocks never agree exactly. The gap between them at a given moment is the skew. The rate at which the gap grows is the drift, and network time sync (NTP) corrects both, sometimes by setting the clock to a new value at once, even backwards.
A timestamp from a wall clock is therefore only as good as the skew. If two machines disagree by 10 ms, a timestamp from one of them can be 10 ms off compared with the other, and that is enough to change which of two close events looks first.
Why a message can appear to arrive before it was sent
Take one node whose clock is 10 ms ahead. It sends a message at true time zero and stamps it 10. The message takes 1 to 10 ms, and the receiver's clock is right, so it stamps the arrival at 1 to 10. That is never later than the 10 on the send, so the receive looks as early as or earlier than the send.
With three nodes there are six directions a message can travel. The skew was spread as one node behind, one right, one ahead. In the lab, the directions that run from a fast clock to a slow one were three of the six, and 47% of messages (366 of 779) were stamped early. Once the skew reaches the longest delay, every message on those directions looks early. More skew adds no more directions, so the share stays at 47%.
Lamport and HLC timestamps never reversed a cause and its effect at any skew. The HLC time stayed within ±10 ms of true time, and its counter reached at most 5.
Simulated network with 1–10 ms message delays and no losses; the simulator knows the true order. The lab counts close pairs separately from all causal pairs, so the two counts are not subsets of each other. One run per skew.
The NTP step case (-50 ms on node n1, 768 events): wall clock stamped 3 events no later than an earlier one; Lamport and HLC stamped 0.
The table in the widget comes from the lab's first script. At 1 ms of skew only 19 messages (2.4%) looked early, because most of the arrivals were slower than the gap. At 10 ms it jumped to 366, and at 100 ms it was still 366.
Averages hide the errors
Looking at all causally related pairs of events hides most of the damage. Two events are causally related when one could have caused the other: an earlier event on the same machine, or a chain of messages leading from one to the other. In the lab's sample of 148,886 such pairs, most were seconds apart. At 100 ms of skew, only 956 of them (0.64%) were in the wrong order.
The pairs a system usually has to order are the close ones: a write and the read after it, a message and its reply. In the lab's 5,989 causal pairs less than 200 ms apart, 1,064 (18%) were in the wrong order at 100 ms of skew. The lab reports the two as separate counts. The close-pair count (1,064) is larger than the all-pairs count (956), so do not read one as a subset of the other.
Lamport clocks: a counter that only moves forward
A Lamport clock is one counter per node. It goes up by one on every event, and when a node receives a message it jumps to one more than the larger of its own counter and the number the message carries. Think of it as a page number in a set of letters: always one more than any page you have read.
The rule guarantees that a cause always has a smaller number than its effect, because every receive moves the counter past what was sent. It never reads the wall clock, so skew cannot break it. The lab found 0 reversed causal pairs at every skew from 0 to 100 ms. That is by design, not luck.
The price is what the counter cannot say. A Lamport number that is smaller for one event than another does not mean the first caused the second. Concurrent events, the ones with no causal link, also get an order, and that order is arbitrary.
Vector clocks: one counter per node
A vector clock keeps one counter per node, and each message carries the whole list. To compare two events, you look at the lists node by node. If every entry of one is no larger than the other's, the first could have caused the second. If one is larger in some entries and smaller in others, neither could have caused the other, so the two are concurrent. A single counter can never give that third answer.
The cost is the list itself. It grows with the number of nodes that have written, and it is carried on every message. The lab measured it at 3, 10 and 50 nodes.
Vector clocks got every pair right: 0 wrong directions and no causal pair flagged as concurrent. A Lamport timestamp is 8 bytes; the vector grew to a largest of 540 bytes on a message.
Simulated network at ±10 ms skew, 150 events a second across all nodes. The vector's "0 wrong" is by construction: the simulator's truth is kept as the same kind of vector. One run per node count. The "Ordered by Lamport" bar is a share of the concurrent pairs.
Hybrid logical clocks: a counter that still reads as a time
A Lamport number says nothing about when something happened. A hybrid logical clock (HLC) keeps the largest wall time it has seen, from its own clock or from any message, and adds a counter to break ties. Its time can therefore be read and compared with a real time, and it still never puts an effect before its cause.
At 100 ms of skew, the lab's HLC time stayed within 100 ms of true time, never further. Its counter reached 17 at most. It reversed 0 causal pairs, the same as the Lamport counter. This is the kind of timestamp that a database can use for transactions and still show a person as a time.
The workflow and the results
The lab's simulator, simnet.py, runs three nodes that exchange messages. Each node does 50 things a second for 10 seconds, half of them sends, and each message takes 1 to 10 ms. The nodes stamp every event with four kinds of timestamp: the wall clock, a Lamport counter, an HLC and a vector. clocks.py holds the three counter rules, and workload.py drives the chatting nodes.
Script 01, ordering. It runs the nodes at skews of 0, 1, 10 and 100 ms, with one wall clock per node spread as one behind, one right and one ahead. For 300,000 random pairs of events and every message, it asks whether each timestamp puts the cause first. Then it steps one node's wall clock back 50 ms and checks the events stamped after the step.
| Skew | Wall clock: all causal pairs reversed | Close pairs (<200 ms) reversed | Messages received before sent | Lamport | HLC |
|---|---|---|---|---|---|
| 0 ms | 0 of 148,886 | 0 of 5,989 | 0 of 779 | 0 | 0 |
| ±1 ms | 0 | 3 (0.05%) | 19 (2.4%) | 0 | 0 |
| ±10 ms | 51 (0.03%) | 45 (0.75%) | 366 (47%) | 0 | 0 |
| ±100 ms | 956 (0.64%) | 1,064 (18%) | 366 (47%) | 0 | 0 |
With the wall clock of one node stepped back 50 ms at 5 seconds, 3 of that node's 768 events were stamped no later than an earlier event on the same node. Lamport and HLC had 0.
Script 02, vectors. It spreads the same traffic, 150 events a second across all nodes at ±10 ms of skew, over 3, 10 and 50 nodes. It samples 200,000 pairs of events for each count and compares each with the truth.
| Nodes | Truly independent pairs | Vector clock flagged independent | Wrong direction | Lamport gave an order to | Vector size, mean | Largest vector | A Lamport number |
|---|---|---|---|---|---|---|---|
| 3 | 1,093 (0.5%) | 1,093 | 0 | 975 | 32 bytes | 33 bytes | 8 bytes |
| 10 | 9,997 (5.0%) | 9,997 | 0 | 9,621 | 102 bytes | 110 bytes | 8 bytes |
| 50 | 91,458 (46%) | 91,458 | 0 | 89,404 | 357 bytes | 540 bytes | 8 bytes |
Two things stand out. Independence grows with the node count: with the same traffic spread over more nodes, each node hears from each other node less often, so almost half the pairs were independent at 50 nodes. And the Lamport clock put an order on 98% of those pairs anyway, so a system that detected conflicts with Lamport numbers would see almost none.
Caveats
- One run per setting. Each row is one recorded run. The simulator is deterministic, so the same seed gives the same numbers, and another seed is another run.
- The simulator knows the truth. Causes in the lab are only the messages the simulator sends. In a real system, two people talking in a room, or an action outside the system, can make one event a cause of another without any message the clocks can see.
- The vector clock's zero wrong is by construction. The simulator keeps its own hidden vector clock to decide what was truly causal, so script 02 shows the code follows the rule, not that vectors are perfect in the wild.
- The skew values are set, not measured. The simulator is given offsets of 0, 1, 10 and 100 ms. The numbers show what skew of that size does; they do not measure how large skew is on real machines.
- Three to 50 nodes. The lab does not test larger clusters or the cost of pruning a vector (dropping old entries), which real stores do to keep the list short.
- Not covered here: last-write-wins and leases, which depend on these clocks and are the subject of episode 55, and Spanner's TrueTime, which gives a time interval with a bounded error and waits it out; the lab lists a TrueTime-style version as an exercise.
The checklist
- Assume wall clocks disagree by the skew. Once the skew is bigger than the shortest delay between two machines, a message can look as if it arrived before it was sent.
- Look at close pairs, not averages. A small share of all pairs can hide a large share of the pairs you care about.
- Use a Lamport or HLC number to keep causes before effects. Both kept 0 reversed causal pairs at every skew in the lab.
- Use a vector only when you need to know that two events were independent. It costs a list on every message, and the list grows with the nodes that write.
- Prefer an HLC when a person will read the time. It stays within the skew of real time and still never reverses a cause.
Quiz
1.Two machines' clocks are up to 10 ms apart and messages take 1 to 10 ms. In the lab, what share of messages were stamped as received before they were sent?
2.A node whose counter is 3 receives a message stamped 7. What should its Lamport counter become?
3.At 50 nodes, 91,458 pairs of events were independent. What did a Lamport clock do with them, and what did a vector clock do?
Check your understanding
Try each question before opening its answer.
Why did the lab's Lamport clock never reverse a cause and its effect, even at 100 ms of skew?
Because the Lamport rule never reads the wall clock. Each event adds one, and a receive jumps the counter to one more than the larger of its own value and the sender's stamp. So a cause always has a smaller number than its effect, whatever the wall clocks say.
At 100 ms of skew only 0.64% of causal pairs were reversed. Why is that figure misleading?
Most causal pairs in the sample were seconds apart, so a skew of 100 ms rarely crossed the gap between them. Among the 5,989 causal pairs less than 200 ms apart, 1,064 (18%) were reversed. Those close pairs are the ones a system usually has to order.
A vector clock flagged every independent pair correctly. Why not use vector clocks everywhere?
The vector is carried on every message and grows with the number of nodes that have written. The lab measured a mean of 32 bytes at 3 nodes and 357 bytes at 50, with a largest of 540, against 8 bytes for a Lamport number. You pay for the list only when you need to know two events were independent, for example to detect a conflicting write.
A hybrid logical clock and a Lamport clock both reversed 0 causal pairs. What does the HLC add?
It keeps its time within the clock skew of true time: at 100 ms of skew, the HLC time stayed within 100 ms of the real time, and its counter never passed 17. A Lamport number has no time in it at all, so the HLC is the one you can show to a person as a time while keeping the cause-before-effect guarantee.
Next up: Chapter 55, clocks that decide things: last-write-wins on skewed clocks, and a lease that has to expire on time.