CMD Guide
HomeSystem DesignMental Models & Systems Thinking

Time, Clocks & Ordering — Why You Can't Trust the Wall Clock

You cannot trust the clock on the wall

"Which write happened last?" feels obvious — just compare timestamps. But across machines, wall clocks drift, and NTP corrections can make a clock jump backwards. Order two events on two servers by their timestamps and you can get the order wrong — which, with last-write-wins, silently loses data. Distributed systems need a different notion of time: logical time, built on causality, not seconds.

Two process timelines exchange a message; the receiver sets its Lamport clock to max(local, received)+1, preserving causal order
Two process timelines exchange a message; the receiver sets its Lamport clock to max(local, received)+1, preserving causal order

Happens-before: the only ordering you can trust

Event a happens-before b if a could have caused b: same process and earlier, or a is a send and b its receive. (Exactly the happens-before from the concurrency memory model — same idea, now across machines.) Events with no happens-before path either way are concurrent — there is no "true" order, and pretending otherwise is the bug.

Logical clocks

Between vector clocks and TrueTime sits the Hybrid Logical Clock (HLC) — a single ~64-bit timestamp that is causal like a Lamport clock yet stays within the skew bound of wall time; it is the pragmatic default in CockroachDB and MongoDB. See the deep Time & Clocks page for its update rules.

So how does Spanner order globally?

Google TrueTime faces the clock problem head-on: it exposes time as an interval [earliest, latest] with bounded uncertainty (atomic clocks + GPS), and on commit it waits out the uncertainty so two transactions can't overlap. Buying real ordering costs a deliberate wait — a vivid reminder that distributed time isn't free.

Pitfalls

Takeaways


Re-authored for this guide; Lamport-clock diagram hand-authored as SVG. Follows Lamport (1978) and DDIA ch. 8–9. See also: (Concurrency) Memory Model & happens-before, Replication (LWW conflicts), MVCC. Go deeper: Time, Clocks & Ordering — Lamport, Vector Clocks, HLC & TrueTime (the full clock ladder, with the vector-clock trace and commit-wait mechanics).

Which clock for which problem — and how LWW silently loses data

Decision table — which clock for which problem?

ProblemUseNever use
Order events within one processMonotonic clockWall clock
Detect causality across nodesVector clock / version vectorLamport clock alone
Total order across replicasConsensus epoch + log offsetWall-clock timestamp
Bound commit wait globallyTrueTime-style interval clockNTP alone

Failure trace — last-write-wins silent data loss

  1. A client sets x=1; the write is stamped by Node A, whose clock runs 20 ms fast → timestamp 10:00:00.030 (real time ≈ .010).
  2. 5 ms later (real time ≈ .015) the same client sets x=2; this write is stamped by Node B, whose clock is accurate → timestamp 10:00:00.015.
  3. In real time x=2 happened after x=1, so x=2 is the value the user expects to survive.
  4. Last-write-wins keeps the larger timestamp: .030 (x=1) beats .015 (x=2) — so the store keeps the older value x=1.
  5. The genuinely-later write x=2 is silently discarded: a fast clock on Node A quietly ate the user's most recent update.

Moral: with skewed wall clocks, "largest timestamp" ≠ "happened last." Detect the conflict with a version vector instead of guessing an order — which is exactly what Dynamo-style stores do.

Drill ladder

Ops signal: watch NTP/chrony offset and clock-step events (a backward step is the moment LWW quietly loses data), the version-vector conflict rate (rising conflicts mean concurrent writes you are silently resolving), and — on Spanner-style systems — the TrueTime commit-wait histogram (that wait is the price of global order).

🤖 Don't fully get this? Learn it with Claude

Stuck on Time, Clocks & Ordering — Why You Can't Trust the Wall Clock? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Time, Clocks & Ordering — Why You Can't Trust the Wall Clock** (System Design) and want to truly understand it. Explain Time, Clocks & Ordering — Why You Can't Trust the Wall Clock from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Time, Clocks & Ordering — Why You Can't Trust the Wall Clock** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Time, Clocks & Ordering — Why You Can't Trust the Wall Clock** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Time, Clocks & Ordering — Why You Can't Trust the Wall Clock** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes