By Yasin

Time is something that people take for granted without knowing how it actually works. Every day we rely on our smart devices, watches, and digital or quartz clocks to see the time and date.

When you buy a new device and boot it for the first time, you expect that machine to adjust the time to your timezone automatically, right? Have you ever wondered how they synchronize? Have you ever thought about how they communicate in the wild and get the accurate time?

I'm trying to keep this article as digestible as possible. I will not go much into technical details because understanding how time works takes time, and it is fun, and I'm here to inspire you to start your journey with me. This series will have multiple articles, so this is just the beginning.

# Tick Tock... Tock Tick?!

Clocks may seem simple because we have been taught how to measure and interpret time with them, but they are actually more complex than they appear. We often overlook the complexity of the technology and mechanisms that make clocks accurate, but it is this complexity that allows us to use them effectively.

Ever heard about clock Skewing? Drifting? UTC? But before jumping into the intricate details, let's look at some clock types. It's crucial to understand different clock types.

## Physical Clocks

Physical clocks are devices that measure and display the passage of time using mechanical or electronic movements. They can take many forms, including wristwatches, wall clocks, and grandfather clocks. Physical clocks typically have hands or other moving parts that indicate the time and may also have additional features such as alarms or stopwatches.

## Digital Clocks

Digital clocks display the time in numerical form, typically using electronic displays. They resemble physical clocks but do not have physical hands or other moving parts to show the time. Instead, they use electronic circuits and arrays to show the time in hours, minutes, and seconds.

Digital clocks can take many forms, including wristwatches, wall clocks, and alarm clocks. They are often more accurate and easier to read than physical clocks, and they may also have additional features such as alarms, countdown timers, and stopwatches.

## RTC Clocks

RTC stands for "real-time clock," which refers to a type of clock or timing device used to keep track of the current time, even when the power is turned off or the device is not connected to a power source. RTC clocks are often used in computers, mobile phones, and other electronic devices (motherboards) to keep track of the time and date when the device is not in use.

They are typically built using a crystal oscillator, a type of oscillator that uses the vibrations of a crystal to generate a stable, accurate timing signal. RTC clocks are often powered by a small battery or another energy source, which allows them to maintain the correct time even when the device is turned off or disconnected from a power source.

### WTF is Quartz Crystal?

Quartz crystal is a naturally occurring mineral composed of silicon dioxide, which is found in the Earth's crust and is known for its piezoelectric properties, meaning it can produce an electrical charge when subjected to mechanical stress.

### Quartz Crystal in RTC Clocks

Quartz clocks use an electronic circuit to drive the quartz crystal oscillator at a specific frequency. The oscillator generates an electrical signal to operate a motor or other mechanical device, which uses the clock's hands or other timing mechanisms. The accuracy of a quartz clock is determined by the stability of the quartz crystal oscillator and the quality of the electronic circuit used to drive it.

At the core, it works by using the piezoelectric properties of a quartz crystal to generate a stable, accurate timing signal. Piezoelectricity is the ability of certain materials, including quartz, to generate an electrical charge in response to mechanical stress. When a voltage is applied to a quartz crystal, it will deform slightly, and when the voltage is removed, the crystal will return to its original shape. This deformation returns to its form to create a mechanical oscillation, or vibration, at a specific frequency.

The frequency of the oscillation is determined by the physical properties of the quartz crystal, including its size, shape, and type of crystal lattice structure it has. When the crystal is cut and shaped specifically, it oscillates at a particular frequency when a voltage is applied. This frequency can be calculated using the following equation:

For context, in this equation, $f$ is the oscillation frequency, $k$ is the crystal's elastic constant, and $m$ is the mass of the crystal. The oscillation frequency can be adjusted by changing the crystal's size, shape, or other physical properties or applying a different voltage.

So, in a quartz clock, the oscillations of the crystal are used to drive a motor or other mechanical device, which in turn causes the hands of the clock or other timing mechanisms. The clock's accuracy is determined by the stability of the crystal oscillator and the quality of the electronic circuit used to drive it.

#### Time is Beautiful

If you're curious, you'd like learn about Harmonic Motion and understand how it obeys the Hooke's Law.

It is worth to note that, quartz clocks are generally more accurate than mechanical or physical clocks, which use a spring or weight to drive the movement, and they are also more resistant to temperature changes and other environmental factors.

## GPS Clocks

GPS clocks are clocks that use signals from the Global Positioning System (GPS) to set and maintain accurate time. GPS is a satellite-based navigation system operated by the US government and is used to determine the location and time of a device on Earth. GPS clocks are often used as a more convenient and accurate alternative to manually setting the time, and they are commonly used in various settings, including homes, offices (banks), and public places (train stations, airports).

GPS clocks work by receiving a time signal from a GPS satellite and using it to set and maintain the accurate time on the clock. GPS satellites transmit time signals as part of their navigation data, and GPS clocks have a built-in receiver that can pick up these signals and use them to set and maintain the time on the clock.

There are several advantages to using GPS clocks. They are highly accurate and can synchronize their time with an exact atomic reference source. In addition, GPS clocks can automatically adjust for changes in daylight saving time, which means their settings do not need to be manually adjusted when the time changes. However, GPS clocks require a clear view of the sky to receive the time signals from the GPS satellites.

Radio clocks, or radio-controlled clocks, receive time signals from a remote transmitter, typically through a radio frequency connection. These signals are used to set and maintain the accurate time on the clock. Radio clocks are also often used as a more convenient and precise alternative to manually setting the time, and they are also commonly used in various settings, just like GPS clocks.

Radio clocks work by receiving a time signal from a remote transmitter and using it to set and maintain the accurate time on the clock. These signals are typically transmitted using a radio frequency and are often encoded with additional information such as the date and location. Radio clocks have a built-in receiver that can pick up the time signal and use it to set and maintain the time on the clock. Several radio clocks are available such as analog, digital, smart (with LCD displays), atomic, and GPS. Each type of radio clock has features and capabilities and can be used in various settings.

## Atomic Clocks 👑

Atomic clocks are extremely accurate clocks that use the natural resonance frequency of atoms as a time standard. They are the most accurate clocks used as the primary standard for time and frequency measurements.

Atomic clocks work by using the energy levels of atoms as a reference point. Atoms or these particles have natural resonance frequencies at which they will absorb or emit electromagnetic radiation, and these frequencies are incredibly stable and reliable. Atomic clocks use this resonance frequency as a time standard by comparing it to a known reference frequency.

There are several atomic clocks, including cesium, rubidium, and hydrogen. Cesium clocks are the most accurate and are used as the primary standard for time and frequency measurements in many mission-critical systems. They work by using the resonance frequency of Cesium-133 at 9,192,631,770 Hz atoms as a time standard.

To measure time using an atomic clock, a beam of cesium or rubidium atoms is first generated and then passed through a resonant cavity, which is a small, enclosed space that is designed to resonate at the natural frequency of the atoms. As the atoms pass through the cavity, they absorb and emit electromagnetic radiation, which causes the atoms to oscillate at their resonant frequency.

Then, the resonant frequency of the atoms is then measured using a technique called spectroscopy, which involves measuring the absorption and emission of electromagnetic radiation by the atoms. By comparing the resonant frequency of the atoms to a reference frequency, it is possible to determine the passage of time with a high degree of accuracy.

Atomic clocks are used in various applications, including time and frequency standards, aircraft navigation systems, and military and scientific research. They are also used as reference clocks to set the time on many primary time-servers that help electronic devices, such as smartphones and computers, ensure that the time displayed on these devices is accurate.

More on Atomic Clocks: -

# Why Synchronize Time?

In a computer network or in a device, having an inconsistent time, even seconds or microseconds, can cause catastrophic problems that are unforeseen by engineers.

Take the following example: say we have four servers in a private network that is interconnected. Imagine that something has happened to the link that connects Server 1 in this ring topology.

Now, in the recovery phase, say we need to analyze its logs to correlate to see what caused it to go down. Now, imagine that these individual server clocks were out of sync. In this case, if we want to get accurate log information, when and at what time Server 1 went down using Server 2 logs, it could be hard to correlate their logs and deduce a logical reason, thus making it complicated.

As you can see, when dealing with multiple distributed network systems like this, it becomes hairy real quick if we don't synchronize their time appropriately. Because of this, every device has an internal clock that keeps track of the time and date to maintain precision. Why?

BECAUSE time usually just advances. If we have multiple communicating programs running on a network of computers, time still should increase if you switch from one computer to another. If one system is ahead of the others, the others are behind that one. It's an undesirable effect for both humans and computers.

Consequently, isolated networks may run their own inaccurate time. But every computer nowadays connects to the Internet. And as soon as you connect to the Internet, the effects will be perceptible.

Imagine you sent an E-mail message to a recipient, and the recipient received it five minutes before it was sent, and there even was a reply for you two minutes before the message was sent. That'll be super confusing, lol.

Even on a single computer, applications will have trouble when the time jumps backward or forwards. For instance, database systems using ACID transactions and crash recovery like to know the time of the last good state to roll back and prevail the data.

So, this is why we need to synchronize time. And it is where NTP comes into play, and it's more than just a protocol. And I believe every Software Engineer should know about NTP.

# Network Time Protocol (NTP)

Computer scientists like to model timekeeping in a distributed computer network as a happens-before relation because in our universe, the arrow of time constantly increases, and nothing travels faster than the speed of light (Davis Mills, 2011). If such a message contains the sending time, then the receiving time on arrival must always be later (Leslie Lamport, 1978).

In a nutshell, NTP is a distributed service that synchronizes the computer clock to an ensemble of sources, either remotely via the Internet or locally via radio, satellite, or telephone modem service that has an accurate time.

NTP aligns the system clocks in participating computers to Coordinated Universal Time (UTC) used by most world nations. I won't discuss Solar day, Earth's rotation around the sun, or the freaking Gregorian calendar in this article. You should understand that the UTC timescale adheres to International Atomic Time (TAI) by inserting leap seconds at intervals of about two years.

NTP is the backbone technology that allows periodic national standard time dissemination throughout the Internet and interconnected private and corporate networks. Scientists and engineers have developed complex statistical algorithms for NTP to improve clock synchronization accuracy and reduce clock drift.

These algorithms consume considerable memory and processing power to deliver the ultimatum. The ultimate goal of NTP is to synchronize the clocks in all participating computers to the order of less than a millisecond or two; relative to UTC.

# How does NTP Work?

The NTP algorithm uses a hierarchical system of time-servers to distribute accurate time information to client computers. At the top of the hierarchy are several primary time-servers connected to highly accurate reference clocks such as GPS receivers or atomic clocks. These primary time-servers are responsible for maintaining the most precise time available.

Secondary time-servers are connected to the primary time-servers and are responsible for distributing the accurate time to client computers. Client computers use NTP to synchronize their time with the secondary time-servers, which synchronize their time with the primary time-servers with a stratum value of 1. This allows the client computers to maintain accurate time even when they are not connected to a stable reference clock with a stratum value of 2.

## Reference Clocks... Why so Accurate?

Reference clocks, such as atomic clocks, are based on the quantum-mechanical properties of some aspects like Caesium. For example, the NIST-F1 cesium atomic clock in Colorado, USA is so accurate it said that the clock would only lose one second in 100 million years. Because it measures time by monitoring the frequency radiation of atoms. But you might ask why not use it on devices, then, huh?

# How Synchronization Works?

With NTP, clients can easily synchronize their clocks with a server by sending an NTP request message to the server and receiving an NTP response message in return. The client can then use the time information in the response message to adjust its clock to match the server's clock within a clock skew of 10ms. NTP uses complex algorithms*to compensate for network delays and other factors affecting time synchronization accuracy.

NTP uses a combination of algorithms and data from multiple sources to calculate the correct time and determine any skewing or drift in the clock of a particular computer.

When an NTP client starts up, it requests updates from multiple servers for the current time. And all these NTP servers respond with the current time and include information about the round-trip delay of each request and the offset between the server's clock and the true time. The client uses this information to calculate its own clock's drift and adjust it accordingly.

If you want more details on this, refer to the article "Physical Clock Synchronization" written by Pratik Pandey. In his series of articles, he explained how skewing, slewing and stepping work briefly. He also mentioned that, in some situations, even a clock skew of 10ms is not acceptable. In these cases, PTP (Precision Time Protocol) can be used to provide synchronization with a precision of less than 1 microsecond.

# Modes of NTP

Since, NTP is built on a hierarchical system it uses several operation modes to exchange information about the current time between computers, depending on the use case. These modes are indicated by a "mode number," included in the NTP messages exchanged between computers. Here is a brief summary of the different methods:

1. Mode 1 (symmetric active): This mode is used when a client wants to synchronize its clock with a server and can actively participate in the synchronization process. The client sends a request to the server, and the server responds with its current time.

2. Mode 2 (symmetric passive): This mode is similar to mode 1, but the client cannot actively participate in the synchronization process. The client listens for broadcasts from the server and adjusts its clock based on the received information.

3. Mode 3 (client): This mode is used when a client wants to request the current time from a server. The client sends a request to the server, and the server responds with its current time and other information about its clock.

4. Mode 4 (server): This mode is used by a server to respond to a client request for the current time. The server sends a message to the client with its current time and other information about its clock.

5. Mode 5 (broadcast): This mode is used by a server to broadcast its current time to all clients on a network. The server sends a message containing its current time and other information about its clock to all listening clients.

We can further generalize these modes to four joint operations widely used in the industry.

1. Client-Server Mode
2. Symmetric Peers Mode
4. Multicast Mode

NTP offers various operation modes that can be selected depending on the network's specific needs. When the IP address of the NTP server or peer is unknown and multiple devices need to be synchronized, the broadcast or multicast mode can be used. In contrast, the client/server and symmetric peers modes involve synchronization with a specific server or peer. By choosing the appropriate mode, the reliability of clock synchronization can be improved. Now, I will briefly explain the modes and how they operate on a high-level note.

## Client-Server Mode

When operating in client/server mode, a client sends a clock synchronization message to servers with the mode field in the message set to 3 (client mode). Upon receiving the message, the servers automatically operate in server mode and send a reply, with the Mode field in the messages set to 4 (server mode).

Upon receiving the responses from the servers, the client performs clock filtering and selection and synchronizes its local clock to the optimal reference source. In client/server mode, a client can be synchronized to a server but not vice versa.

## Symmetric Peers Mode

In symmetric peers mode, devices that operate in symmetric active mode and symmetric passive mode exchange NTP messages with the Mode field 3 (client mode) and 4 (server mode). Then the device that operates in symmetric active mode periodically sends clock synchronization messages, with the Mode field in the messages set to 1 (symmetric active).

Then, the device that receives the messages automatically enters symmetric passive mode and sends a reply, with the Mode field in the message set to 2 (symmetric passive). The symmetric peers' mode is established between the two devices by exchanging messages. Then, the two devices can synchronize or be synchronized by each other. If the clocks of both devices have been synchronized, the device whose local clock has a lower stratum level synchronizes the clock of the other device.

In broadcast mode, a server periodically sends clock synchronization messages to broadcast address 255.255.255.255, with the Mode field in the messages set to 5 (broadcast mode). Clients listen to the broadcast messages from servers. When a client receives the first broadcast message, the client and the server exchange messages, with the Mode field set to 3 (client mode) and 4 (server mode) to calculate the network delay between the client and the server.

Then, the client enters the broadcast client mode, continues listening to broadcast messages, and synchronizes its local clock based on the received broadcast messages.

## Multicast Mode

In multicast mode, a server periodically sends clock synchronization messages to the user-configured multicast address or, if no multicast address is configured, to the default NTP multicast address 224.0.1.1, with the Mode field in the messages set to 5 (multicast mode). Clients listen to the multicast messages from servers.

When a client receives the first multicast message, the client and the server exchange messages, with the Mode field set to 3 (client mode) and 4 (server mode) to calculate the network delay between the client and the server. Then, the client enters multicast client mode, continues listening to multicast messages, and synchronizes its local clock based on the received multicast messages.

# How to Simulate NTP

Simulating NTP is challenging. But lucky for us, the authors of the Network Time Protocol have implemented a program called ntpdsim to emulate and play with the protocol in a configurable way. It is available via the official ntp distribution. However, I won't show you how to tweak and play with the configuration in this article. Instead, I will lay out the foundation and a starting point, so you can experiment. In an upcoming article, I will show you how to simulate all the different modes NTP operates in.

## Installing NTP

We must avoid using package managers to install NTP distribution, as most build the source without the simulation flags. We can use the development distribution to enable the ntpdsim program. First, let's go ahead and download the NTP development distribution.

Then navigate to that folder using cd ntp-dev-4.3.99.

## Configure & Build NTP

Let's tell the configure-r to enable the simulator for us.

Then, let's compile the NTP distribution.

## Sample Configuration

Create a ntp.conf file and paste into that configuration file to create sample clocks. You can place this file inside the ntpd/ folder or just move it to /etc/conf and point the correct path at the execution time.

## Invoking the Simulator

Then, navigate to the ntpd/ folder and execute the following.

And see the magic happening. The above ntpdsim configuration file simulates two servers, each of which has two acts. Assuming you're a hacker, you can run ./ntpdsim --help to see the available arguments. You can configure skew, authentication, wait time, clock drift, and many more. The following article of this series will cover these commands in-depth.

# Conclusion

Well, that's it folks! If you made it all the way here, thank you! I admire you. We learnt quite a bit about clocks, time, and how we synchronize clocks in computers. So, in summary we learnt that: -

• To measure and display the passage of time we have various types of clocks, including physical clocks with mechanical or electronic movements, digital clocks with electronic displays, atomic clocks, and real-time clocks used in electronic devices to keep track of the time and date.

• Synchronizing time is important in computer networks and devices to avoid issues with log analysis, communication between programs, and database transactions.

• The Network Time Protocol (NTP) is a service that helps to synchronize the clocks of participating computers to Coordinated Universal Time (UTC) by using complex algorithms and referencing external sources. It is important to maintain accurate time synchronization to prevent confusion and ensure the smooth operation of systems.

Until next time !

Published