Local counter: 1
Cloud / Knowledge library
Logical Clock
Orders events by causality rather than elapsed physical time
Why learn Logical Clock?
It avoids assuming perfectly synchronized machine clocks
The problem it solves
It gives distributed protocols a stable event-ordering mechanism
Where it came from
Logical Clock is a distributed systems concept that orders events by causality rather than elapsed physical time. Its guarantees and tradeoffs depend on the concrete implementation and operating context.
Build your understanding
Work through these exercises in order. Try the task before opening a reference.
- Define Logical Clock and distinguish it from a neighboring concept.
- Choose a realistic task where Logical Clock is appropriate.
- Identify a failure mode caused by misusing Logical Clock.
- Interpret a small design or result involving Logical Clock.
- Explain one tradeoff or limitation of Logical Clock.
Documentation and sources
- Designing Data-Intensive Applications (dataintensive.net)
Connect the ideas
Explore next
Real World Example
Global Inventory Reconciliation for Distributed Warehouses
A global retail chain operating independent regional inventory management systems that synchronize stock levels across geographic boundaries.
The business problem
Physical clocks on servers in different regions drift, causing out-of-order event processing where a stock replenishment event appears to occur before the initial sale, leading to incorrect inventory counts.
How this topic solves it
Implementing Lamport logical clocks allows the system to order events by causality rather than elapsed physical time. By attaching a monotonically increasing counter to every inventory update, the system ensures that if event A causes event B, the logical timestamp of A is strictly less than B, regardless of server clock skew.
- Assign a unique local counter to each inventory node starting at zero.
- Increment the local counter before executing any local inventory transaction.
- Attach the current counter value as a logical timestamp to all outgoing inter-node messages.
- Upon receiving a message, update the local counter to be the maximum of the current local value and the received timestamp plus one.
- Process events based on the logical timestamp sequence to maintain a consistent causal history.
This diagram illustrates how events propagate through nodes using logical timestamps to maintain causal order.
Timestamp: 1
Local counter: 2
Timestamp: 2
- Warehouse A → increments → Stock Sale
- Stock Sale → sends update → Warehouse B
- Warehouse B → syncs and increments → Replenishment
Expected business outcome
The system achieves a consistent causal ordering of inventory transactions, ensuring that stock depletion and replenishment events are processed in the correct logical sequence, preventing negative inventory artifacts.
Limitations and checks
Logical clocks provide a total ordering of events but cannot determine if two events are concurrent or if one happened before another in real-time. To check for this, one must inspect the partial order of causality; if neither event's timestamp is strictly less than the other, they are concurrent.