Every machine has its own clock, and they never agree exactly.
The clock synchronization problem is simple to say: every machine has its own clock, no two clocks agree exactly, and in a distributed system you cannot fully trust any of them to say which event came first.
On one machine, ordering is easy: there is one clock, and it only moves forward. In a distributed system there are many machines, each with its own clock, and they talk over a network that has delays you cannot predict.
Two things go wrong. First, clock skew: at one instant, two clocks show different times. Second, clock drift: the gap keeps growing, because each crystal ticks at a slightly different speed. A common cheap crystal can be off by about 50 parts per million (ppm), which is roughly 4 seconds of error per day.
To fight this, machines ask a time server for the right time. NTP (Network Time Protocol) measures the round trip, assumes the delay was split evenly between the two directions, and adjusts the local clock. If the outgoing and returning delays are not equal, the clock ends up wrong by about half of the difference. Even after syncing, error is usually a few milliseconds on a good network and can be much worse on a bad one.
| Cause | What happens | Typical size |
|---|---|---|
| Drift | The crystal ticks a little too fast or slow, and the error grows over time | Around 10 to 200 ppm, so a few seconds a day |
| Initial offset | A machine boots with the wrong time | Can be seconds, minutes or more |
| Network asymmetry | NTP assumes equal delay both ways, but the paths differ | Half of the difference, often a few ms |
| Virtual machines | A paused or migrated VM wakes up with an old clock | Can be seconds |
| Leap seconds | UTC adds a second, and some software repeats or skips it | 1 second |
| Clock steps | A sync can jump the clock backward, so time seems to go back | Whatever the correction is |
| Approach | Idea | Cost |
|---|---|---|
| NTP | Sync over the internet or a local server | Cheap, but only milliseconds of accuracy |
| PTP (Precision Time Protocol) | Hardware timestamps in the network cards | Microsecond accuracy, needs special hardware |
| GPS / atomic clocks | Each data centre has its own accurate reference | Expensive, used by the biggest systems |
| TrueTime (Spanner) | A clock returns an interval [earliest, latest], and a commit waits out the interval | Waits a few ms per commit |
| Monotonic clock | A clock that never jumps backward, only used for measuring durations | Cannot compare across machines |
| Logical clocks | Order events by counters and messages, not by time | Not tied to real time |
Server B's clock was never synced. It started 200 ms behind true time and then keeps drifting.
A simplified model with a fixed formula and no random numbers. Clock drift of 1 ppm means 1 microsecond of error for every second. Real clocks also change speed with temperature and load, and real NTP errors vary from one exchange to the next.
end โ start using the wall clock can become negative if NTP steps the clock back in the middle. Use a monotonic clock for it.| Term | Meaning in easy words |
|---|---|
| Clock skew | The difference between two clocks at the same moment |
| Clock drift | The rate at which a clock slowly gets ahead of or behind true time |
| ppm | Parts per million. 50 ppm means 50 microseconds of error for each second |
| NTP | Network Time Protocol, which adjusts a clock using a time server |
| PTP | Precision Time Protocol, a hardware-assisted sync with microsecond accuracy |
| Wall clock | The time of day, which can jump when corrected |
| Monotonic clock | A clock that only moves forward, good for measuring durations |
| Last-write-wins (LWW) | A conflict rule that keeps the write with the largest timestamp |
| TrueTime | Google's clock API that returns a time range, not a single time |
| Leap second | An extra second added to UTC to keep it close to Earth's rotation |
| Happened-before | A cause-and-effect order between events that does not need clocks |
| Choice | We gain | We lose |
|---|---|---|
| Wall-clock timestamps | Real-world meaning and simple code | Wrong order when clocks differ |
| Logical clocks | Correct cause-and-effect order | No link to real time |
| Atomic clocks and GPS | Very small error | Cost and operations work |
| Waiting out uncertainty (TrueTime) | Strong ordering across the globe | Each commit waits a few ms |
| Last-write-wins | Very simple conflict handling | Lost updates when clocks are off |
| Frequent NTP sync | Smaller drift | More traffic, and visible clock steps |