Order events by counting them instead of trusting the wall clock.
A logical clock orders events by cause and effect, not by the time of day. Instead of asking "what does my watch say?", it asks "could this event have influenced that one?"
In 1978 Leslie Lamport pointed out that in a distributed system we do not really need to know the exact time of events. We need to know which events could have affected which others. He called this the happened-before relation, written A → B.
There are three rules. One: if A and B are on the same machine and A ran first, then A → B. Two: if A sends a message and B receives it, then A → B (you cannot receive a message before it was sent). Three: if A → B and B → C, then A → C.
If neither A → B nor B → A is true, the events are concurrent. That does not mean they happened at the same instant. It means no chain of messages connects them, so neither could have influenced the other, and the system has no reason to put one first.
| Clock | What it keeps | What it can tell you |
|---|---|---|
| Lamport timestamp | One integer per machine | If A → B then ts(A) < ts(B). The reverse is not guaranteed. |
| Vector clock | One integer per machine, for every machine | Whether A → B, B → A, or the two are concurrent |
| Version vector | Like a vector clock, but counts updates to a data item | Whether two replicas of the data diverged |
| Hybrid logical clock (HLC) | Wall-clock time plus a small counter | A close to real time order that never goes backward |
| Sequence number or term | One counter from a single leader | A simple total order inside one stream |
| Kind of order | Meaning | Example |
|---|---|---|
| Partial order | Some pairs are ordered, some are not (concurrent) | Happened-before; vector clocks |
| Total order | Every pair can be ordered | Lamport timestamps with a tie-break by machine ID |
Click any circle to pick an event. Lines between servers are messages. Time flows left to right on each server.
Rules used: (1) events on one server are ordered by the order they happened there, (2) sending a message happens before receiving it, (3) if X is before Y and Y is before Z, then X is before Z. Anything left over is concurrent.
| Term | Meaning in easy words |
|---|---|
| Logical clock | A counter that orders events by cause and effect, not by time of day |
| Happened-before (→) | A relation that says one event could have influenced another |
| Concurrent | Neither event could have influenced the other |
| Causality | The link between a cause and its effect |
| Partial order | An order where some pairs of events cannot be compared |
| Total order | An order where every pair of events can be compared |
| Lamport timestamp | A single counter updated by max-plus-one on receive |
| Vector clock | A list of counters, one for each machine |
| Version vector | A vector that tracks updates to one piece of data across replicas |
| Hybrid logical clock | A clock that combines wall time and a counter |
| Tie-break | A fixed rule, such as machine ID, to order equal timestamps |
| Choice | We gain | We lose |
|---|---|---|
| Lamport clock | Tiny metadata, total order | Cannot tell concurrent from ordered |
| Vector clock | Exact causality and conflict detection | Size grows with the writer count |
| Hybrid logical clock | Order plus a time close to real | Needs reasonably synced wall clocks |
| Single sequencer | Simple, strict order | A bottleneck and a single point of failure |
| Wall clock | Human meaning | Wrong order when clocks differ |