Why wall clocks fail
In a distributed system each machine has its own clock, and those clocks drift apart. You cannot trust timestamps to tell you which event truly happened first. Lamport timestamps give you a logical clock that captures causal order instead.
The rules
Every process keeps an integer counter.
- Before each local event, increment the counter.
- When you send a message, attach the current counter value.
- When you receive a message, set your counter to max of local and received, then increment.
This guarantees that if event A causally happened before event B, then the timestamp of A is less than the timestamp of B.
What it cannot do
The reverse is not true. A smaller timestamp does not prove causality, because two unrelated events can get comparable numbers. To break ties and get a total order, pair the timestamp with the process id.
When to reach for it
Lamport clocks are cheap and need no synchronization. They power ordered logs and tie breaking but cannot detect concurrency, which is where vector clocks take over.
Key idea
Lamport timestamps assign monotonic logical numbers so that causal order is always preserved even without synchronized physical clocks.