Skip to main content
KBY Technologies Logo
cd ../lexicon
sys/docs/lexicon/lamport-timestamp-logical-clock.md
Lexicon Entry

Lamport Timestamp (Logical Clock)

A monotonically increasing scalar counter, assigned to each event in a distributed system, that establishes a partial order consistent with causality without relying on synchronized wall clocks. It guarantees that if event A causally precedes event B, then timestamp(A) < timestamp(B), though the converse does not hold.

Practical example

A leaderless key-value store tags every write with a Lamport counter to order replicated log entries deterministically across shards, then breaks ties with node ID to derive a strict total order for applying writes consistently on every replica.

A Lamport clock is the minimal mechanism required to order events in a system with no shared memory and no reliable global clock. Each process maintains a local integer counter. On every local event the process increments its counter. On message send, the current counter value is attached to the message. On message receive, the process sets its counter to max(local_counter, received_timestamp) + 1. This single rule is sufficient to enforce the Clock Condition: causal precedence implies timestamp precedence.

The critical limitation, and the reason more elaborate structures like Vector Clocks and Interval Tree Clocks exist, is that Lamport timestamps only provide a necessary condition for causality, not a sufficient one. Two events can have timestamp(A) < timestamp(B) while being entirely causally unrelated (concurrent). This makes Lamport clocks unsuitable for detecting concurrent writes or conflict resolution in replicated data stores — you cannot use them to answer “did A happen-before B, or are they concurrent?” You can only ever answer “B did not happen-before A.” Systems that need genuine concurrency detection must pay the O(n) space cost of vector clocks or adopt a hybrid encoding.

In practice, engineers rarely deploy bare Lamport counters directly; they appear as the theoretical substrate underneath higher-order mechanisms. Total Order Broadcast implementations often break Lamport timestamp ties using a fixed process ID ordering to derive a strict total order from the partial order — this is the classic construction for deriving a consistent global sequence from logical clocks. Hybrid Logical Clocks extend the same increment-and-merge rule but bind the counter to physical time so that timestamps remain causally consistent while also approximating wall-clock semantics for external observers. Distributed databases that assign monotonic transaction sequence numbers per-shard (independent of GTID or LSN mechanisms) are frequently reimplementing Lamport’s merge rule under a different name.

The operational failure mode to watch for is counter overflow and reset semantics across restarts: a process that crashes and restarts with a stale or zeroed counter can issue timestamps lower than ones it previously emitted, silently violating the Clock Condition for any events causally downstream of the crash. Persisting the counter to stable storage before acknowledging any send, or deriving a safe restart floor from the highest timestamp observed in persisted logs, is mandatory wherever Lamport ordering feeds into durability or replication guarantees. Engineers should also resist the temptation to treat a low timestamp gap as evidence of low latency or near-simultaneity — the counter encodes nothing about elapsed physical time, only relative causal position.

Lamport timestamps remain foundational precisely because they are the cheapest possible causality-respecting primitive: a single integer, one comparison, one merge rule. Understanding their strict happens-before guarantee — and its explicit inability to detect concurrency — is what allows an engineer to correctly choose between a bare logical clock, a vector clock, an HLC, or a full consensus-derived sequence number when designing ordering guarantees into a distributed protocol.