How NTP Achieves High Precision Using UDP
The Network Time Protocol (NTP) achieves sub-millisecond precision over variable-latency networks by combining the minimal latency of the User Datagram Protocol (UDP) with a four-timestamp exchange algorithm and advanced statistical filtering. By operating over UDP, NTP avoids the transmission delays, connection handshakes, and retransmission overhead inherent in connection-oriented protocols like TCP. This foundational transport efficiency allows NTP clients and servers to accurately calculate round-trip network delays, estimate local clock offsets, and dynamically adjust system clocks without introducing latency artifacts.
The Advantage of UDP Over TCP
NTP operates over UDP port 123. TCP requires a three-way handshake, maintains connection states, and automatically retransmits lost packets. These mechanisms introduce unpredictable queuing delays, jitter, and head-of-line blocking, which corrupt precise timing measurements.
UDP is connectionless and lightweight. It transmits packets immediately without session negotiation or acknowledgment waits. If an NTP packet is lost or delayed, the protocol simply drops that sample and relies on subsequent polls rather than waiting for retransmissions. This ensures every processed time sample represents the most direct, real-time path through the network.
The Four-Timestamp Handshake
To neutralize the variable network transit times inherent to packet-switched networks, NTP uses an exchange of four distinct timestamps:
- \(T_1\) (Origin Timestamp): The client records the local time when the NTP request packet is sent.
- \(T_2\) (Receive Timestamp): The server records its local time upon receiving the request.
- \(T_3\) (Transmit Timestamp): The server records its local time when sending the reply packet.
- \(T_4\) (Destination Timestamp): The client records the local time upon receiving the reply.
Client Server
| |
|--- T1 (Client Send) ---->|
| |--- T2 (Server Receive)
| |--- T3 (Server Transmit)
|<-- T4 (Client Receive)---|
| |
Calculating Round-Trip Delay and Clock Offset
Using these four values, the client calculates two critical metrics under the baseline assumption that network path latency is symmetrical:
Round-Trip Delay (\(\delta\)): The total transit time of the packet across the network, excluding the server’s processing time: \[\delta = (T_4 - T_1) - (T_3 - T_2)\]
Clock Offset (\(\theta\)): The actual difference between the client’s clock and the server’s clock: \[\theta = \frac{(T_2 - T_1) + (T_3 - T_4)}{2}\]
By subtracting the server’s internal turnaround time (\(T_3 - T_2\)) from the total elapsed time (\(T_4 - T_1\)), NTP isolates pure network transit delay, allowing precise offset corrections down to milliseconds over the public internet.
Statistical Filtering and Clock Discipline
A single measurement can still be skewed by asymmetric routing or temporary network congestion. NTP mitigates this by maintaining a sliding window of recent timestamp exchanges and applying statistical algorithms:
- Clock Filter Algorithm: Selects the samples with the lowest round-trip delay, as lower delays have mathematically lower potential error margins.
- Intersection Algorithm (Marzullo’s Algorithm): Cross-references multiple NTP time sources (peers or higher-stratum servers) to identify and discard faulty or deceptive clocks (“falsetickers”).
- Clock Discipline (Phase-Locked and Frequency-Locked Loops): Rather than abruptly jumping the client clock—which can disrupt running applications—NTP gradually slews the local system clock speed until it smoothly converges with the reference time.
Hardware Timestamping Integration
In modern high-precision deployments, NTP bypasses operating system kernel delays through hardware timestamping. Network Interface Cards (NICs) capture the \(T_1\) through \(T_4\) timestamps at the physical layer (PHY) as the UDP packet enters or exits the network port. This eliminates interrupt latency and OS scheduling jitter, allowing NTP over UDP to achieve microsecond-level accuracy.