diagram.mmd — sequence
NTP Time Sync sequence diagram

NTP (Network Time Protocol, RFC 5905) is the protocol used to synchronize the clocks of computers over a network, achieving accuracy within a few milliseconds of UTC on local networks and tens of milliseconds over the public internet.

Accurate time is foundational to distributed systems: TLS certificate validity, JWT expiration, log correlation, distributed tracing, database ordering, and cryptographic protocols all depend on synchronized clocks. Clock drift of even a few seconds can cause authentication failures, event ordering bugs, and security vulnerabilities.

Stratum Hierarchy: NTP uses a hierarchical, layered architecture called strata. Stratum 0 devices are reference clocks — GPS receivers, atomic clocks, or radio signals like WWVB. Stratum 1 servers (e.g., time.cloudflare.com, pool.ntp.org) are directly connected to stratum 0 devices. Stratum 2 servers synchronize from stratum 1, and so on. Clients typically use stratum 2 or 3 servers.

Client-Server Exchange: NTP uses UDP port 123. The client sends a request with its current timestamp (T1). The server records the arrival time (T2), processes the request, and records the send time (T3). The client records the arrival time (T4). From these four timestamps, both the round-trip delay ((T4-T1) - (T3-T2)) and clock offset (((T2-T1) + (T3-T4)) / 2) can be computed.

Offset Correction: The client uses the computed offset to adjust its local clock. NTP applies adjustments gradually (slewing) rather than jumping the clock, to avoid disrupting time-sensitive applications. Adjustments larger than 128ms trigger a step change.

Modern alternatives like Roughtime and Chrony improve on NTP's security and accuracy. Google and Amazon operate their own NTP infrastructure with leap-second smearing for cloud workloads.

Free online editor
Edit this diagram in Graphlet
Fork, modify, and export to SVG or PNG. No sign-up required.
Open in Graphlet →

Frequently asked questions

NTP (Network Time Protocol) synchronizes computer clocks over a network to within milliseconds of UTC. Accurate time is essential for distributed systems because TLS certificate validation, JWT token expiry, log correlation, distributed tracing, database transaction ordering, and security protocols all depend on clocks being synchronized within acceptable tolerances.
The client records its send time (T1) and sends a UDP request to the server. The server records the arrival time (T2) and the send time of its response (T3). The client records the arrival time (T4). From these four timestamps, the client computes the round-trip delay `(T4-T1) - (T3-T2)` and the clock offset `((T2-T1) + (T3-T4)) / 2`, then adjusts its local clock accordingly.
The NTP stratum hierarchy describes proximity to a reference clock. Stratum 0 devices are primary reference clocks (GPS receivers, atomic clocks). Stratum 1 servers are directly connected to stratum 0 devices. Stratum 2 servers synchronize from stratum 1, and so on. Most servers and workstations sync from public stratum 2 servers in the pool.ntp.org hierarchy.
mermaid
sequenceDiagram participant RefClock as Reference Clock (Stratum 0) participant NTPServer as NTP Server (Stratum 1) participant Client as NTP Client RefClock-->&gt;NTPServer: Continuous time signal\n(GPS / atomic clock) note">Note over NTPServer: Synchronized to UTC note">Note over Client: Record T1 = client send time Client->&gt;NTPServer: NTP Request (UDP port 123)\nOriginTimestamp=T1 note">Note over NTPServer: Record T2 = server receive time note">Note over NTPServer: Process request note">Note over NTPServer: Record T3 = server send time NTPServer-->&gt;Client: NTP Response\nOriginTimestamp=T1\nReceiveTimestamp=T2\nTransmitTimestamp=T3 note">Note over Client: Record T4 = client receive time note">Note over Client: Round-trip delay = (T4-T1)-(T3-T2) note">Note over Client: Clock offset = ((T2-T1)+(T3-T4))/2 note">Note over Client: Adjust local clock gradually (slew)\nif offset < 128ms, else step note">Note over Client: Repeat every 64-1024 seconds\n(poll interval adapts to stability)
Copied to clipboard