← back to distributed systems

Time and Clocks

Lamport 1978 · Wikipedia · wpLamport timestamps

Physical clocks drift. Logical clocks don't measure wall time; they capture causality. If event A could have influenced event B, then A's timestamp is less than B's. Lamport clocks give a total order consistent with causality. Vector clocks give the exact causal partial order.

Physical clocks and drift

Every computer has a quartz oscillator. It drifts by roughly 10-100 ppm (parts per million). Over a day, that is 1-10 seconds of error. NTP corrects this but cannot eliminate it. Two nodes will never agree on the exact time.

Scheme

The happens-before relation

wpLamport defined the happens-before relation: A happens-before B (written A → B) if A and B are on the same node and A came first, or if A is a send and B is the corresponding receive. The relation is transitive. Events that are not related by happens-before are concurrent: neither caused the other.

P1 P2 P3 1 2 5 2 4 1 3 time

Lamport clocks

Each node keeps a counter. On each local event, increment. On send, increment and attach the counter. On receive, set your counter to max(yours, received) + 1. This gives a total order consistent with causality: if A → B then L(A) < L(B). But the converse is not true: L(A) < L(B) does not mean A caused B.

Scheme

Vector clocks

A vector clock is an array of counters, one per node. Node i increments its own entry on each event. On send, attach the whole vector. On receive, take the componentwise max, then increment your entry. Two events are concurrent if and only if neither vector dominates the other. Vector clocks capture the exact causal partial order.

Scheme
Neighbors

Cross-references

  • 🔑 Logic Ch.7 — partial orders: the mathematical structure behind happens-before