What it is
A logical clock assigns numbers to events so that the numbers respect causality rather than physical time. Lamport clocks (Leslie Lamport, "Time, Clocks, and the Ordering of Events in a Distributed System", 1978) give each event a single counter. Vector clocks (Fidge and Mattern, 1988) give each event a vector with one counter per node, which is enough to tell whether two events are causally related or concurrent. Hybrid logical clocks (Kulkarni et al., 2014) combine a physical timestamp with a logical counter.
Why it exists
Distributed systems constantly need to order events: which write is newer, which message came first, whether an update was made with knowledge of another. Physical clocks cannot answer that reliably across machines. Quartz oscillators drift (tens of parts per million is common), NTP corrections step or slew clocks, virtual machines pause, and leap seconds are handled differently by different systems. NTP typically keeps servers within a few milliseconds of each other, but any two events closer together than the error can be ordered backwards, and a misconfigured node can be off by much more. Last-writer-wins on wall-clock timestamps then silently discards newer data.
How it works
Happened-before. e → f if e and f are on the same node and e came first; or e is the sending of a message and f its receipt; or there is a chain of such steps. If neither e → f nor f → e, the events are concurrent (e ∥ f): neither could have influenced the other.
Lamport clocks. Each node keeps a counter L. It increments L before each event, attaches L to each message, and on receipt sets L = max(L, L_message) + 1. This guarantees e → f ⇒ L(e) < L(f). Breaking ties by node id yields a total order that all nodes agree on, which algorithms such as Lamport's mutual exclusion and some replicated logs use. The converse does not hold: L(e) < L(f) can be true for concurrent events, so Lamport timestamps cannot detect conflicts.
Vector clocks. Each node keeps a vector V with one entry per node. It increments its own entry on each event, attaches V to messages, and on receipt takes the element-wise maximum and then increments its own entry. Compare: V ≤ W if every entry of V is ≤ the matching entry of W. Then e → f if and only if V(e) < V(f), and if neither V(e) ≤ V(f) nor V(f) ≤ V(e), the events are concurrent. A replica can therefore decide whether an incoming version descends from what it has (replace it) or is concurrent (keep both as siblings and resolve).
Version vectors. Storage systems usually attach a vector per data item with one entry per replica that has coordinated writes, rather than per client or per event. Amazon's Dynamo paper (2007) described this, and Riak used it, later with dotted version vectors to avoid false conflicts. Clients read all siblings and write back a merged value whose vector dominates them.
Hybrid logical clocks. An HLC timestamp is a pair (l, c). l is the largest physical time the node has seen, c counts events within the same l. On send or local events, l = max(l, physical time), with c reset to 0 if l advanced and incremented otherwise; on receipt, l = max(l, l_message, physical time) and c is chosen so the new timestamp exceeds both the local and the message timestamp. HLC timestamps stay within the clock skew of physical time, respect happened-before, and fit in 64 bits.
Synchronised clocks with bounded uncertainty. Google Spanner's TrueTime exposes clock time as an interval with an explicit error bound (kept small with GPS and atomic clocks) and makes transactions wait out that uncertainty before committing, which yields externally consistent timestamps. Cloud providers now offer similar precise time services.
When to use it
- Lamport clocks when you need a total order consistent with causality (ordering requests, tie-breaking) and do not need to detect concurrency.
- Version vectors when replicas accept writes independently (multi-leader, leaderless) and you must detect conflicts rather than silently drop one.
- Hybrid logical clocks for database transaction timestamps and MVCC when you want time-like values ("read as of 10:00") without trusting clocks for correctness.
- Causal consistency: delivering messages or showing updates only after everything they depend on.
When not to
- Within a single-leader system that already orders every write (a database primary, a Raft log): the log position is the order.
- When last-writer-wins is acceptable for the data (caches, presence indicators) and occasional loss of a concurrent update does not matter.
- When the number of writers is huge and unbounded: per-writer vectors grow without limit; use per-replica version vectors or a different conflict strategy.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Wall-clock LWW | Simple, small timestamps | Lost updates whenever clocks disagree or writes are concurrent |
| Lamport clock | One integer; total order consistent with causality | Cannot detect concurrency |
| Vector / version vector | Detects concurrent updates exactly | One entry per node or replica on every value and message; application must merge siblings |
| Hybrid logical clock | Causal and close to physical time, 64 bits | Correctness of time-based reads still depends on a configured maximum clock offset |
| TrueTime-style bounded clocks | External consistency | Special time infrastructure and commit wait |
Alternatives
- A single sequencer or leader that orders all writes (a database primary, a Raft log, a Kafka partition).
- CRDTs: data types whose concurrent updates merge automatically; many use version-vector-like metadata internally.
- Application-level versions: optimistic concurrency with a version number checked on write, which turns concurrent updates into a retryable error.
Failure modes
- Silent lost updates under LWW when clocks are skewed or a node's clock jumps forward (all its future writes win) or backward.
- Sibling explosion: clients that never merge siblings let them pile up.
- Vector growth and pruning: pruning old entries to bound size can make descendant versions look concurrent (or the reverse).
- Exceeding the assumed clock offset in HLC-based databases: CockroachDB, for example, makes a node shut itself down when its clock drifts beyond the configured maximum offset relative to the cluster, rather than risk anomalies.
In production
Monitor clock offset on every node (chrony or NTP statistics) and alert well below the bound your database assumes. Avoid last-writer-wins for data where losing a concurrent update matters; if you must use it, use a logical or hybrid clock rather than raw wall-clock time. In leaderless stores, make sure clients read siblings and write merged values. Know which clock your database uses: CockroachDB and YugabyteDB use hybrid logical clocks, MongoDB uses a hybrid cluster time for causal consistency, Spanner uses TrueTime.
Common misconceptions
- "NTP keeps clocks in sync, so timestamps are fine." Milliseconds of error are enough to reorder events, and clocks can jump.
- "A higher Lamport timestamp means the event came later." Only for causally related events; concurrent events also get ordered numbers.
- "Vector clocks resolve conflicts." They detect them; resolution is up to the application or a merge rule.
- "Logical clocks measure time." They measure causality; they say nothing about how long ago something happened (HLCs add an approximation).
Interview angle
When a design has replicas accepting writes, ask how conflicts are detected. Explain why wall-clock last-writer-wins loses data, then contrast Lamport clocks (order, no concurrency detection) with vector clocks (detect concurrency, need merging). Mentioning version vectors in Dynamo-style stores, HLCs in CockroachDB and Spanner's TrueTime shows you know how the theory is used.
Model assumptions
This lesson is a scripted illustration, not a simulation: the simulator has no per-node clocks, so every message is a packet placed by hand and clock values are annotations. The lesson's tests recompute every Lamport, vector and hybrid clock value from the scripted messages using the textbook rules and check the annotations against them. Latencies (20 ms and 30 ms one way), clock offsets (+50 ms, −40 ms) and physical clock readings are illustrative. In the vector steps each send carries a write; the receive of a message counts as an event, as in the standard vector clock rules.