0%
Distributed Systems: Consensus, Clocks, and Replication
Foundations
Replication
Consistency Models
Consensus
Transactions and Operations
Lamport Clocks and Happens Before
The previous lesson showed that physical clocks cannot reliably order events across machines. In 1978, Leslie Lamport published a paper called Time, Clocks, and the Ordering of Events in a Distributed System that changed how the field thinks about time. His insight was that for most purposes, systems do not need to know when events happened. They need to know which events could have influenced which others. That relationship, called happens before, can be tracked exactly with simple counters and no physical clocks at all. This lesson develops the relation, the Lamport clock that captures it, and the many places it appears in real systems.
Events, processes and messages
Lamport modeled a distributed system as a set of processes, each performing a sequence of events. An event can be a local step, such as updating a variable, sending a message or receiving a message. Within one process, events happen in a definite order, the order in which the process executes them. Between processes, the only connection is messages: a process can learn about events on another process only by receiving a message that was sent after those events.
This model is deliberately minimal. It does not mention clocks, network delay or wall time. It captures only what information could have flowed where, which turns out to be exactly what matters for correctness in many algorithms. A database replica deciding whether a write supersedes another, a debugger reconstructing why a request failed and a consensus protocol deciding which leader is newer all depend on the same question: could this event have known about that one?
Defining happens before
Lamport defined the happens before relation, written with an arrow, by three rules. First, if a and b are events in the same process and a comes before b in that process, then a happens before b. Second, if a is the sending of a message and b is the receipt of that same message, then a happens before b. Third, the relation is transitive: if a happens before b and b happens before c, then a happens before c.
That is the whole definition. It says that a happens before b exactly when there is a path from a to b that moves forward within processes and along messages. If such a path exists, information from a could have reached b, so a could have influenced b. If no path exists in either direction, the two events are concurrent: neither could have known about the other, no matter what the wall clocks said when they happened.
Concurrency is about information, not time
The word concurrent in this sense often surprises people. Two events can be concurrent even if, by a perfect wall clock, one happened an hour before the other, as long as no message chain connected them. Conversely, two events a microsecond apart are ordered if a message connected them. Concurrency here means causal independence, not simultaneity. This is the right notion for distributed systems because it reflects what each process could actually know. If two clients updated the same record and neither had seen the other update, the updates are concurrent and represent a genuine conflict, regardless of which one the wall clock says came first.
A partial order
Happens before is a partial order. It orders some pairs of events and leaves others unordered. Within one process, it is a total order. Across processes, many pairs are concurrent. The relation is irreflexive, since no event happens before itself, and it can have no cycles, since messages cannot be received before they are sent. Every possible execution of a distributed system defines such a partial order, and many correctness properties are statements about it. For example, causal consistency, covered in a later lesson, requires that if a write happens before another write, every process sees them in that order.
Causality and influence
Happens before is sometimes called potential causality. If a happens before b, then a might have caused b: the process that performed b could have known about a and acted on it. It does not mean a actually influenced b. A process can receive a message and ignore its contents. But if a does not happen before b, then a certainly did not influence b, because no information from a could have reached b. Algorithms that need to respect causality track this potential influence conservatively, ordering every event after everything that might have caused it.
Why this matters in practice
Many real bugs are violations of happens before. A user sees a reply before the post it answers. A cache serves a value that predates the write the user just made. A replica applies a delete before the insert it was meant to remove. A monitoring system shows an alert resolving before it fired. In each case, the system ordered events by something other than causality, often by physical timestamps or arrival order, and the result contradicted what the participants knew. Tracking happens before explicitly, with logical clocks, prevents this whole class of anomaly.
Space time diagrams
Lamport drew executions as space time diagrams: one horizontal line per process, time flowing to the right, events as dots and messages as arrows between lines. These diagrams are the standard way to reason about distributed executions, and they appear throughout this course. To find whether a happens before b, trace forward from a along process lines and message arrows. If you can reach b, the answer is yes. If you cannot reach b from a, and cannot reach a from b, the events are concurrent. Sketching such a diagram is often the fastest way to find a race condition in a protocol design.
Cuts and consistent snapshots
A cut through a space time diagram divides events into those before the cut and those after it, one boundary point per process. A cut is consistent if, whenever it includes the receipt of a message, it also includes the sending of that message. In other words, a consistent cut never shows an effect without its cause. Consistent cuts correspond to global states the system could actually have been in, which makes them the basis of distributed snapshots and checkpoints. The Chandy Lamport snapshot algorithm, published in 1985, records a consistent cut of a running system by sending marker messages along every channel, without stopping the system. Stream processors such as Apache Flink use a variant of this algorithm for their checkpoints.
A small exercise
Picture two processes. P1 performs event a, then sends message m to P2. P2 performs event b before m arrives, then receives m as event c, then performs event d. Which pairs are ordered? Event a happens before c and d, because the path runs from a along P1 to the send, along m to c and along P2 to d. Event b happens before c and d, because they follow it on the same process. Events a and b are concurrent: no path runs from a to b, since m arrived after b, and none runs from b to a, since P2 sent nothing to P1. Working small examples like this by hand is the quickest way to make the definition feel natural, and it is the same reasoning a debugger applies to a real trace.
Drawing the diagram before writing code pays off.
An event happens before another when a chain of process steps and messages connects them, and events with no such chain are concurrent, whatever the wall clocks say. Happens before is a partial order capturing potential causality, and consistent snapshots are cuts that never include an effect without its cause.