Skip to content

Physical clocks and clock skew

On one machine, “which came first” has an answer: the machine’s clock. In a distributed system every machine has its own clock, and the clocks are kept close by exchanging messages over a network whose delays vary. They are never exactly equal. The difference between one clock and true time is its skew.

For measuring how long something took on one machine, a small skew does not matter. It matters when timestamps from two machines are compared to decide an order: which write is newer, which event happened first. Lamport’s paper starts from this problem. He observes that in a distributed system it is sometimes impossible to say that one of two events occurred first, and that “happened before” is therefore only a partial ordering.

Mental model. Three people in different cities each note the time on their own watch when something happens, and mail each other letters. If one watch runs fifty minutes slow, its owner’s notes put the reply to a letter before the letter was sent.

Three processes exchange messages. P1 publishes a post and sends a message to P2. P3 does something of its own. P2 receives the message, replies to the post, and tells P3, who receives it. Each message takes 18 ms in true time. Each process also stamps its events with its own clock. P1 and P3 are exact, and P2’s clock is set by Clock of P2 compared with true time, which starts at -50 ms.

Three processes exchange messages. Skew one clock, and compare what physical timestamps, Lamport clocks and vector clocks say about which event came first.

-50 ms
60 ms
P1P2P311 [1, 0, 0]22 [2, 0, 0]31 [0, 0, 1]43 [2, 1, 0]54 [2, 2, 0]65 [2, 3, 0]76 [2, 3, 2]

Each dot is an event, numbered in true-time order. Next to it: its Lamport clock, then its vector clock. An arrow is a message.

P3 receives a message from P2. Its own clock reads 60 ms, Lamport 6, vector [2, 3, 2].

Results of the run

Pairs of events where one caused the other
16
Of those, put in the wrong order by physical clocks
6
Of those, put in the wrong order by Lamport clocks
0
Pairs of concurrent events
5
Concurrent pairs that Lamport clocks still put in order
4
Physical clocks put the reply before the post
Yes
Events drawn so far, with the three kinds of timestamp
ProcessEventTrue timeOwn clockLamportVector
P1publishes a post10 ms10 ms1[1, 0, 0]
P1sends a message to P212 ms12 ms2[2, 0, 0]
P3does something of its own20 ms20 ms1[0, 0, 1]
P2receives a message from P130 ms-20 ms3[2, 1, 0]
P2replies to the post40 ms-10 ms4[2, 2, 0]
P2sends a message to P342 ms-8 ms5[2, 3, 0]
P3receives a message from P260 ms60 ms6[2, 3, 2]
Every event in words
Every event of the run, in time order
At secondEvent
10 msP1 publishes a post. Its own clock reads 10 ms, Lamport 1, vector [1, 0, 0].
12 msP1 sends a message to P2. Its own clock reads 12 ms, Lamport 2, vector [2, 0, 0].
20 msP3 does something of its own. Its own clock reads 20 ms, Lamport 1, vector [0, 0, 1].
30 msP2 receives a message from P1. Its own clock reads -20 ms, Lamport 3, vector [2, 1, 0].
40 msP2 replies to the post. Its own clock reads -10 ms, Lamport 4, vector [2, 2, 0].
42 msP2 sends a message to P3. Its own clock reads -8 ms, Lamport 5, vector [2, 3, 0].
60 msP3 receives a message from P2. Its own clock reads 60 ms, Lamport 6, vector [2, 3, 2].

Illustrative values, not benchmarks of any real product.

Model notes

Assumptions

  • P1 publishes a post and tells P2. P2 replies. P3 does something of its own, then hears from P2. The reply is caused by the post.
  • Every message takes 18 ms. The clocks of P1 and P3 are exact, and only the clock of P2 is off, by the amount you choose.

Simplifications

  • A Lamport clock numbers events so that a cause always has a smaller number than its effect. The reverse does not hold: a smaller number does not mean a cause.
  • The vector clocks here have one slot per process, three in all. A system with many clients needs ways to keep them small.

Parameter values are illustrative. They are not benchmarks of any real product.

The post is stamped 10 ms by P1. The reply happens 40 ms in true time, 30 ms after the post, but P2’s clock is 50 ms behind and stamps it -10 ms. Sorted by own clock, the reply comes before the post it answers. The lab shows it as Physical clocks put the reply before the post: Yes.

The lab counts the pairs of events where one caused the other: there are 16. At the starting skew, the physical clocks put 6 of those pairs in the wrong order. Move the slider and watch:

  • With P2’s clock exact, or only 10 ms behind, no pair is misordered. The 18 ms a message takes is a margin: a skew smaller than the delay cannot reverse a cause and its effect.
  • At -20 ms, past the margin, 2 pairs are misordered.
  • A clock that runs ahead breaks other pairs. At +50 ms, 3 pairs are misordered: P2’s three events before P3 hears from it are stamped later than P3’s receipt of that message.

The size that matters depends on how close in time causally related events are, and that is exactly what a busy system cannot control.

What happens if the newest timestamp wins?

Section titled “What happens if the newest timestamp wins?”

Some systems resolve a conflict between two writes to the same key by keeping the one with the larger timestamp. Dynamo’s authors call this last write wins and say that the object with the largest physical timestamp value is chosen. Suppose the post and the reply were such writes. At the starting skew, the post carries 10 ms and the reply -10 ms. The reply was caused by the post and is the later one in true time, yet it loses. Nothing flags it. The writer was told the write succeeded, and it is gone.

This is the same loss as in the previous lesson, where last write wins dropped the addition of 5. There the cause was a partition, here the cause is a clock. Both are silent.

The way out is to stop treating timestamps as truth in one of two ways. Spanner’s TrueTime exposes the uncertainty: it returns an interval that is guaranteed to contain the true time of the call, unlike standard time interfaces that give clients no notion of uncertainty. The Spanner paper says that if the uncertainty is large, Spanner slows down to wait it out, and that its implementation keeps uncertainty small, generally less than 10 ms, using GPS and atomic clocks. That is a price paid in hardware and in waiting. The other way is to order events by something that does not depend on any clock, which is the next lesson.

  • Each machine has its own clock, and the clocks differ by their skew. Timestamps from two machines cannot be compared as if they came from one.
  • In the lab, a clock 50 ms behind stamps a reply at -10 ms, before the 10 ms post that caused it, and 6 of the 16 cause-and-effect pairs are misordered. At 0 or -10 ms none is, because a message takes 18 ms.
  • Last write wins keeps the larger timestamp, so a skewed clock can make the wrong write win without any error.
  • Spanner exposes clock uncertainty as an interval, and waits it out. Another way is to order by cause and effect.

Next: Logical clocks: Lamport and vector clocks.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.