← Lessons

quiz vs the machine

Silver1050

System Design

Lamport Timestamps

Ordering events across machines without a shared clock using simple counters.

4 min read · intro · beat Silver to climb

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.

Check yourself

Answer to earn rating on the learn ladder.

1. On receiving a message what does a process do to its counter?

2. Why does a smaller Lamport timestamp not prove that one event caused another?