Clock Skew and Lamport Clocks: Explaining Event Ordering in a System Design Interview

A common follow-up in distributed-systems interviews goes like this: "Two services write to the same record and each stamps the write with its local time. Which write wins?" The answer that loses points is "the one with the later timestamp". Here is how to give a better one.

1. Why wall clocks lie

Every machine's clock drifts. NTP keeps drift small, but small is still tens of milliseconds on a good day and seconds on a bad one (a VM paused for live migration, a leap-second smear, an NTP server that went away). If server A's clock runs 80 ms fast, a write A makes after a write from B can still carry an earlier timestamp. "Last write wins" by wall clock silently drops data.

Say this out loud in the interview: physical timestamps can tell you roughly when, but not reliably in what order.

2. Lamport clocks in four lines

Leslie Lamport's logical clock gives every event a counter so that if event a could have caused event b, then L(a) < L(b).

on local event:      counter += 1
on send(msg):        counter += 1; msg.ts = counter
on receive(msg):     counter = max(counter, msg.ts) + 1
tie-break:           compare (counter, node_id)

That tie-break gives a total order every node agrees on, which is enough for things like a replicated log or deciding who holds a lock in a simple mutual-exclusion protocol.

3. The limitation interviewers want you to name

The implication only runs one way. L(a) < L(b) does not prove that a happened before b; the two events might be concurrent. If you need to detect concurrency (say, to keep both versions of a shopping cart and merge them), you need vector clocks: one counter per node, compared element-wise. If every entry of V(a) is ≤ V(b), a happened before b; if neither dominates, they are concurrent.

4. Where hybrid clocks fit

Hybrid Logical Clocks (HLC) combine a physical timestamp with a logical counter. They stay close to wall time (handy for "show me writes from the last hour") while still respecting causality. CockroachDB uses them; Google Spanner goes the other way and bounds clock uncertainty with TrueTime, waiting out the uncertainty window before committing.

5. A 60-second answer template

  1. State the problem: wall clocks skew, so timestamp ordering can contradict causality.
  2. Offer Lamport clocks for a consistent total order, with node-id tie-breaks.
  3. Note they can't detect concurrency; offer vector clocks when you need conflict detection.
  4. Mention HLC or TrueTime if the system also needs real-time queries or external consistency.
  5. Close with the trade-off: vector clocks grow with the number of writers, so many systems prune them or use per-key version vectors.

6. Practice prompt

Try explaining, without notes, why a key-value store using last-write-wins by wall clock can lose a user's update during a network partition, and what you would change. If you can do it in under two minutes, you are ready for the follow-up questions.

Disclosure: I work on Linkjob AI, an AI interview assistant. This note is from our system-design interview prep material.