liveBook · Manning

NTP

As explained in Rust in Action: Systems programming concepts and techniques

NTP is a network protocol for synchronizing computer clocks using timestamp exchanges between clients and time servers.

Network Time Protocol (NTP) helps computers synchronize their clocks with time servers. A simple client sends a request, reads the reply, and estimates how far its local clock differs from the server’s. Repeating that process lets a clock move toward agreement over time. section 9.9 section 9.9.2

One request and response produce four timestamps:

  • T1: the client sends the request; T2: the server receives it. section 9.9.1
  • T3: the server sends its reply; T4: the client receives it. section 9.9.1

The four timestamps let the client estimate both clock offset (θ) and network delay (δ). A large delay makes a reading less reliable: the reply took longer to travel, so the client has less confidence in its comparison. There is no directly accessible, universal “right” time—only readings to compare. section 9.9.1 section 9.9.2

Table 9.2 How to calculate δ and θ in NTP (view table figure)

δ = (T4 – T1) – (T3 – T2)

(T4 – T1) calculates the total time spent on the client’s side. (T3 – T2) calculates the total time spent on the server’s side.

The distinction between the two differences (e.g., δ), is an estimate of the difference between the clocks, plus a delay caused by network traffic and processing.

θ = ( (T2 – T1) + (T4 – T3) ) / 2

We take the average of the two pairs of timestamps.

Use the timestamp formulas to calculate the estimated delay and offset for a round trip. section 9.9.2

A clock at boot

A Raspberry Pi without a battery-backed real-time clock starts with its system clock set to epoch time and uses NTP during boot to find the current time. Without a network connection, that approach cannot supply an accurate timestamp; an added real-time clock is one alternative. section 9.1

A practical client can query several trusted servers rather than rely on one reply. It can favor quicker responses and limit how much any adjustment changes the local clock. NTP also has an always-on mode in which computers continually work toward agreement. section 9.9.2 section 9.9

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Works through request and response timestamps, offset, delay, and clock adjustment; leaves fuller protocol error handling aside.
Examples
Uses a Rust client’s four-timestamp exchange and a Raspberry Pi that needs time at boot.
What it assumes
Assumes some Rust familiarity while introducing the clock behavior and NTP timestamp exchange needed for the client.
What you can do after
Build it from scratch as a request-response client that compares server time and makes cautious local-clock adjustments.
How it connects
Connects clock synchronization to system time, real-time clocks, network latency, and distributed systems.

Also explained in

Compare how 2 books explain NTP →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.