How Computers Keep Their Clocks Synchronized Over a Network
Follow the four timestamps that let a computer measure its own clock error across a network, and see what limits accuracy from milliseconds down.
Key takeaways
- Computer clocks are driven by inexpensive oscillators that can drift by a second or more per day if left alone.
- A four-timestamp exchange yields estimates of the clock offset and the network round-trip delay.
- The offset estimate assumes the outward and return trips take equal time, and any difference becomes an error.
- Time servers form layers called strata, starting from reference clocks such as atomic or satellite-fed sources.
- Precision systems use hardware timestamps to reach far finer agreement than ordinary internet synchronization.
A computer synchronizes its clock by asking a better clock for the time, measuring how long the question and answer took, and correcting for that delay. The most common method on the internet is the Network Time Protocol (NTP). By recording four timestamps in one exchange, your machine can estimate both how wrong its clock is and how long the round trip lasted.
The correction is then applied gently, usually by running the local clock a little fast or slow until it agrees. All of this happens in the background on most phones, laptops and servers.
Why computer clocks wander
Most computers count the vibrations of a quartz crystal. As explained in how a quartz watch keeps time, a crystal's frequency depends on how it was made and on temperature. An error of 20 parts per million is a reasonable illustration for an uncorrected machine.
20 × 10⁻⁶ × 86,400 s per day ≈ 1.7 s per day That drift would soon cause trouble. File timestamps decide which copy is newer. Security certificates are valid only between stated dates, and some login systems reject messages whose times differ too much. Engineers reconstructing a failure need logs from many machines to line up.
The delay problem
Simply asking "What time is it?" is not enough. The answer spends time in transit, and you do not know how much. If a server says 12:00:00 and the message took 80 milliseconds to arrive, setting your clock to 12:00:00 leaves you 80 milliseconds behind.
You cannot measure a one-way delay without already having synchronized clocks at both ends. You can, however, measure a round trip using only your own clock. That is the way out.
Four timestamps
In an NTP exchange, four moments are recorded:
- t1: the client sends a request, read from the client's clock.
- t2: the server receives it, read from the server's clock.
- t3: the server sends the reply, read from the server's clock.
- t4: the client receives the reply, read from the client's clock.
From these the client computes two quantities.
offset = [ (t2 − t1) + (t3 − t4) ] / 2
delay = (t4 − t1) − (t3 − t2) The delay is the total elapsed time on the client, minus the time the server spent holding the request. The offset averages the apparent differences seen in each direction, so that the travel time cancels.
A worked example
Say a client clock is exactly 0.500 seconds behind the server, and each direction takes 40 milliseconds. The server needs 5 milliseconds to respond. The client sends when its clock reads 10.000.
| Event | True server time | Recorded value |
|---|---|---|
| Request leaves client | 10.500 | t1 = 10.000 (client) |
| Request arrives | 10.540 | t2 = 10.540 (server) |
| Reply leaves | 10.545 | t3 = 10.545 (server) |
| Reply arrives | 10.585 | t4 = 10.085 (client) |
offset = [ (10.540 − 10.000) + (10.545 − 10.085) ] / 2
= [ 0.540 + 0.460 ] / 2
= 0.500 s
delay = (10.085 − 10.000) − (10.545 − 10.540)
= 0.085 − 0.005
= 0.080 s The client recovers the true offset and the 80 millisecond round trip.
When the two directions differ
Now keep the 80 millisecond round trip but let the request take 60 milliseconds and the reply 20. Then t2 becomes 10.560 and t3 becomes 10.565, while t4 stays at 10.085.
offset = [ 0.560 + 0.480 ] / 2 = 0.520 s The estimate is off by 20 milliseconds, which is half the difference between the two travel times. No amount of arithmetic on these four numbers can reveal the imbalance. The error cannot exceed half the round-trip delay, so a short path to a nearby server is valuable.
Clients therefore take many samples and favor those with the smallest delay, which are least likely to have waited in a congested queue. Filtering noisy samples this way relates to the broader topic of signal and noise. Averaging helps with random scatter but not with a constant asymmetry.
Layers of servers
NTP, developed by David Mills and colleagues starting in the 1980s, arranges clocks in levels called strata.
| Stratum | What it is |
|---|---|
| 0 | A reference source, such as an atomic clock or satellite navigation receiver |
| 1 | A server connected directly to a reference source |
| 2 | A server or client synchronized over the network to stratum 1 |
| 3 and higher | Each further step away from the reference |
A client usually consults several servers. An algorithm compares their answers, sets aside any that disagree with the majority and combines the rest. With only one source you cannot tell if it is wrong. With two that disagree you cannot tell which. Several independent ones give you a vote.
The reference at the top traces back to national laboratories and the international time scale described in UTC and atomic time.
How often to ask
A client faces a trade-off. Asking frequently catches changes in its own crystal quickly, but adds traffic and lets network jitter, the random variation in delay, dominate the result. Asking rarely averages the jitter over a long baseline but lets temperature-driven drift build up unnoticed.
Suppose each offset measurement is uncertain by about 5 milliseconds. You want to learn the crystal's frequency error by comparing two measurements.
Measurements 100 s apart: about 7 ms ÷ 100 s ≈ 70 ppm uncertainty
Measurements 10,000 s apart: about 7 ms ÷ 10,000 s ≈ 0.7 ppm uncertainty The 7 milliseconds comes from combining two independent 5 millisecond uncertainties, √(5² + 5²) ≈ 7. Over the short interval the noise swamps a 20 ppm drift. Over the long one the drift is measured well, provided it stays constant that long.
NTP software therefore adjusts its polling interval, typically from about a minute up to many minutes, lengthening it when the clock behaves well. The question of the best averaging time is exactly what Allan deviation was designed to answer.
Steering without jumping
Once it knows the offset, the client prefers to slew: to speed up or slow down the clock slightly until the error disappears. Time then never runs backward, which protects software that assumes it will not. A large error, such as after a machine has been switched off for a long period, may be fixed with a single step.
The software also learns the crystal's typical frequency error and compensates for it continuously. A machine that loses its connection for a while therefore drifts much less than an untrained one.
Programs measuring how long something took should use a monotonic clock, one that only moves forward and is not reset by synchronization. Subtracting two wall-clock readings can give strange results if an adjustment or a leap second fell between them.
Doing better than milliseconds
Over the public internet, accuracy is typically in the range of milliseconds to tens of milliseconds, and better on a quiet local network. Much of the uncertainty comes from variable waiting inside computers and network equipment.
The Precision Time Protocol (PTP), standardized as IEEE 1588, attacks this by stamping packets in network hardware, very close to the wire, and by having switches account for the time packets spend inside them. On suitably equipped networks it can reach agreement below a microsecond. It is used in areas such as telecommunications, industrial control, power systems and financial trading.
Another option is to give each machine, or each site, its own satellite navigation receiver. That bypasses network delay but needs an antenna with a view of the sky and some defense against interference.
Trust and order
A false time signal can make expired credentials look valid or scramble records. Network Time Security, published as an internet standard in 2020, adds cryptographic checks so a client can confirm which server replied and that the message was not altered. Authentication does not stop an attacker from delaying packets, so multiple sources and sanity limits still matter.
Some software problems need only the order of events, not the true time. In 1978 Leslie Lamport described logical clocks, simple counters passed along with messages, which guarantee that a cause receives a smaller number than its effect. Many distributed systems combine such counters with physical time.
Virtual machines and sleeping devices
Some environments make the job harder. A virtual machine is a computer simulated inside another. It can be paused or moved, and its sense of elapsed time may jump when it resumes. Virtualization systems offer special clock sources and host assistance to reduce the problem.
Battery-powered devices sleep to save energy. While asleep, many rely on a small, low-power clock chip and then resynchronize on waking. Tiny sensors in a field may synchronize only with their neighbors by radio, aiming for agreement among themselves more than with world time.
In every case the same two questions apply. How quickly does the local oscillator drift, and how accurately can it be compared with something better? The answers set how often a comparison is needed to stay within a chosen limit.
Illustration: drift 20 ppm, tolerance 10 ms
Time to reach the limit = 0.010 s ÷ (20 × 10⁻⁶) = 500 s A device that learns and removes most of its drift can wait far longer between comparisons.
Checking your own machine
Your operating system can usually report which time source it uses and when it last synchronized. If your clock is far off, confirm that automatic time setting is switched on, check the time zone separately, and make sure the network allows the time traffic. A wrong zone looks like a clock error of whole hours, while a failing sync shows up as slow drift. Zone rules are explained in how time zones were created.