A simple counter rule that gives every event an order that respects cause and effect.
A Lamport timestamp is the simplest logical clock: each machine keeps one counter, adds 1 for every event, and sends the counter in every message. A receiver jumps ahead of the number it received.
Leslie Lamport introduced this in 1978, in his paper "Time, Clocks, and the Ordering of Events in a Distributed System". The goal is a number for each event, so that if A happened-before B, then the number for A is less than the number for B. This is called the clock condition.
The algorithm has two rules. Rule 1: before each event on a machine (including sending), increase its counter by 1, and give the event that value. Rule 2: a message carries the sender's counter. When a machine receives one, it sets its counter to the larger of its own value and the message value, then adds 1.
Rule 2 is the important one. It makes the receive always look later than the send, even if the receiver had a lower counter before. Without the max step, a receiver with a small counter would stamp the receive with a number smaller than the send, and the effect would look older than its cause.
| When | What the machine does | Example |
|---|---|---|
| Local event | counter = counter + 1, event gets the value | Counter 3, event is 4 |
| Send a message | counter = counter + 1, message carries the value | Counter 4, sends 5 |
| Receive a message | counter = max(counter, message) + 1 | Counter 2, message 5, new counter 6 |
Two events on different machines can get the same number. To make every pair comparable, sort by (timestamp, machine ID). The result is arbitrary for concurrent events, but identical on every machine.
Each step is one local event, or one message that is sent and received straight away. Lamport timestamps never go backwards, but a smaller number does not prove that an event really happened first: two events with different numbers may be unrelated.
on_event: c += 1 ยท on_send: c += 1; msg.ts = c ยท on_receive: c = max(c, msg.ts) + 1| Term | Meaning in easy words |
|---|---|
| Lamport timestamp | A number from a per-machine counter that respects cause and effect |
| Counter | An integer each machine increases on every event |
| Clock condition | If A happened-before B then L(A) < L(B) |
| Happened-before | A โ B means A could have influenced B |
| Concurrent | Neither event could have influenced the other |
| Total order | A way to order every pair of events |
| Tie-break | A fixed rule (such as machine ID) used when two numbers are equal |
| max rule | On receive, set the counter to max(own, message) and then add 1 |
| Converse | The opposite claim. L(A) < L(B) does not imply A โ B |
| Lamport mutual exclusion | A lock built by ordering requests by timestamp |
| Fencing token | A growing number given with a lock, so storage rejects old holders |
| Choice | We gain | We lose |
|---|---|---|
| Lamport timestamp | Tiny, simple, total order | No concurrency detection |
| Vector clock | Detects concurrency exactly | Size grows with machines |
| Tie-break by machine ID | Same order everywhere | Some bias toward certain machines |
| Persisting the counter | Safe restarts | A write on each tick (or batch it) |
| Wall-clock timestamp | Real time meaning | Not safe for order |