Course contentsShow
← Computer Science
Lesson 2628 of 2,87258. Distributed Systems BasicsFree lesson

Lamport Timestamps

Using logical counters to capture causal ordering of events across distributed nodes.

Lamport Timestamps

You'll learn how to assign logical sequence numbers to events so different nodes can agree on "what happened before what" without relying on physical clocks.

The Core Problem

In a distributed system, nodes can't rely on wall-clock times to order events because clocks skew and drift. You need a way to capture causality: if event A influenced event B, A should have a "smaller" timestamp.

How Lamport Timestamps Work

Each node maintains a simple logical counter (starting at 0). The rules are:

  1. Before an event: increment your counter
  2. Send a message: include your current counter value
  3. Receive a message: set your counter to max(your_counter, message_counter) + 1

This ensures that if event A happens-before event B (either on the same node or via message passing), then timestamp(A) < timestamp(B).

Example

  • Node 1 sends message at counter 5
  • Node 2 receives it when its counter is 3
  • Node 2 updates: max(3, 5) + 1 = 6
  • Node 2's next event gets timestamp 7

Now we know Node 2's event at timestamp 7 happened after Node 1's send at timestamp 5, respecting causality.

What It Doesn't Do

Lamport timestamps create a partial order: if timestamp(A) < timestamp(B), A might have happened before B—but not necessarily. Concurrent events (no causal relationship) can have arbitrary ordering. To break ties, you typically append node IDs: (timestamp, node_id).

Key Takeaway: Lamport timestamps use incrementing counters and message-piggybacking to establish a logical ordering that respects causality, giving distributed systems a consistent way to reason about event sequences without synchronized clocks.