Logical Clock
Three machines, three clocks, all wrong by different amounts and drifting at different rates. Nothing here tries to correct them. Instead each machine keeps a counter that is not a clock at all — it measures no duration, it cannot be compared to a second, and it only ever goes up. The rule is one line: carry your counter on every message, and on receiving one, jump past what it says. That is enough to guarantee that nothing ever appears to arrive before it was sent. It is not enough to tell you what happened first, and this page shows you exactly where that fails rather than mentioning it.
New to time without clocks? Start here
Two machines' clocks disagree, and no amount of syncing removes it. So a timestamp cannot settle which of two events came first, and on a network that question is asked constantly.
What survives is a weaker relation that is still enough: if one event caused another, everyone must agree it came first. Events that could not have influenced each other need no order at all, and this page is about counting rather than timing.
A message that takes time and may not arrive
A message takes time to travel from one computer to another. Sending it does not establish that it arrived, and receiving one message does not establish that an earlier one arrived first.
Messages may be delayed, lost, duplicated or reordered. A protocol defines how the participants respond to those possibilities. Some machines here model only one of them; the page says which assumptions its result needs.
The machine for this idea on its own is Packet Switching, if you would rather press it than read about it.
Three wrong clocks, and one order all three agree on
1 Three machines, three counters, ticking at rates that have nothing to do with each other
Three machines, and three wall clocks that disagree at every instant. These numbers are displayed and never used to order anything.
| machine | clock reads | drifting at | out by |
|---|---|---|---|
| alpha | 1050 | 1.00x | — |
| bravo | 996 | 1.11x | -54 |
| charlie | 1121 | 0.92x | +71 |
At the same instant the three clocks are 125 apart, and because they tick at different rates the disagreements keep changing: the fast one here is behind and the slow one is ahead, so right now they are closing. Nothing below uses any of these numbers.
2 Every message carries its sender's counter, and the receiver jumps past it
The run. A local event adds one; a send adds one and carries the number; a receive takes the larger of the two and adds one.
1
| machine | what happened | rule | counter |
|---|---|---|---|
| alpha | reads a key | +1 | 1 |
| bravo | reads a key | +1 | 1 |
| alpha | asks bravo for the lock | +1, and carries 2 | 2 |
| charlie | reads a key | +1 | 1 |
| bravo | hears alpha's request | max(2 carried, own) + 1 | 3 |
| bravo | tells charlie | +1, and carries 4 | 4 |
| alpha | writes | +1 | 3 |
| charlie | hears bravo | max(4 carried, own) + 1 | 5 |
| charlie | answers alpha | +1, and carries 6 | 6 |
| charlie | writes | +1 | 7 |
| alpha | hears charlie | max(6 carried, own) + 1 | 7 |
| bravo | writes | +1 | 5 |
3 Now every message arrives later than it was sent, on the numbers, on every machine
The guarantee, over every message in the run: received at a higher number than it was sent, on every machine, without the machines agreeing on what time it is.
| message | sent at | received at | higher? |
|---|---|---|---|
| alpha → bravo | 2 | 3 | yes |
| bravo → charlie | 4 | 5 | yes |
| charlie → alpha | 6 | 7 | yes |
All 3 messages arrive at a higher number than they left, and no machine had to know what time it was anywhere else.
4 Break ties by machine and the three of them agree on one order, without a clock between them
And the limit. Break ties by machine name and every machine here computes the same order. That works because each of them can see every event, which a real system cannot: what the rule guarantees is that any two machines agree wherever their knowledge overlaps, not that anybody holds the whole list. It is an order, not the order: the pairs below are events that nothing connects, put in a sequence anyway.
| this | is ordered before | but actually |
|---|---|---|
| bravo reads a key (1) | alpha asks bravo for the lock (2) | neither caused the other |
| bravo reads a key (1) | alpha writes (3) | neither caused the other |
| charlie reads a key (1) | alpha asks bravo for the lock (2) | neither caused the other |
| charlie reads a key (1) | bravo hears alpha's request (3) | neither caused the other |
| charlie reads a key (1) | bravo tells charlie (4) | neither caused the other |
| charlie reads a key (1) | alpha writes (3) | neither caused the other |
| charlie reads a key (1) | bravo writes (5) | neither caused the other |
| alpha writes (3) | bravo tells charlie (4) | neither caused the other |
The first 8 of 15 pairs of events are ordered by their numbers while nothing connects them: no message runs between them in either direction. The order the three machines agree on is consistent, and it is not history.
These ran in this browser when the page loaded. Each claim, whether it held, and the number behind it.
| claim | held | measured |
|---|---|---|
| the run completes with every message delivered | yes | all 3 delivered |
| every one of 3 messages is received at a higher number than it was sent | yes | no message arrives before it was sent |
| and across all 45 ordered pairs, a cause always has the smaller number | yes | every one of them |
| a smaller number does NOT mean it happened first: 15 such pairs here | yes | unrelated events, ordered by number alone |
| one order over all 12 events, and no two of them compare equal, though 6 pairs share a number | yes | the tiebreak settles every one of them |
| the wall clocks are 125 apart at the same tick | yes | which is what the counters are for |
What is real here, and what is not
The counters are not clocks and the page never converts them to time
A Lamport counter has no unit. The difference between 4 and 7 is not three of anything — not seconds, not messages, not events on any one machine. It is only ever compared, never subtracted, and the page prints no elapsed time derived from one anywhere.
One fixed run, written down rather than generated
The twelve events are a script in the source, so the page and its tests are talking about the same run and the numbers below are reproducible. A randomly generated interleaving would demonstrate the same rule and would make every quantity on this page unquotable.
Messages here cannot be lost, delayed or reordered
Every send is delivered, and delivered in the order the script sets. That is not what a network does, and the difficulties that follow from it are most of why distributed systems are hard. The rule shown here survives all of that in reality; this page simply does not exercise it.
Vector clocks are what you need if you want the converse, and they are not here
The last panel is honest about the limit: a smaller counter does not mean it happened first. Recovering that requires a counter per machine rather than one number, which costs n numbers on every message and is a different machine. Saying Lamport clocks order events, full stop, is the mistake this panel exists to prevent.
Sources
- L. Lamport, Time, Clocks, and the Ordering of Events in a Distributed System, Communications of the ACM 21(7), July 1978, pages 558-565.
- F. Mattern, Virtual Time and Global States of Distributed Systems, 1989. Where the vector clock recovers the converse this page says is missing. Colin Fidge published the same construction independently in 1988, and this page names both rather than the one it happened to cite first.
- Logical Art, the studio this belongs to.