Sincronización de tiempo usando el protocolo NTP

Este artículo trata sobre cómo sincronizar el tiempo usando el protocolo NTP.

Structure of time sources (Stratum)
The NTP protocol uses a hierarchical structure of time sources. The originate time source, which consists of high-precision timekeeping devices such as atomic clocks, is called Stratum 0. The next level (Stratum 1) are servers whose system time is synchronized with a Stratum 0 time source. The next level (Stratum 2) has synchronized the time with Stratum 1 servers. That goes until Stratum 15.

Structure of time sources

How it works

1. The client application sends a data packet to the server, which consists mainly of 4 timestamp. The first timestamp, in our example T1, is set by the client application with the time of the client side.

2. The server receives this data packet and sets the second timestamp, in our example T2. The package will then be set to a queue for further processing.

3. The server sets the third timestamp, in our example T3 and sends the data packet back to the client application.

4. The client application receives this data packet and sets the fourth timestamp, in our example T4.

NTP protocol

The round trip delay (Delay)
The round trip delay briefly referred as delay, is important to calculate the time difference between server and client. The delay consists of the time between T1 and T4 with the exception of the time between T2 and T3. The formula looks like this:

(T4 - T1) - (T3 - T2)

The time difference (Offset)
The time difference is briefly referred as offset. Since the timestamp (T4) of the server is no longer up to date, when receiving the data packet, the time has to be added, which the data packet needed, to get from the server to the client. Suppose the time between client and server is approximately the same as from the server to the client, we can divide the delay by 2 and get the time, that have to be added to T4. The whole formula looks like this:

((T2 - T1) + (T3 - T4)) / 2