100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What are Lamport Timestamps and How Do They Order Events?

Learn how Lamport timestamps order distributed events via logical clocks, the happens-before rule, and their limits vs vector clocks.

mediumQ117 of 224 in System Design Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

A Lamport timestamp is a single logical counter each node maintains and increments for every local event, and sends along with every outgoing message so that receivers can advance their own counter past it, producing a total ordering of events that respects causality even though machines have no synchronized physical clocks.

Each node keeps one integer counter starting at zero. On any local event, the node increments its counter by one. When it sends a message, it attaches its current counter value; when a node receives a message, it sets its counter to one more than the maximum of its own counter and the received timestamp, then processes the event. This guarantees the “happens-before” property: if event A causally precedes event B, A’s timestamp is strictly less than B’s. The catch is the reverse is not guaranteed — two unrelated events can end up with the same or arbitrarily ordered timestamps, so Lamport timestamps only give a partial, causally-consistent view, not proof of true concurrency detection (that requires vector clocks). To get a total order for things like tie-breaking, systems typically pair the Lamport counter with a fixed node ID as a tiebreaker.

  • Provides a consistent event ordering across nodes without needing synchronized physical clocks
  • Extremely lightweight — a single integer per node, cheap to maintain and transmit
  • Guarantees the happens-before property required for causal correctness
  • Forms the basis for total ordering when combined with a node-ID tiebreaker

AI Mentor Explanation

A Lamport timestamp is like each umpire on the field keeping a single running ball-count instead of relying on wall clocks that might not be synced. Every time an umpire signals something, they bump their own count by one; when umpires confer and one hears the other’s count, they set their own to one more than the higher of the two before continuing. This guarantees that if one signal genuinely caused another, its count is always lower, even though the numbers alone can’t prove two signals were truly independent. That single incrementing counter, bumped past whatever it hears, is exactly how a Lamport timestamp orders events.

Step-by-Step Explanation

  1. Step 1

    Initialize a single counter per node

    Each node keeps one logical clock, an integer starting at zero.

  2. Step 2

    Increment on every local event

    Before processing any local event, the node increments its counter by one.

  3. Step 3

    Attach timestamp to outgoing messages

    The current counter value is sent along with every message to other nodes.

  4. Step 4

    Advance past received timestamps

    On receiving a message, the node sets its counter to max(own, received) + 1, preserving happens-before order.

What Interviewer Expects

  • Correctly describes the increment-on-event and max-plus-one-on-receive rules
  • States the happens-before guarantee: causally related events are correctly ordered
  • Acknowledges the limitation: equal or comparable timestamps do not prove true independence
  • Distinguishes Lamport timestamps (partial/logical order) from vector clocks (concurrency detection)

Common Mistakes

  • Confusing Lamport timestamps with wall-clock/NTP-synchronized time
  • Claiming Lamport timestamps can detect true concurrency (that requires vector clocks)
  • Forgetting the max(own, received) + 1 rule on message receipt
  • Not mentioning the node-ID tiebreaker needed to get a strict total order

Best Answer (HR Friendly)

A Lamport timestamp is a simple counter each machine keeps that goes up every time something happens locally, and gets updated to stay ahead of any counter value it receives from another machine. This lets a distributed system agree on an order of events that respects cause and effect, without needing every machine’s clock to be perfectly synchronized.

Code Example

Lamport clock increment and receive rules
class LamportClock {
  constructor() {
    this.counter = 0
  }

  localEvent() {
    this.counter += 1
    return this.counter
  }

  send() {
    this.counter += 1
    return this.counter // attach this to the outgoing message
  }

  receive(incomingTimestamp) {
    this.counter = Math.max(this.counter, incomingTimestamp) + 1
    return this.counter
  }
}

// Total order tiebreak: combine (timestamp, nodeId) and compare lexicographically
function totalOrderKey(timestamp, nodeId) {
  return `${timestamp}:${nodeId}`
}

Follow-up Questions

  • How does a Lamport timestamp differ from a vector clock in what it can guarantee?
  • How would you turn Lamport timestamps into a strict total order across all nodes?
  • Where are Lamport timestamps used in real distributed systems, e.g., for distributed mutual exclusion?
  • What happens if a node’s Lamport counter is not incremented on every message send?

MCQ Practice

1. What does a Lamport timestamp guarantee?

Lamport timestamps guarantee the happens-before property: if A causes B, A’s timestamp is smaller than B’s.

2. On receiving a message, how does a node update its Lamport counter?

The receive rule advances the local counter past whatever timestamp it just learned, preserving causal order.

3. What is a key limitation of plain Lamport timestamps compared to vector clocks?

Lamport timestamps give a consistent partial order but two unrelated events can still get comparable timestamps, so they cannot prove concurrency the way vector clocks can.

Flash Cards

What is a Lamport timestamp?A single logical counter per node, incremented on events, used to order events causally across a distributed system.

Receive rule for Lamport clocks?Set the local counter to max(own, received) + 1.

What guarantee do Lamport timestamps provide?The happens-before property: causally related events get increasing timestamps.

Key limitation of Lamport timestamps?They cannot prove true concurrency between two unrelated events, unlike vector clocks.

1 / 4

Continue Learning