Distributed systemslamport clocklamport timestamphybrid logical clock

Logical clock

Definition

A logical clock assigns numbers to events such that if event A could have caused event B, A gets a smaller number; a Lamport clock is a single counter, while a vector clock keeps one counter per node to also detect concurrent events.

In practice

Each node increments its counter on every event and, when it receives a message, sets its counter to the maximum of its own and the sender's plus one. That captures the happens-before relation without trusting physical clocks, which is what replication, event ordering and conflict detection actually need.

A Lamport clock gives a total order but cannot tell whether two events were concurrent or causally related; vector clocks can, at the cost of one entry per node. Hybrid logical clocks combine physical time with a logical counter so timestamps stay close to real time while respecting causality, as in CockroachDB.

Example

Node A sends a message stamped 5; node B, whose counter is 3, receives it and sets its counter to 6, so its reply is stamped 6 and is always ordered after the message it answers.