1.Wall-clock time can move; process clocks drift
Wall-clock time is the human-facing notion of "what time is it right now" — and the fact that it "can move" refers to something most application code implicitly assumes cannot happen: a system clock does not only ever advance smoothly forward. NTP (Network Time Protocol) synchronization, which most servers use to keep their clocks accurate, corrects a clock periodically against a reference time source, and that correction can move the clock backward as well as forward if it had drifted ahead — meaning code that reads the wall clock twice in a row and assumes the second reading is always greater than or equal to the first can be wrong. Separately, even between synchronization corrections, every machine's clock runs on its own local oscillator, which is not perfectly accurate — this is clock drift, and it means two machines' clocks, even if synchronized a moment ago, will have diverged by some small amount by the time either is read again, with the exact amount of divergence depending on hardware quality and how recently each was last corrected. Neither of these is a rare hardware fault; both are the normal, expected behavior of real clocks on real machines, in every data center, all the time. Code that treats wall-clock time as instantaneous, monotonically increasing, and identical across machines is making three assumptions that are each false in the general case, and a design that depends on any of them being true needs a different tool for that specific need — which is exactly what the next two concepts in this section provide.
Key features
- NTP synchronization corrects a system clock periodically against a reference source, and that correction can move the clock backward, not just forward — code cannot assume two consecutive wall-clock reads are non-decreasing
- Clock drift is the natural divergence between two machines' local oscillators, happening continuously even between synchronization corrections — two machines synchronized a moment ago will already have measurably diverged clocks by the next read
- Neither NTP correction nor drift is a rare hardware fault — both are the normal, continuous, expected behavior of every real clock on every real machine
- The magnitude of drift and correction varies by hardware and network conditions, but in practice can be tens of milliseconds to low seconds between synchronizations on typical commodity hardware — enough to matter for anything comparing timestamps at fine granularity across machines
- A single machine's wall clock going backward can break code that assumes elapsed time (end_time - start_time) is always non-negative, or that a later-created record always has a numerically larger creation timestamp
- This is a distinct problem from the previous section's "arrival order is not production order" — clock drift means even the timestamps themselves, set at true production time on different machines, may not accurately reflect the true relative order of events
Two distinct clock behaviors and what each one breaks
- T+0: Clock synchronized — matches reference time exactly
- T+30min: Drift accumulates — local oscillator runs slightly fast; clock is now ahead
- T+60min: NTP correction — clock is stepped backward to match reference time
- T+60min+ε: A reading taken just after — can be earlier than a reading taken just before the correction
Command
# An assumption that silently breaks when the wall
# clock moves backward mid-measurement
start = time.time()
do_work()
end = time.time()
duration = end - start # can be NEGATIVE if an NTP
# correction moved the clock
# backward between the two readsA wall clock can move backward (NTP corrections) and two machines' wall clocks continuously drift apart between corrections (clock drift) — both are the normal, expected behavior of real clocks, not rare faults. Never use the wall clock to measure elapsed duration (use a monotonic clock instead) and never assume two machines' wall-clock timestamps are precise enough for an exact ordering decision, even when both run NTP in the same data center.