Course map

Topic 135 / Advanced

Clock synchronization and clock drift

Chapter 8.4 · Time, Ordering, and Conflicts

What and why

Topic 134 ordered events logically because physical clocks can't be trusted to agree; this topic explains why not. Every machine's clock runs on its own imperfect oscillator that drifts very slightly faster or slower than true time — clock drift. NTP (Network Time Protocol) periodically corrects a machine's clock toward a reference time over the network, which bounds the error but never eliminates it: drift reaccumulates between corrections, and the correction itself arrives over a network with its own variable delay. Builds on: → 134 (logical clocks and version vectors — the reason physical time was avoided there).

Diagram

clock error
   ▲
   │    drift up   ╱│  periodic NTP correction
   │            ╱───┘  (jump or gradual slew)
   │        ╱──┘
   └────────────────────────────────► time
   0

How it works

  1. Clock drift accumulates continuously between synchronizations — the longer the interval, the larger the possible error just before the next correction.
  2. NTP estimates and corrects both the clock's offset from true time and the network delay of the correction itself, but the correction happens on a schedule, not continuously, against a network delay that is itself variable.
  3. A correction can move the clock in two ways: a sudden jump (which can make time appear to go backward) or a gradual "slew" (adjusting the clock's own rate until it catches up, avoiding a backward jump but taking longer).
  4. Because different machines synchronize against the same reference with different offsets and delays, two machines' clocks are never guaranteed to show exactly the same instant — the same wall-clock timestamp on two machines is not proof the two events happened at the same moment, or even in that order.
  5. This is why cross-machine ordering uses logical clocks (→ 134) or explicit coordination rather than comparing timestamps directly, and why anything timing-sensitive (a lease expiry → 137, a cache TTL) needs a safety margin for the bounded-but-nonzero error rather than assuming exact synchronization.
  6. At awareness level: production systems have built further refinements on this problem. Hybrid logical clocks combine a physical-time component with a logical counter so timestamps stay close to real time while still preserving causal order. Some systems (Spanner is the commonly cited example) go further and expose the clock's own uncertainty bound explicitly, so other parts of the system can wait out that bound and get a stronger, externally consistent guarantee. Neither mechanism eliminates drift; both manage it explicitly instead of ignoring it.

Practical example

(illustrative). Two servers each synchronize periodically against the same NTP source; between corrections, each clock can drift by some small error that grows with the length of the interval. If code on server A timestamps an event, sends it to server B, and B compares that timestamp to its own local clock to decide which event happened first, the comparison can be wrong by up to the sum of both machines' accumulated drift — a real margin even though NTP is working correctly, not a sign of misconfiguration.

When to use it

  • Use when: wall-clock timestamps for human-facing display and rough, non-critical ordering where being off by the synchronization error margin is harmless.
  • Avoid when: using wall-clock comparisons to decide correctness-critical ordering between machines (which write happened first for conflict resolution, whether a lease has truly expired everywhere) — use logical clocks (→ 134) or a lease duration with an explicit safety margin instead.
  • Failure scenarios: a lease treated as expired by one node's clock while another node's clock still considers it valid, producing two holders (→ 137); events ordered purely by timestamp across machines, silently misordering writes that were actually causally related in the opposite direction.
  • Simpler alternative: for events generated on a single machine, the local clock alone is sufficient — this problem only arises when comparing clocks across machines.

Practice

(Vocabulary/process kind — classification drill; 15–20 min, no lab services). For 4 scenarios (ordering two log lines from different servers by timestamp; deciding whether a distributed lease has expired; displaying "posted 2 minutes ago" on a comment; deciding which of two concurrent writes "wins" by timestamp), classify each as safe with wall-clock time alone, or unsafe (needs a logical clock, a safety margin, or a different mechanism), and name the risk for each unsafe case. Done when every unsafe case names its specific risk, not just "clocks aren't reliable."

Key takeaway

Clock drift is continuous and NTP only bounds it, never eliminates it — never use raw timestamp comparison across machines for correctness-critical ordering. Done when you can: name one correctness bug a direct cross-machine timestamp comparison can cause, and the mechanism that avoids it. Used next in → 137 (lease expiry must account for this); Project 11.

References

Kleppmann and Riccomini, Designing Data-Intensive Applications — see references.md.

Source directory

Search the guide