Imagine you’re running a relay race – but every runner has a slightly different wrist-watch, and the track marshal’s stopwatch sometimes jumps backward.
That’s basically what computers face when they try to agree on time across a network.
Distributed programs constantly ask questions like:
To answer those, you need a clock – but there isn’t one single clock. Each machine owns its own little quartz oscillator that:
| Clock type | What it tells you | Safe use-case | Hidden trap |
|---|---|---|---|
Time-of-day clock (“wall-clock”, System.currentTimeMillis()) |
A calendar date & time | Show a timestamp to users | Can go backwards (NTP correction, leap-second, VM resume) – never measure durations with it |
Monotonic clock (System.nanoTime() / CLOCK_MONOTONIC) |
An ever-increasing counter with no real-world meaning | Measure elapsed time (timeouts, latency) | Values from two machines are unrelated – can’t order events across nodes |
Rule of thumb:
Use monotonic for how long, wall-clock for when.
Programs often run ntpd or chronyd, which: