Clocks, and why you cannot trust them
Wall time goes backwards, monotonic time does not measure what you think, and both of them are lying about the other machine.
There are two clocks on your machine and most bugs come from using the wrong one.
Wall clock — time.time(), System.currentTimeMillis(), Date.now() — answers "what time is it." It is synchronised to the outside world by NTP, which means it can jump forward, jump backward, or be smeared slowly across a leap second.
Monotonic clock — time.perf_counter(), System.nanoTime(), performance.now() — answers "how much time has passed." It only increases. It has no relationship to any calendar and its zero point is arbitrary.
The rule is short: timestamps use the wall clock, durations use the monotonic clock. Every violation is a bug waiting for an NTP correction.
# wrong: a 30-second timeout that NTP can turn into -4 seconds or 12 hours
start = time.time()
while time.time() - start < 30:
...
# right
start = time.perf_counter()
while time.perf_counter() - start < 30:
...That first loop is in more production code than anyone would like to admit. It works perfectly until the day it does not.
the harder problem: clocks on other machines#
Within one process, the monotonic clock is trustworthy. Across machines, nothing is.
NTP typically holds machines within a few milliseconds of each other in a well-run data centre and within tens of milliseconds elsewhere. "Typically" is doing real work in that sentence. A machine with a failing NTP daemon can drift for days.
This breaks three things people build constantly:
Ordering by timestamp. Two events written by two servers cannot be ordered by comparing their timestamps if they are within the clock error of each other. "Last write wins" across nodes silently means "whichever node had the faster clock wins."
Distributed locks with TTLs. A lock that expires at an absolute time assumes both parties agree on what time it is. They might not. Prefer fencing tokens: a monotonically increasing number issued with the lock, checked by the resource, so a stale lock holder's writes are rejected regardless of clocks.
Certificate and token validation. A machine whose clock is wrong by an hour will reject valid tokens or accept expired ones. When a fleet suddenly cannot authenticate, check clock skew before you check the auth service.
what to use instead of timestamps for ordering#
- Sequence numbers from a single source. Simple, correct, and a bottleneck — which is often fine.
- Logical clocks. A Lamport counter gives you causality: if A caused B, A's counter is lower. It does not tell you about concurrent events, which is honest, because there is no answer.
- Vector clocks. Detect concurrency explicitly, at the cost of size proportional to the number of nodes.
- Hybrid logical clocks. Wall time plus a logical component, so timestamps are both human-readable and correctly ordered. This is what you want when you need both properties, and it is what several modern databases use internally.
the operational checklist#
- Monitor clock offset as a metric on every host.
chronyc trackingorntpq -pgives you the number; alert above 100ms. - Store timestamps in UTC, always, and store the offset separately if you need to reconstruct local time.
- Never persist a monotonic value. It is meaningless after a restart.
- Log both when it matters: the wall time for humans, a sequence number for machines.
If a bug's explanation requires two events to have happened in a specific order, and your only evidence is that one timestamp is smaller than the other, you do not have evidence.
— Dom, November 11, 2026