Clock Skew: What a 3-Second Gap Between Servers Broke

The customer support ticket contained a report that violated the laws of physics: according to the trip report, the vehicle had entered its destination at 14:03:11 but had started the trip at 14:03:13. In other words, the vehicle arrived two seconds before it set off. We read the code that generated the report three times, and there was no bug. We looked at the data, and the data really did say that. The trip start event had been processed on one server and the end event on another, and there was a 3.2-second difference between the two servers' wall clocks. The code was right, the data was right, and the time was wrong.

How a Clock Silently Drifts

The first question was: how can this happen when we have NTP? The answer was hiding in the chasm between "NTP is installed" and "NTP is working." The server in question was a virtual machine that had been live-migrated during hypervisor maintenance a few weeks earlier. After the migration, ntpd interpreted the sudden clock jump as "this source is unreliable," gave up on synchronization, and didn't tell anyone. From then on, the machine had been at the mercy of its own crystal oscillator, drifting by about half a second a day. We had no alert for it, because we had never thought of clock synchronization as something to monitor. We were monitoring disk usage, memory usage and service uptime; it simply never occurred to us that time itself could break.

After a live migration ntpd stopped syncing, the clock drifted silently and an impossible arrival time appeared in the trip report.
Having NTP installed and having NTP working are not the same thing.

The Damage List Was Longer Than We Thought

The trip report was just the tip of the iceberg. As we dug, we found what else those 3.2 seconds had touched. In the cache layer, TTL calculations went off: what one server wrote as fresh, the other considered dead before it was even born. Our debugging tool, which merges logs by timestamp, put events in the wrong order and twice led us to believe in false cause-and-effect relationships; in one case we spent half a day chasing a ghost in which the effect was logged before the cause. The sneakiest victim was the rate limiter: because the window calculation worked differently on the two servers, some users could never reach their limits while others hit them early. A single broken clock had produced four different diseases in four separate systems with no code connection between them.

The clock difference broke cache TTLs, log ordering, rate limiter windows and the trip report.
Different diseases appeared in systems with no code connection between them.

Right after the incident, we audited the entire fleet: we ran a small script on every machine that measured its offset from the reference NTP source. The distribution of the results was a lesson in itself: most machines were under 10 milliseconds, three were in the 100-500 millisecond band, and our hero was at 3.2 seconds. So the problem wasn't an isolated accident; the fleet had been quietly drifting apart because nobody was looking. Two of those three in-between cases turned out to have the same cause: ntpd sulking after a virtual machine migration. Finding three cases of the same disease at different levels of severity on the same day killed the comforting thought "this won't happen again" before it was even born, and ended any haggling over whether we needed a monitoring metric.

The Fix: chrony, an Alert and a Decision

The technical fix was quick. We replaced ntpd with chrony on every machine; it recovers noticeably more intelligently after virtual machine migrations. We added clock offset to central monitoring as a metric: every machine continuously reports its distance from the reference source, an offset above 100 milliseconds raises a warning, and an offset above 500 milliseconds raises a critical alert. In its first week, this alert caught two more machines. It turned out we had been drifting as a herd without knowing it.

But the truly lasting fix was an architectural decision: we stopped entrusting the order of events to the wall clock. For events coming from the same device, the device's packet sequence number is the single source of truth; trip start and end are now ordered not by the clocks of two different servers but by the same monotonic counter. Where there's no sequence number, the rule is that whichever layer stamps a timestamp on an event, it should always be the same layer. Comparing the clocks of two different machines on the same ruler makes as little sense as adding up grams from two different scales.

One easily overlooked part of the fix was duration measurement. We found dozens of places in the codebase that computed "end time minus start time" with both values taken from the wall clock. Because NTP can pull the wall clock backward, these differences can come out negative. Sure enough, we found a "processing time" of minus 1.4 seconds in the logs and realized that one of our metrics libraries was silently skipping negative values. Every place that measures a duration was moved to the monotonic clock, which never goes backward, only forward, and exists precisely for this job. The rule is now on our code review checklist: the wall clock answers "when did it happen?" and the monotonic clock answers "how long did it take?" Code that mixes them up gets sent back.

The wall clock measures when an event happened, the monotonic clock measures duration; mixing them up produces negative durations.
In code review, code that mixes up the two gets sent back.

Device Clocks: A Whole Other World of Trouble

We got our servers back in line, but out in the field there are thousands of vehicle tracking devices, and their clocks are another realm entirely. A GPS module gets perfect time from the satellites, but when there's no satellite visibility, some models fall back on an internal clock, and those clocks can drift by minutes. We saw one device model jump its clock on coming out of a tunnel and send the same location packet stamped both "in the past" and "now." That's why every event in our schema has two times: the time the device declares and the receipt time we stamp at the door. The difference between the two is a signal in its own right; if it's consistently large, that device's clock is faulty, and we now report this automatically.

After this incident, we also made clock behavior testable. Code that reads the time directly from the system can't be tested, so an injectable clock interface went into every service. In integration tests, we now skew clocks on purpose: the scenario "what happens if the report service is 5 seconds ahead of the event producer?" lives in a test file and runs on every pipeline run. When we first wrote these tests, three more tests broke, which means we found three more hidden assumptions about clocks. The most instructive was session timeout: when a session's "last activity" stamp was set on one server and checked on another, sessions could expire early by the amount of the clock difference. That turned out to be the explanation for customers' complaints that the app "logged me out for no reason." Discovering that the answer to a complaint that had come in on and off for years, one we had never been able to explain, was hiding in a 3.2-second offset is one of those moments in this profession that teach you humility.

The skew had another victim, the authentication layer, and we found it almost by accident. We use short-lived signed tokens for service-to-service calls, and the tokens have a "not valid before" field. A token generated by the server whose clock had drifted ahead was being rejected as "not yet valid" by its neighbor with the correct time. Those mysterious authorization errors that had shown up sporadically in the logs for months, which nobody had chased because they disappeared on retry, turned out to be the fingerprints of the clock difference. We added a 30-second tolerance to token validation, but we now know the real fix isn't that tolerance, it's the offset alert. Tolerance masks the symptom; the alert catches the disease.

The sentence that stays with me from this case is this: in a distributed system, the wall clock isn't a measurement, it's hearsay. Hearsay has its uses. It's good enough for logs, graphs and human reading, but in load-bearing places like ordering, duration calculations and claims of correctness, either use a single clock or use sequence numbers that don't trust clocks at all. And always, always monitor clock offset, so that the only thing that tells you on its own that a clock has broken isn't a customer report that violates the laws of physics. If you're seeing ordering oddities in your system that you can't explain, send me a message. The first place I'd look probably wouldn't be your code, but your clocks.