In a single machine, we can confidently use the system clock to determine the order of events. In a distributed system, this is impossible because:
- Clocks drift apart.
- Network latency is variable, making synchronization imperfect (e.g., NTP).
- A node might pause (Garbage Collection, VM migration) and lose track of time.
Without a shared notion of time, how do we agree on which user made an update first? How do we detect causality between events across different machines?
Since physical time is unreliable, Leslie Lamport proposed a solution in 1978: Logical Clocks. Instead of measuring absolute time (seconds/minutes), logical clocks measure the ordering of events.
An event happens before event (written as ) if:
- and occurred on the same process, and happened before .
- is the sending of a message by one process, and is the receipt of that message by another process.
- Transitivity: If and , then .
Lamport Timestamps
Each process maintains a local counter, .
Event Ordering with Lamport Clocks
Before executing an event, process increments its counter: .
When sending a message , piggybacks its current counter value on the message: .
Upon receiving a message , a process updates its counter to be greater than both its current value and the message's value: .
If event happens before event (), then the Lamport timestamp of is strictly less than the Lamport timestamp of ().
If , we cannot conclude that . They might be concurrent events that just happened to be assigned these timestamps. This limitation led to the development of Vector Clocks.