Lab · Puzzle

Clock skew

Three servers, one bank account, and clocks that disagree by a few hundred milliseconds. Sort the history by timestamp and the account goes overdrawn. Put it right by hand, then work through the clocks distributed systems use instead.

Level 1 of 4

Sorted by timestamp

Three servers handle one account. Support pulled the history and sorted it by timestamp, and it shows the account going $80 overdrawn. B's clock runs 400 ms slow and C's 250 ms fast, and nobody knew.

GoalReorder the events into a history that could really have happened.

ABCa1a2c1b1b2c2a3a4
Positioned by each server's own clock. A red arrow is a message that arrived before it was sent.

Historytap one event, then tap where it goes

Impossible history

  • The balance goes negative, which the bank never allowed.
  • b1 receives a message before a2 sent it.
  • a3 receives a message before c2 sent it.

Why wall clocks don't order events

Every server's clock drifts. NTP pulls them back towards the right time, but typically only to within a few milliseconds on a good network and much worse on a bad one. A clock can also jump, backwards included, when it is corrected. So when two servers stamp events a few milliseconds apart, the stamps say nothing reliable about which came first. Sorting a merged log by timestamp, as level 1 does, can put a reply before its question and a withdrawal before the deposit that paid for it.

What you can rely on is causality. A server does its own events in order, and a message is received after it is sent. Lamport called the order those two rules imply "happens-before". It is a partial order: some pairs of events have no order at all, because neither could have known about the other.

Lamport clocks

A Lamport clock is a counter per server, bumped on every event and carried on every message. On receive, the server jumps past the larger of its own counter and the message's. That gives one guarantee: if a happened before b, then L(a) < L(b). Sorting by counter, with the server id to break ties, gives a total order every server agrees on, which is enough for things like a distributed mutual-exclusion queue. The converse doesn't hold. A smaller counter doesn't mean the event came first, so Lamport clocks can't tell you that two writes conflicted.

Vector clocks

A vector clock keeps one counter per server and merges the whole vector on receive. Now the guarantee runs both ways: a happened before b exactly when a's vector is less than or equal to b's in every position and smaller in at least one. If each is larger somewhere, the events were concurrent. Dynamo-style stores use this, or its cousin the version vector, to keep both sides of a conflicting write and hand them back to the application, rather than letting the later timestamp silently win. The cost is size: one entry per participant.

Hybrid logical clocks

A hybrid logical clock is a pair (l, c). l is the largest physical time the server has seen, from its own clock or from any message, and c counts events that happened while l stood still. It keeps the Lamport guarantee, it fits in 64 bits, and l is never further from real time than the worst clock skew in the system. So the timestamps can be compared across nodes and still read like wall-clock time. CockroachDB and YugabyteDB use HLCs to order transactions, alongside an upper bound on skew. A node whose clock drifts past that bound is taken out of the cluster rather than trusted.

None of these clocks makes skew go away. They decide which question you can ask. "Could this have caused that?" needs vector clocks. "Give me an order everyone agrees on" needs Lamport clocks or HLCs. "What happened at 3:02 pm?" needs real clocks and an honest error bar, like Spanner's TrueTime.