Time, Clocks, and the Ordering of Events in a Distributed System
"Before" made precise without a clock: a partial order built out of the messages a system already sends.
Read the original ↗This is the paper that made "before" a word you can use precisely about a distributed system. It is also the paper most often cited for a guarantee it explicitly says it cannot provide, and its more influential half is the one almost nobody remembers is in it - a claim Lamport has made himself, in writing, about his own most-cited paper.
The problem it was solving
Start with the ordinary meaning of the word before. On one computer it is easy: two things happened, one of them happened first, and the clock says which. Spread the same program across several computers that can only talk by sending each other messages, and that easy question stops having an answer. Each machine keeps its own clock and the clocks disagree. A message takes time to arrive. And nobody is standing outside watching all of it at once. This is the paper that noticed you can still answer a narrower and far more useful question without any clock at all, could this event possibly have influenced that one, and that the answer is already written in the messages the system sends. If you build systems today that is why a log sorted by timestamp quietly lies to you, why the same request can be applied twice in the wrong order, and why nearly every replicated store you use carries some counter that is not a time.
The setting is processes that communicate only by sending messages, where the delay of a message is not negligible compared with the time between events inside a single process. In that setting the ordinary question, did A happen before B, has no answer you can trust, because there is no shared clock to ask and no observer who saw both. The paper's opening move is to notice that our confidence about time comes from an assumption we never examine, a single stream of events with a common now, and that the assumption simply does not hold once the system is spread out.
The concrete failure it opens on is an ordering one, and it is mundane: a system in which requests must be served in the order they were made, where no participant can see the whole order. Any answer built by comparing wall-clock readings inherits whatever disagreement those clocks have, and in 1978 physical clocks disagreed by considerably more than a message took to arrive. So the paper refuses the question in that form and asks a different one, which turns out to be answerable without a clock at all: not when did this happen, but could this event have influenced that one.
One note on scope, because it explains most of what the paper does not contain. Throughout, messages are assumed to be delivered, delivered in order between any given pair of processes, and never lost, and processes are assumed not to fail. That is not an oversight, the paper states the assumptions and gives the reason the failure case is beyond it: without physical time there is no way to distinguish a failed process from one that is merely pausing between events, and a user only knows a system has crashed because they have been waiting too long. Twenty years of distributed systems research lives inside the gap that sentence names.
The paper's way of drawing all this is worth adopting before anything else in it. A space-time diagram, the picture in Figure 1, gives each process its own line, marks its events as dots along it, and draws an arrow from every send to the matching receipt. Once the picture exists the definition is almost forced: one event happened before another exactly when you can travel from the first to the second by moving forward along a process line and forward along message arrows. Every claim in the paper is then a statement about paths in that picture, and a clock, when it arrives two pages later, is only a way of numbering the picture such that the numbers never contradict the paths.
flowchart LR
subgraph P["Process P"]
p1["p1 sends m1"] --> p2["p2"]
end
subgraph Q["Process Q"]
q1["q1 gets m1"] --> q2["q2 sends m2"]
end
subgraph R["Process R"]
r1["r1, on its own"] --> r2["r2 gets m2"]
end
p1 -- "m1" --> q1
q2 -- "m2" --> r2Follow the arrows. p1 happened before r2 because a chain of messages joins them, so p1 could have influenced r2. Nothing joins r1 to p1 or p2 in either direction, so those pairs are concurrent, and no clock inside this system can order them.
What it actually claims
The paper is eight pages and does four things in order: define an ordering, build a clock that respects it, use the clock to solve a synchronisation problem, and then show that the solution generalises to any synchronisation problem at all. The last of those is the one worth the reread.
Two statements carry the whole correctness argument, and it helps to keep them apart. The Clock Condition is the requirement, if a happened before b then C(a) is less than C(b), and it decomposes into two cases, one for two events inside a single process and one for a send and its receipt. The implementation rules IR1 and IR2 exist for no other reason than to satisfy those two cases. The paper then checks its own work with a picture rather than an induction: draw a clock's tick lines across the space-time diagram, and the Clock Condition holds exactly when every message arrow crosses at least one tick line. A clock is a legal numbering of the drawing, and nothing more than that.
sequenceDiagram participant P as Process P participant Q as Process Q Note over P: counter 4 Note over Q: counter 1 P->>Q: request, stamped 5 Note over Q: 1 is not above 5,<br/>so jump to 6 Q->>P: reply, stamped 7 Note over P: 5 is not above 7,<br/>so jump to 8
The two rules in one exchange. A process bumps its counter for its own events, and on receiving a message it jumps its counter above the stamp it was handed. Q never sees P's counter otherwise, and no one is trying to agree on a time.
- Happened-before, defined without reference to a clock
- The relation is the smallest one satisfying three rules: within one process, events are ordered by that process's own sequence. The sending of a message happens before its receipt. And the relation is transitive. That is the whole definition, and it is built entirely out of the communication structure, no clock appears anywhere in it, which is the point. Two events are concurrent when neither happens before the other, and the paper's reading of the relation is causal: a happened before b means a can causally affect b, and concurrent means neither can affect the other.
- It is a partial order, and the paper's real thesis is that you should leave it that way
- The single most useful thing in the paper is the refusal to complete the order. Its closing paragraph states the position directly: in a distributed system it is important to realise that the order in which events occur is only a partial ordering, and that this idea is useful for understanding any multiprocess system. Everything afterwards in the field that goes wrong about time goes wrong by treating a partial order as though it were a total one that merely has not been measured yet.
- Logical clocks, in exactly two rules
- A clock here is just a counter held by each process. IR1: a process increments its counter between any two successive events. IR2: a message carries the sender's counter value as a timestamp, and on receipt the recipient sets its counter to something greater than or equal to its current value and greater than the timestamp received. Those two rules are the entire implementation, and they are enough to guarantee the Clock Condition: if a happened before b, then C(a) is less than C(b). There is no synchronisation protocol, no coordinator, and no assumption about how fast anything runs.
- The converse does not hold, and the paper says so immediately
- This is the sentence the field keeps forgetting, and it appears within a page of the clock's definition. The paper notes we cannot expect the converse condition to hold, because it would force any two concurrent events to occur at the same time. So a smaller timestamp is evidence of nothing on its own: C(a) < C(b) is consistent with a having caused b and equally consistent with the two being entirely unrelated, and a logical clock cannot separate those cases even in principle. All a Lamport timestamp can ever certify is the negative: that b did not happen before a.
- The total order, and how arbitrary it is
- To get a total order you break ties with any arbitrary fixed ordering of the processes. The paper is unusually clear that the result is arbitrary: different systems of clocks satisfying the Clock Condition yield different total orders, and any of them will do for the purpose. That combination: the total order is correct, consistent with causality, and meaningless as a statement about the world, is precisely the shape of thing engineers find hardest to hold, and it is why the total order gets misread as a timeline.
- The worked example: mutual exclusion by replicated request queue
- Every process keeps its own copy of a request queue and follows five rules: send a timestamped request to everyone and queue it locally, acknowledge requests received, send a release on finishing, delete the corresponding request on receiving a release, and grant yourself the resource when your own request is the earliest in your queue and you have received a later-timestamped message from every other process. The requirement it satisfies is the interesting part: requests are granted in the order they were made, where order means the arbitrary total order just constructed.
- What that algorithm costs, counted rather than asserted
- Count the messages before judging it. One process that wants the resource sends a request to each of the other N minus 1 processes, collects an acknowledgement from each, and later sends a release to each: three rounds of N minus 1 messages, so 3(N-1) messages per grant, growing with how many participants exist rather than with how much contention there is. The fifth rule is heavier still, because granting requires a later-timestamped message from every other process, so one slow participant delays every grant in the system and one stopped participant stops all of them. None of this is hidden. It is the reason the very next section throws the specific algorithm away and keeps the method.
- The half that mattered more: the state machine
- Having solved one problem, the paper generalises. Describe what you want as a state machine, have every process independently run that machine over the same totally-ordered sequence of commands, and every process arrives at the same state, which, the paper says, allows one to implement any desired form of multiprocess synchronisation in a distributed system. That is state machine replication, and it is the idea underneath every replicated log built since, including both consensus papers already on this shelf. Lamport also names its fatal condition in the same breath: the method requires the active participation of every process, so the failure of a single one halts the system.
- Anomalous behaviour, which is where the paper turns honest
- A person issues a request on one computer, telephones a friend, and the friend issues a request on another. Nothing inside the system connects the two events, so the total order may well put the second first, and both people can see that the answer is wrong. The paper names this anomalous behaviour and offers exactly two remedies: get the external ordering information into the system explicitly, or build clocks satisfying a Strong Clock Condition, which requires real physical clocks. The final section is the theorem about how closely independently-running physical clocks can be held together given bounded drift and bounded message delay, and the answer is that they must agree to within roughly the shortest possible message transit time. The Spanner discussion on this shelf is that bound, bought and paid for, thirty-four years later.
The one idea worth stealing
Order what your system can actually observe, not what you wish it could. "Before" is a claim about influence, and influence in a distributed system travels only in messages, so build the ordering out of the messages you already send rather than out of a clock you cannot trust.
The smallest usable version of that idea is a counter attached to a message that a resource refuses to go backwards on. That is a fencing token: the current lease holder carries a higher number than the zombie that lost its lease, and the resource rejects the lower one without knowing anything about time, timeouts, or who is still alive. The idempotency-and-fencing board in this repo is a logical clock doing a full day's work: one counter, one comparison, a whole class of stale writes rejected, and Kafka's per-partition offsets and producer sequence numbers are the same shape wearing different names.
The harder discipline to carry away is the one about meaning. A Lamport total order is arbitrary and correct. A wall-clock ordering of the same events is meaningful and wrong. Systems get into trouble by having the first and believing they have the second, sorting by timestamp and calling the result history, or resolving a conflict by taking the larger number and calling that the winner. When you write down an ordering, write down what it is a statement about, and what it is not.
The second thing worth stealing is the reduction, not the clock. The paper turns a synchronisation problem into two much easier ones: agree on a single sequence of commands, and make every replica a deterministic function of that sequence. Once you have seen the shape you find it everywhere: a partitioned log with consumers reading it in order, a service replaying its own events, a write-ahead log shipped to a follower and applied. It also tells you in advance where such a system breaks, which is always the non-deterministic command: feed two replicas the same log and one call to a random number generator, the wall clock, or an unordered map iteration, and they end in different states while every piece of the ordering machinery reports success.
What aged badly
Almost nothing in the paper is wrong. What aged badly is what people believe is in it, which by now is a distinct object from the paper itself.
- A logical clock cannot say two events were concurrent, which is why vector clocks exist
- The relation the paper defines has three outcomes: a before b, b before a, or concurrent, and the clock it builds can only certify one negative. Losing the concurrency case is exactly what makes a Lamport timestamp useless for detecting a conflict: two writes to the same key get two different numbers whether one caused the other or they were made simultaneously in complete ignorance of each other, and taking the larger one silently discards the other's information. Vector clocks: a decade later, and not in this paper, restore what is lost by carrying a counter per process, at the cost of metadata that grows with the number of writers. The Dynamo discussion on this shelf is what that cost looks like in production. The point here is only that the gap is real, and that the paper's own construction is the reason for it.
- The state-machine half is more influential than the clocks and is routinely skipped
- Ask what this paper is about and you will be told logical clocks. Lamport's own retrospective note on it says the opposite in unusually blunt terms: it is his most-cited paper, many computer scientists claim to have read it, he has rarely met anyone aware that it says anything about state machines, and people have insisted to him that there is nothing about state machines in it - to the point where he went back and reread his own paper to check. Two consequences get skipped along with the section. The determinism requirement is load-bearing: a replicated state machine fed a non-deterministic command diverges, which is the practical reason statement-based replication breaks on things like NOW() and RAND(). And the algorithm's need for every process to participate is the exact limitation that consensus, twenty years later, exists to remove.
- "Logical clocks solve time in distributed systems" is a misreading, and a very common one
- They solve an ordering question, and only for events connected by a chain of messages. A logical clock cannot tell you how long ago something happened, cannot expire a lease, cannot decide a node is dead, cannot answer whether something occurred in the last five minutes, and cannot order two unrelated events in a way that matches what someone outside the system observed. The paper's own anomalous-behaviour section is the demonstration, and the paper's own remedy is to reach for physical clocks. Citing it as a reason to stop worrying about wall-clock time inverts its conclusion.
- The algorithm in it is a teaching device, and a fragile one
- The mutual exclusion algorithm requires every process to know every other, requires messages between any pair to arrive and to arrive in order, requires no process ever to fail, and costs a message to every participant for every request. Nobody runs it. It is in the paper as the simplest example the author could think of, to demonstrate a method, Lamport says as much himself. This is worth being explicit about because the algorithm is frequently presented as the paper's contribution, and a reader who evaluates it as production engineering will conclude the paper is naive, when in fact it is being scrupulous about what it claims and where it stops.
- The physical-clock section was an afterthought, and it is the one that came back
- The theorem about synchronising real clocks: bounded drift, bounded message delay, a bound on how far apart independently-running clocks can be held, reads like an appendix, and by Lamport's own account it was: he added it to see what synchronisation his scheme provided for real-time clocks and was surprised how hard the proof turned out to be. It was treated as an appendix for decades, because you could not buy clocks good enough for it to matter. Then you could. A modern system that spends atomic clocks and satellite receivers on bounding its clock uncertainty, and then pays for the bound by waiting out the uncertainty on every commit, is buying the Strong Clock Condition at the price this section says it costs. The least-read part of the paper turned out to describe the only exit, and the exit turned out to be billed in milliseconds.
- What to take from it today
- Read it for the definition and the last two sections, and treat the algorithm in the middle as an illustration. The definition hands you a precise version of a word you argue about most weeks: before is a claim about influence, and influence travels only in messages. The anomalous-behaviour section tells you exactly when that is not enough, which is any time two parties can talk to each other outside your system, so an ordering you build has to be told about those channels or it will be visibly wrong to the people using it. The state-machine section gives you the standard shape for making several machines agree: fix one order, then make everything downstream a deterministic function of it. And if you take away a single habit, take the counter, when correctness depends on which of two things came first, send a number along with the thing instead of reading a clock at the far end.
Related on chalk
The same idea as a board, lab, video or lesson.
- BoardThe Hard Part, Timedrifting clocks and a single source of now, the problem the paper answers by not using a clock
- BoardWhen Machines Failpartial failure and unreliable clocks, the demons this paper assumes away
- BoardWho Holds the Penone writer at a time, which is the mutual-exclusion problem the paper's worked example solves
- BoardIdempotency and Fencinga rising token that rejects a zombie's write, a logical clock doing a day's work
- BoardThe Copy Clerksreplicas fed the same changes in the same order, which is the paper's second half
- BoardReplicationstatement-based replication breaking on NOW() and RAND(), the determinism a replicated state machine needs
- BoardPartitionsordering promised inside a partition and nowhere else: a partial order, shipped
- BoardNeither OT nor CRDTconcurrent edits that must converge, answered by putting one server in charge of the order
- LabWho holds the penthe split brain a fence prevents, which is one counter deciding who is stale
- LabThe copy clerksreplicas a beat behind, what a partial order looks like from a reader's chair
- LabThe two ledgers must agreetwo logs that must agree on one order, and the coordinator that makes them
- VideoConsensus: who holds the penone pen, five clerks, a fence, and the fence is a number
- VideoReplication: the copy clerksthe lag between copies, before anyone tries to timestamp it away
- VideoDistributed transactions: the two ledgers must agreethe crash that lands between two writes, which no clock resolves
- VideoThe money-movement state machine: a payment is not a row you updateone object as a chain of legal transitions. The paper's method is running that chain everywhere at once
- LessonScheduler 07: The Hard Part, TimeUses the board The Hard Part, Time
- LessonWhen Machines FailUses the board When Machines Fail
- LessonRaft 01: Leader ElectionUses the board Who Holds the Pen
- LessonRaft Capstone: EverythingUses the board Who Holds the Pen
- LessonWho holds the pen (consensus & Raft)Uses the board Who Holds the Pen
- LessonHow you know it holds: Jepsen, simulation and modelsUses the board Who Holds the Pen
- LessonScheduler 06: Idempotency and FencingUses the board Idempotency and Fencing
- LessonKV: quorumUses the board The Copy Clerks
- LessonThe copy clerks (database replication)Uses the board The Copy Clerks
- LessonWhat did the database promise? (consistency models)Uses the board The Copy Clerks
- LessonBackups and the day you need them (RPO, RTO, and the restore nobody tested)Uses the board The Copy Clerks
- LessonCompute on borrowed disks: disaggregated storageUses the board The Copy Clerks
- LessonReplicationUses the board Replication
- LessonA CRDT of your ownUses the board Replication
- LessonKafka 04: PartitionsUses the board Partitions
- LessonFigma, the case studyUses the board Neither OT nor CRDT
- LessonEveryone edits offline: CRDTs and local-first syncUses the board Neither OT nor CRDT
Further reading
Read the original for the full argument and its evidence.
- The paper (PDF) ↗Communications of the ACM, July 1978, eight pages. The first half is the clocks. The section on anomalous behaviour is where it turns honest, and the physical-clock section is the one to read last and slowly.
- Lamport's own note on it ↗His retrospective on his publications page: where the idea came from, why he thinks the interesting part is trivial, and his complaint that nobody notices the state machines.
- Virtual Time and Global States of Distributed Systems (PDF) ↗Later work, 1988: vector clocks, which restore the concurrency information a Lamport timestamp throws away, and the reason every conflict-detecting store carries one.