Physical Clocks and Their Limits

Topics Covered

Why Time Is Hard

Every machine has its own clock

Two kinds of clock on every machine

Why measuring a duration with wall time is a bug

No global now

What this lesson covers

Where time shows up in a distributed system

Precision versus accuracy

Reading the clock has a cost

A thought experiment

Why this matters for design

A quick self check

Ordering events is at the heart of distributed systems. Which write happened last? Did the payment happen before the refund? Has this lease expired? On one machine, these questions have easy answers, because there is one clock and one sequence of instructions. Across many machines, each with its own clock, the answers become surprisingly hard. This lesson looks at physical clocks, the hardware and protocols that try to agree on the time of day, explains why they can never agree perfectly, and shows what that means for systems that rely on them.

Every machine has its own clock

A server keeps time with a quartz crystal oscillator, the same technology found in wristwatches. The crystal vibrates at a nominal frequency, and the operating system counts vibrations to advance its clock. No two crystals vibrate at exactly the same rate. Manufacturing differences, temperature and age all change the frequency slightly. A typical server crystal is accurate to within tens of parts per million, which sounds small but adds up: an error of 50 parts per million is about 4 seconds per day. Two servers that start perfectly synchronized will disagree by seconds within a day if nothing corrects them.

This gradual divergence is called drift. The difference between two clocks at a given moment is called skew or offset. Clock synchronization protocols exist to measure and correct offset continually, but they can only reduce it, never eliminate it.

Every server clock ticks at a slightly different rate, so clocks that start together slowly drift apart unless something corrects them.

Two kinds of clock on every machine

Operating systems expose two different clocks, and confusing them causes real bugs. The time of day clock, also called the wall clock or real time clock, reports the current date and time, such as milliseconds since the Unix epoch. It is what you get from calls like System.currentTimeMillis in Java or time.time in Python. Because it is kept in step with real time by synchronization, it can jump: forward when the system discovers it was behind, backward when it was ahead, and around leap seconds. The monotonic clock, from calls like System.nanoTime or time.monotonic, counts elapsed time from some arbitrary starting point, such as boot. It never jumps backward, though its rate may be adjusted slightly. Its absolute value means nothing, and values from different machines cannot be compared.

The rule is simple. Use the monotonic clock to measure durations on one machine: timeouts, latencies, rate limits, how long a lease has been held. Use the time of day clock only when you need a human meaningful timestamp, and never assume it moves forward smoothly.

Why measuring a duration with wall time is a bug

Suppose code records the wall clock time when a request starts and subtracts it from the wall clock time when the request ends. If the synchronization daemon steps the clock backward by 200 milliseconds in between, the measured duration can be negative. If it steps forward, a fast request can appear to take seconds and trigger a timeout. Bugs like this appear in production as rare, inexplicable latency spikes, negative durations in metrics and leases that expire early or late. Cloudflare had a widely discussed outage in 2017 when a leap second made the wall clock go backward and code that assumed time only moves forward produced negative values that crashed a DNS service.

No global now

The deepest problem is not imprecise hardware. Even with perfect clocks, information takes time to travel. When server A reads its clock and sends the value to server B, the value is already out of date when it arrives, by an amount B cannot measure exactly because it does not know the one way network delay. Two machines therefore cannot know with certainty that their clocks agree. They can estimate the offset and bound the error, but every timestamp from another machine comes with uncertainty. This is why distributed systems treat time as a source of uncertainty to be managed, not a fact to be trusted.

What this lesson covers

The rest of the lesson explains how clocks are synchronized with NTP and PTP and what accuracy each achieves in practice, the failure modes of synchronized clocks such as steps, smears and leap seconds, the dangers of ordering events by timestamps, and the safe ways to use physical time: for leases with margins, for human facing timestamps and with explicit uncertainty bounds as Google Spanner does. The following lessons introduce logical clocks, which order events without trusting physical time at all.

Where time shows up in a distributed system

It helps to list the places where a typical system depends on time, because each one is a potential failure point. Timeouts and retries depend on elapsed time. Leases and leader election depend on elapsed time on two machines agreeing roughly. Caches expire entries by time to live. Rate limiters count requests per second. Certificates and tokens have validity windows, and a server with a clock far off rejects valid tokens or accepts expired ones. Logs, traces and audit records carry timestamps used for debugging and compliance. Databases may resolve conflicts or choose snapshot versions by timestamp. Scheduled jobs run at wall clock times. Some of these need only local elapsed time, which is easy to get right with the monotonic clock. Others compare times across machines, which is where the trouble lives.

Precision versus accuracy

Two words are often confused. Precision, or resolution, is how finely a clock can be read, such as nanoseconds. Accuracy is how close the reading is to true time. A clock can report nanoseconds and still be wrong by tens of milliseconds. Many APIs return timestamps with far more digits than they can justify, which tempts developers into trusting comparisons that the underlying synchronization cannot support. When comparing timestamps from different machines, the relevant number is the synchronization error, not the resolution of the clock API.

Reading the clock has a cost

Reading the clock is not free and not atomic with the work around it. A program that reads the time, then does some work, then writes a record stamped with that time has a gap between the reading and the effect, and a thread can be descheduled in that gap. On some virtualized platforms, reading the clock involves a trap to the hypervisor and is slow enough to matter in hot loops. These details rarely matter alone, but they add to the uncertainty of any timestamp and explain why careful systems treat a timestamp as a range rather than a point.

A thought experiment

Imagine two servers in different cities, each with a perfect atomic clock, synchronized once and never drifting. Even then, if a user in one city clicks a button at the same instant that a user in the other city clicks, which click came first depends on who is asking and how messages travel between them. Each server sees its own click immediately and the other one milliseconds later. Without a rule for ordering, the two servers can each believe their own event came first. Physics does not provide a global order for events that cannot influence each other in time; systems must impose one, and the next lessons show how.

Why this matters for design

The practical consequence is a habit of mind. Whenever a design compares two times, ask where each time came from, which clock produced it and how large the error between those clocks can be. If both came from the same monotonic clock, the comparison is sound. If they came from different machines, the comparison is only as good as synchronization, and the design must say what happens when synchronization is worse than expected. That single question catches a large share of time related bugs during design review.

A quick self check

Before moving on, try answering three questions about a system you know. Which components compare timestamps produced on different machines? What is the worst clock offset those machines have had in the last month, and is anyone measuring it? What happens if one machine jumps ten seconds into the future? If the answers are unclear, the rest of this lesson will show where to look.

Durations from monotonic time, dates from wall time

Every machine clock drifts, synchronization only bounds the error, and no two machines can know that their clocks agree exactly. Measure durations with the monotonic clock, use the time of day clock only for human meaningful timestamps, and treat every remote timestamp as uncertain.