Skip to content

Lesson 1 of 13 · 16m

Clocks Are Lying to You

Wall-clock timestamps cannot order events across machines, because NTP is allowed to move your clock backward.

Every timestamp your system writes came from a clock a daemon is actively rewriting. On Linux, clock_gettime(CLOCK_REALTIME) returns wall time — the number chrony or systemd-timesyncd believes UTC to be — and NTP is permitted to slew it, step it, and, on a leap second, play an entire second twice. When the offset is larger than about 128 ms, ntpd steps the clock: the value jumps. It can jump backward. CLOCK_MONOTONIC is the other clock the kernel gives you: never stepped, never leap-adjusted, only rate-corrected. It is the right source for measuring intervals and the wrong source for comparing notes with another machine, because its epoch is arbitrary.

Java exposes the same split. System.currentTimeMillis() is realtime. System.nanoTime() is a monotonic source for measuring elapsed time and nothing else — its origin is not meaningful and is not the same on two processes, let alone two hosts.

The wrong mental model

The assumption is that a timestamp answers the question "which event happened first?" Across a network it cannot. Two NTP-disciplined servers commonly disagree by tens of milliseconds, and that disagreement is not bounded by anything you control — a congested upstream, a lost symmetric key, a leap smear, a VM restored from snapshot. Causality is a partial order that clocks approximate badly.

What this breaks in production is conflict resolution. "Last write wins" resolves two concurrent writes by comparing their wall-clock timestamps. Cassandra lets you supply USING TIMESTAMP, and older clients genuinely did. Now picture a client whose clock runs 200 ms ahead writing a tombstone, or a node ahead-of-time deleting a value a slower node writes "earlier." The system does not report an error. It silently keeps the wrong value. The data loss has no log line in it.

The real world already demonstrated this

June 30, 2012, a leap second was inserted into UTC. The Linux kernel's hrtimer code had a livelock bug around leap adjustment, and applications using CLOCK_REALTIME saw the clock jump — the classic "one second happened twice" problem that breaks any code which assumed time moves forward. Reddit, LinkedIn, Mozilla, and a long tail of Java stacks pegged CPUs at 100%. Google's fix for the next leap second was the leap smear: lie to the fleet deliberately, spreading the extra second across the day in milliseconds, because a controlled lie beats an uncontrolled jump.

Spanner went further and made uncertainty explicit. TrueTime exposes TT.now() as an interval, not a point — GPS receivers and atomic clocks per datacenter keep the half-width epsilon in the single-digit milliseconds — and commit-wait literally sleeps out the interval before acknowledging a write. CockroachDB gets most of the same ordering benefit without atomic clocks using hybrid logical clocks, but it has to make the opposite trade: if a node's offset exceeds --max-offset (default 500 ms), the process exits. The system would rather kill a node than let its clock order writes.

Logical clocks exist for the same reason. A Lamport clock guarantees a -> b implies L(a) < L(b), but the converse is false — L(a) < L(b) does not prove a caused b. Vector clocks pay more metadata to detect concurrency rather than hide it.

What to actually do

Measure durations with a monotonic clock only: clock_gettime(CLOCK_MONOTONIC), System.nanoTime(), Go's time.Since (monotonic-aware since Go 1.9). Treat stored wall timestamps as data about an event, not truth about its order. Order events with sequence numbers that exist because they order things: Kafka offsets, Postgres WAL LSNs, etcd revisions, Raft log indices. When a caller supplies a timestamp for anything security- or correctness-relevant — token expiry, write conflict, retry dedup windows — do not use their clock. Alert on NTP offset; a node with 500 ms of skew is a correctness bug in waiting.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.