Guide

On one machine, "which happened first?" is easy. You compare timestamps, or read the lines of a log in order. Across machines, that breaks. Each server has its own clock. The clocks disagree by milliseconds or sometimes seconds, and they can jump without warning. Two updates made 3 ms apart on two servers can carry timestamps in the wrong order. A database that keeps "the latest" write then throws away the one that actually came last.

Ordering sits under nearly every distributed problem. Replicas must apply writes in the same order. A reply must not appear before its question. A lock must not go to two holders. You can order events without trusting clocks, or you can bound how wrong the clocks can be. The pages below go in that order.

Concepts on this topic

StepConceptWhat it answers
1Clock drift and NTPHow wrong can a clock get, and how is it corrected?
2Monotonic vs wall-clock timeWhich clock measures durations safely?
3Last-write-wins and clock skewHow does skew silently lose writes?
4Lamport clocksHow can counters respect cause and effect?
5Vector clocksHow do you detect concurrent events?
6Hybrid logical clocks and TrueTimeHow do you keep time close to real time, and bounded?

Which approach fits

ApproachClock needsOrdering it givesUsed by
Wall clock with LWWNTP, hopeNone reliable. Loses writes under skewCassandra, many caches
Lamport clockNoneTotal order consistent with causalityRaft terms, many protocols
Vector clockNoneExact causality, detects concurrencyDynamo, Riak, CRDT libraries
HLCBounded skewCausality plus near-real-timeCockroachDB, YugabyteDB, MongoDB
TrueTimeGPS and atomic clocks, explicit εExternal consistencySpanner

Total order and consensus

The tools above order events after the fact. Many systems need more. Every node must deliver the same messages in the same order, and once message 17 is delivered, nothing can be inserted before it. That is total order broadcast. It needs reliable delivery to all non-faulty nodes, and the same order everywhere.

Lamport timestamps look like they solve this, but they don't. A node that receives timestamp 5 can't know whether another node is about to send timestamp 4. It would have to hear from every node first, and one dead node would block everyone.

Total order broadcast is equivalent to consensus. A replicated log is one such broadcast. Problems such as electing one leader, granting one lock holder and assigning gap-free sequence numbers all need consensus. That's why systems like ZooKeeper and etcd are the standard tools. The consensus page shows how it's done.

In practice

  • Never time a duration with wall-clock time. Search for Date.now() - or time.time() - used for timeouts, and switch to monotonic clocks.
  • Don't order events across machines by their timestamps unless the system was designed for it. Hybrid logical clocks with a maximum offset, or TrueTime, are the designed options.
  • Be suspicious of last-write-wins. Find out which writes can conflict, and whether losing one is acceptable.
  • Monitor clock offset. Alert when a node's NTP offset grows past a threshold.
  • Use sequence numbers inside one partition. A Kafka offset or a database sequence is a total order with no clocks involved.
  • In interviews, expect "whose clock?" When you say "we'll order by timestamp," have the answer ready: a per-conversation sequence assigned by the partition leader, as in the chat system case study.

Key takeaways

  • Clocks drift, and NTP corrects them only within the network's limits.
  • Use monotonic clocks for durations, and wall clocks only for display.
  • Last-write-wins with physical timestamps silently discards writes inside the skew window.
  • Lamport clocks give an order that respects causality. Vector clocks also detect concurrency.
  • Hybrid logical clocks and TrueTime bound the uncertainty, and TrueTime waits it out.
  • Total order broadcast, a replicated log and consensus are equivalent problems.

Further learning

Kleppmann's lecture on logical time builds Lamport and vector clocks step by step.

Distributed Systems 4.1: Logical timeMartin Kleppmann

His earlier lecture covers physical time, quartz drift, leap seconds and monotonic clocks.

Distributed Systems 3.1: Physical timeMartin Kleppmann
Time, Clocks, and the Ordering of Events in a Distributed System (Lamport, 1978)time-clocks.pdflamport.azurewebsites.net How and why the leap second affected Cloudflare DNSAt midnight UTC on New Year’s Day, deep inside Cloudflare’s custom RRDNS software, a number went negative when it should always have been, at worst, zero. A little later this negative value caused RRDNS to panic.Cloudflare Blog Living without atomic clocks (CockroachDB)If someone knows even a little about Spanner, the first question they have is: "How does CockroachDB provide external consistency without atomic clocks?"cockroachlabs.com Logical Physical Clocks and Consistent Snapshots (Kulkarni et al., HLC paper)2014-04.pdfcse.buffalo.edu