System design paper · 1978
Lamport clocks
How Lamport showed that machines with no shared clock can still agree on an order of events, using a simple counter, and where real clocks are still needed.
You’ll learn to
- Explain why “which happened first?” often has no answer in a system whose parts only talk by messages.
- Apply the counter rule to number events and break ties to get one order every machine agrees on.
- Trace how machines take turns on a shared resource using only stamped messages.
- Say when counters give an order that surprises a person, and what synchronised physical clocks add.
The idea in short
Computers in different places have no shared clock, and the clocks they do have drift apart. So “which of these two things happened first?” often cannot be answered by looking at the time. Leslie Lamport asked a better question: could one event have influenced the other? If a message travelled from the first to the second, yes. If nothing links them, nobody can say which came first, and the system does not need to.
He then gave every machine a counter. Add one for each event. When a message arrives, jump past the number it carries. Sort all events by number, and when two events get the same number, let the machine name decide which goes first. Every machine builds the same single order, one that never puts an effect before its cause. The paper uses this to let machines take turns without a boss, and ends by showing when real clocks are still needed.
The problem
Picture an airline booking system. The rule is easy to say: give the last seat to whoever asked first. Lamport mentions a reservation system in passing; the seat scenario here is this guide’s own. On one computer it is easy to follow, because one clock stamps every request. Now run the system on machines in different cities, each with its own clock, and “whoever asked first” stops being easy.
A is a set of separate programs, called processes, that run in different places and talk only by sending messages. Lamport gave a precise test: a system is distributed when a message takes a noticeable time compared with the gap between events inside one process. If that holds, a process hears about the others late, and by an unpredictable amount.
The obvious fix is to compare clock readings. Two things make that unreliable:
- : every clock runs slightly fast or slow. The paper notes that a typical crystal clock is off by no more than about one part in a million. That sounds tiny, but it is up to about 86 milliseconds a day (86,400 seconds × 0.000001 = 0.0864 seconds).
- : at one real instant, two clocks show different readings. Machines can be synchronised, but never perfectly.
Drift piles up into skew. Say two machines have not been synchronised for a day and now differ by 50 milliseconds (an illustrative figure). A message that takes 5 milliseconds to arrive can then be stamped by its receiver with an earlier time than the sender gave it, so the stamps are in the wrong order.
The paper also shows a trap that no clock fixes. Say process P1 asks a central scheduler, P0, for a resource. P1 then sends a message to P2. When it arrives, P2 sends its own request to P0. Because the network is unpredictable, P2’s request can reach P0 first. A scheduler that grants requests in arrival order now serves P2 before P1, though P2’s request was only made because of something P1 did earlier.
The question the paper answers: what does “before” mean when there is no shared clock, and can machines still agree on one order of events?