Time, Clocks, and the Ordering of Events in a Distributed System
1978 · Communications of the ACM · ~11 min read
Read the original paperDefines “happened before” from messages instead of clocks and builds a counter that respects it. Read it for the state-machine idea in its second half, which every replicated database still uses.
Lamport’s 1978 paper gives us a way to put events on different machines into an order without trusting any machine’s clock. That sounds like a modest result. It is the reason a replicated database can promise that every copy applies the same writes in the same sequence, and the reason we can say precisely when two updates conflict.
Start with the situation the paper was written against. A service runs on three processes, P1, P2 and P3, and each one writes a log line with its own wall-clock time. P1 sends a request to P2 and logs it at 10:00:05. P2 logs the arrival at 10:00:02, because its clock runs a few seconds behind P1’s. Anyone who later merges the logs and sorts by timestamp sees a reply to a request that had not yet been sent. Nothing is broken. Quartz clocks drift, synchronisation is approximate, and the message took 40 milliseconds while the clocks disagree by three seconds.
A debugging log can survive this. A system that uses timestamps to decide which write wins, or who gets a lock, cannot. Lamport’s question was what “before” can mean when no clock is trustworthy, and his answer was that the system already records the order that matters, in the messages it sends.
What “before” can mean when clocks disagree
Two facts about any distributed system are beyond dispute. Within one process, events happen in the order the process performed them. And a message is sent before it is received. Lamport defines the relation happened-before, written →, as exactly these two facts plus chaining: if a → b and b → c, then a → c. Nothing else goes in. No clocks, no durations.
Before our own example, here is the smallest picture of the idea. In the figure, a message from P1 to P2 and another from P2 to P3 chain three events together. A fourth event on P1, later by any clock, touches neither message.
Now the example we will carry through this page, with events named by process. P1 does a local step a1, then sends a message to P2 (the send is a2). P2 has already done a local step b1; it receives P1’s message as b2, then sends to P3 as b3. Meanwhile P3 does two local steps of its own, c1 and c2, before receiving P2’s message as c3.
Following the arrows, a1 → a2 → b2 → b3 → c3. Also c1 → c2 → c3, because they are earlier steps on P3. But no chain of messages connects c1 to a1 or a2 in either direction. Lamport calls such events concurrent. The word does not mean they happened at the same instant. It means neither could have influenced the other, so the system has no basis for ordering them and no correct program can depend on an order between them.
This is a partial order: some pairs are ordered, some are not. Lamport borrowed the framing from special relativity, where two events outside each other’s light cones have no observer-independent order. The common misreading is that concurrency is a gap in our knowledge that better clocks would fill. It is not. Even a perfect clock would tell us only that c1 occurred at some instant, and that fact would not change what P1 or P3 could have known.
A counter that never contradicts the messages
Having defined the order, the paper asks for numbers that respect it. A logical clock is a counter in each process, and the paper’s requirement, the clock condition, is one direction only: if a → b then C(a) < C(b). Two implementation rules are enough to guarantee it.
on any local event or send:
C = C + 1
(a send carries the new C in the message)
on receive of a message carrying T:
C = max(C, T) + 1Run our example through those rules. P1’s local step a1 gets 1 and the send a2 gets 2, so the message carries 2. On P2, local step b1 gets 1. When the message arrives, P2 computes max(1, 2) + 1 = 3, so b2 gets 3, and the send b3 gets 4. On P3, c1 gets 1 and c2 gets 2. The receive c3 computes max(2, 4) + 1 = 5.
| event | process | what happens | Lamport time |
|---|---|---|---|
| a1 | P1 | local step | 1 |
| a2 | P1 | send to P2, carries 2 | 2 |
| b1 | P2 | local step | 1 |
| b2 | P2 | receive: max(1, 2) + 1 | 3 |
| b3 | P2 | send to P3, carries 4 | 4 |
| c1 | P3 | local step | 1 |
| c2 | P3 | local step | 2 |
| c3 | P3 | receive: max(2, 4) + 1 | 5 |
Every arrow in the example goes from a smaller number to a larger one, which is all the clock condition asks. The receive rule is what makes it hold: by jumping past the sender’s value, the receiver makes sure everything it does from now on is numbered after everything the sender had done when it sent.
In the table, c1 on P3 has Lamport time 1 and a2 on P1 has Lamport time 2. Did c1 happen before a2?
Show the worked answer
No. There is no chain of messages from c1 to a2 or from a2 to c1, so they are concurrent. The smaller number tells us only that c1 did not happen after a2. If P3 had done ten local steps before c1, it would carry 11 and the comparison would flip, while the causal facts stayed exactly the same.
What a smaller timestamp does not tell you
The clock condition runs one way. If a → b, then C(a) < C(b). The converse is false, and reading it as true is the most frequent mistake made with this paper. A Lamport timestamp folds a partial order into a single line of integers, and a single line cannot represent “these two are unordered”. Concurrent events get numbers anyway, and the numbers compare one way or the other by accident.
Change one variable to see it. Suppose P3 is busy and performs ten local steps before c1 and c2. Now c1 is 11, c2 is 12, and the receive c3 becomes max(12, 4) + 1 = 13. Nothing about who could have influenced whom has changed, yet c1 now looks “later” than everything P1 and P2 did. A system that used these numbers to pick a winning write would now prefer P3’s value for no reason other than P3’s workload.
Telling concurrency apart from order needs more state. Vector clocks, introduced independently by Colin Fidge and Friedemann Mattern in 1988, keep one counter per process and compare element by element. If neither vector is greater than or equal to the other in every position, the events are concurrent. The cost is size: a vector grows with the number of processes that have touched the data. Dynamo, later in this set, pays that cost because detecting concurrent writes is the point of its design. The simulation below computes both clocks, shows you only the Lamport value, and reveals the vector verdict when you pick two events.
Add local events and messages, then click any two events. The number on each event is its Lamport clock; the verdict below uses the true happens-before relation.
Click two events to compare them. Each is labelled with its Lamport clock: a local step adds one, and a message carries its sender's clock so the receiver jumps past it.
From a partial order to one queue everybody agrees on
The second half of the paper is easy to skip and should not be. Lamport breaks ties between equal timestamps using process numbers: order first by Lamport time, then by process id. That turns the partial order into a total order that every process can compute from the same information and get the same answer. The total order is somewhat arbitrary for concurrent events, and that is fine, because for concurrent events any order is consistent with causality.
He then uses the total order to solve a real problem: mutual exclusion over a shared resource with no central lock server. Each process keeps a queue of requests. To ask for the resource, a process timestamps a request, adds it to its own queue and sends it to everyone. A receiver adds the request to its queue and replies with a timestamped acknowledgement. To release, the holder removes its request and sends a release message. A process may take the resource when its own request is first in its queue by the total order, and it has received some message from every other process with a timestamp later than its request.
Walk it through with three processes. P2 requests at time 2. Before hearing about it, P1 requests at time 3. Both broadcast. Once the messages land, every queue holds the same two entries in the same order, 2:P2 then 3:P1. P2’s request is first, and P2 has heard from P1 (its request carried 3) and from P3 (an acknowledgement stamped above 2), so P2 enters. P1 sees that its request is second and waits. When P2 sends its release, every process drops 2:P2, P1’s request becomes first, and P1 enters. No process ever asked a coordinator. They each ran the same rule over the same queue.
The second condition is the subtle one. Waiting until it has heard something later from every process is how P1 knows that no earlier request is still in flight. Lamport assumes messages between two processes arrive in the order they were sent, so a later message from P3 means anything P3 sent earlier has already arrived.
Change one variable here too. Suppose P1 had not been slow, and both requests carried timestamp 3. The tie-break decides: 3:P1 sorts before 3:P2 because 1 is less than 2, so P1 goes first. Is that fair? Not in any human sense, since P2 may have asked earlier by the wall clock. But the two requests were concurrent, so no process could have known which came first, and the only requirement is that everyone picks the same winner. Every process applies the same comparison to the same pair of numbers, so they do.
Where the algorithm breaks
Now make one process fail. P3 crashes just after P2 broadcasts its request and before it acknowledges. P2’s request is first in every queue, but P2 cannot enter, because it has not heard anything from P3 stamped later than 2. P1 cannot enter either, since its request sits behind P2’s. The whole system stops because one member went quiet. Lamport says plainly that his algorithm assumes no failures and that handling them is a hard problem. The fix arrived in his own Paxos work about a decade later: wait for a majority instead of everyone, and add a protocol for agreeing on the order when the coordinator changes. That fix costs extra message rounds and a great deal of subtlety, which is the story of the Raft paper.
The paper also names a failure that logical clocks cannot fix on their own, in a section titled “Anomalous Behavior”. A user issues request A on P1, then phones a colleague, who issues request B on P3. In the real world A came first. Inside the system no message connects them, so they are concurrent and B may well get the smaller timestamp. Lamport gives two remedies. Either carry the ordering into the system (the user passes A’s timestamp along so B is stamped after it), or use physical clocks synchronised tightly enough that an event outside the system cannot outrun them. He then proves a bound on how far apart physical clocks drift given a maximum drift rate and message delay. Thirty-four years later, Spanner’s TrueTime is an engineering answer to that same section.
The reasoning in the physical clocks section is worth having in your head, because it recurs. If clocks can disagree by at most some amount, and no message (or phone call) can get from one machine to another faster than that amount, then nothing sent can arrive stamped earlier than it left. Lamport adjusts each clock forward on every received message, much like the logical clock rule, and shows that clocks with a small drift rate stay within a computable bound of each other. Our opening example broke this condition: the clocks disagreed by three seconds and the message took 40 milliseconds. Tighten the clocks to within a few milliseconds and the reply could no longer appear before the request. That is the trade Spanner later makes, paying for very tight clocks so that physical timestamps can be trusted to respect order.
| clock | what a comparison tells you | cost | used by |
|---|---|---|---|
| wall clock | roughly when, if clocks are well synchronised; nothing reliable about causality | free, but wrong by the skew | logs, last-write-wins stores |
| Lamport clock | if a → b, then C(a) < C(b); a smaller value does not prove order | one integer per message | log sequence numbers, total ordering, Raft terms |
| vector clock | exactly whether a → b, b → a, or concurrent | one counter per participant | Dynamo, Riak, causal stores |
| hybrid logical clock | Lamport guarantee, plus values close to wall time | one timestamp, needs bounded skew | CockroachDB, YugabyteDB |
| bounded interval (TrueTime) | real-time order when intervals do not overlap | GPS and atomic clocks, plus waiting | Spanner |
Lamport has written on his publications page that the idea came from reading a paper on replicated databases whose timestamp scheme could order events in a way that violated causality, and that most readers remember the clocks while missing the state-machine idea he considered the real contribution. Read with that in mind, the paper is less about time than about how independent processes can agree on a sequence.
How to read the original
The paper is short, about eight pages, and needs no background beyond comfort with a little notation. Read the sections on the partial ordering and on logical clocks closely, and draw the space-time diagrams yourself with three processes before you read his. Then read the total ordering section slowly. The mutual exclusion algorithm is five rules; trace them on paper with two requests, and then with one crashed process, as we did above. The anomalous behaviour section is a page and is worth more than its length suggests. The physical clocks section and the appendix proof can be skimmed on a first pass unless you care about the bound itself. There is no evaluation section; the argument is the result.
The next paper, MapReduce, comes from the opposite end of the field. It takes ordering and coordination as solved problems for one master and asks how to run one program across thousands of unreliable machines without the programmer thinking about any of this.
The original. Read it after this page; the explainer above is a map, not a substitute.
A clear lecture on happens-before, Lamport clocks and vector clocks, with the same examples drawn out step by step.
His publication page has a short note on how the idea came about and why he thinks people cite it for the wrong reason.