Clocks: It’s not that simple!
A real-time clock (RTC) is a computer clock, usually in the form of an integrated circuit(IC) that is solely built for keeping time.
Refresher Articles:
In this series(Yes, this is a series as well because Clocks ain’t simple), we will talk about the different types of Clocks, how our computers & other devices can tell time and the challenges associated with tracking time in a distributed context & their solutions.
We’ve already seen in our consistency series that to establish strong consistency models, we need to establish an ordering between our operations, either total ordering or causal/partial ordering. But establishing ordering across distributed systems isn’t straightforward and that’s where we leverage clocks.
Let’s also think about use cases where we need ordering -
Distributed Data Stores for consistency reasons as already discussed.
Event-Driven Systems where any event can trigger a series of ordered calls to different services.
Business flows like placing an order on any website. There is an ordered flow for all of these executions irrespective of the number of devices you’re working with.
Logs. Logs are supposed to be ordered stream of events(even across services) & any out-of-order logs can cause a big headache during debugging.
There are different types of clocks that we can leverage to implement ordering & in this article, we’ll talk about the clock we know best -
Physical Clocks
Have you ever wondered how your computer/mobile phone can tell time? Have you wondered why most clocks are always in sync & why some are out of sync?
Computers and other devices like phones etc need to tell time & to achieve that, they leverage Clocks, to be precise Physical Clocks!
Physical Clocks, at least the ones used in our computers and devices are all digital clocks that are based on a vibrating quartz crystal. Quartz clocks are extremely famous and are used on everything ranging from our wristwatches to computers and microwave ovens.
A real-time clock (RTC) is a computer clock, usually in the form of an integrated circuit(IC) that is solely built for keeping time. It runs on an alternate power source to ensure that the clock is in sync, even when the device is powered off. A small memory on the IC stores system description or setup values — including current time values stored by the real-time clock.
RTC also leverages a Quartz crystal oscillator to track time. So, to understand how time is captured, let’s understand at a high level how Quartz crystal oscillators help us!
Quartz Clocks
Quartz Clocks rely on laser-trimmed quartz crystals which are tailored to resonate at a specific frequency.
Quartz demonstrates the PiezoElectric effect, which means that quartz produces an electric field when a mechanical force is applied to it and vice-versa. So, we can apply a fixed voltage to a Quartz crystal & make it oscillate at a fixed frequency. The clock can count a fixed number of oscillations(tick) as a unit of time & increments a 64-bit counter to capture every tick.
You can go through the below short video to understand the same.
Challenges with Quartz Clocks
Quartz clocks are widely used because its abundantly available and do a good job of being fairly accurate. However, there are challenges which make Quartz clocks, not the perfect choice -
Manufacturing imperfections can cause some clocks to run faster/slower than others.
Accuracy depends on the ideal temperature. Quartz clocks are tuned to be stable around room temperature, but significantly higher or lower temperatures slow down the clock impacting accuracy.
Atomic Clocks
Atomic Clocks are based on the quantum-mechanical properties of certain elements like Caesium. It measures time by monitoring the frequency of radiation of atoms. Since the electron configuration of all atoms of a particular element is the same, Atomic Clocks don’t suffer from the manufacturing imperfections that Quartz clocks do. Read this to understand more about Atomic Clocks.
The second is defined as exactly 9,192,631,770 cycles of a Caesium atomic clock. Atomic Clocks are hence known to be the most accurate clocks.
The NIST-F1 cesium atomic clock in Boulder, Colorado is so accurate that the clock would lose one second in 100 million years.
Why are Atomic Clocks not used in Devices then?
Because they are huge devices & very expensive($$$). Also, the frequency of oscillation can be impacted by external influences, and hence care needs to be taken to run this in an isolated environment.
External influences, such as electromagnetic fields, can influence the energy levels of the states and thus also the transition frequency. Atomic clocks are therefore shielded at great expense, and special pairs of electrical configurations are selected that are as insensitive as possible to external interference.
GPS Clocks
GPS Clocks rely on satellites orbiting the Earth, which have an atomic clock onboard. These satellites broadcast the current time & location to receivers on Earth & use the speed of light to calculate distance.
Atomic Clocks installed on the satellites are not as accurate as NIST-F1, but they too provide an accuracy such that accurate that the clock would lose one second in 10 million years.
Great, so now we’ve covered all these different types of physical clocks. But, what’s the point of knowing this?
The system of time measurement using Atomic Clocks is accurate. However, there is still a problem. It's too accurate & the real world is not so accurate :D
Earth’s speed of rotation is not constant, it changes due to the effects of tides, earthquakes, and some other factors. So one rotation of the earth around its axis might not take 24 * 60 * 60 * 9,192,631,770 cycles of a Caesium atomic clock.
So, now we have two dimensions of time -
Based on Atomic Clocks
International Atomic Time (TAI) is a time scale that uses the combined output of some 400 highly precise atomic clocks. It provides the exact speed at which our clocks tick.
2. Based on Earth’s Rotation Speed
Universal Time (UT1), also known as astronomical time, refers to the Earth’s rotation. Universal Time is a solar time standard that reflects the average speed of the Earth’s rotation. Using the prime meridian at 0° longitude as a reference point, it shows the actual length of an average solar day on Earth, which is the time from one solar noon to the next.
UTC is the time standard commonly used across the world. The world’s timing centres have agreed to keep their time scales closely synchronized — or coordinated — therefore the name Coordinated Universal Time. UTC is determined by TAI & UT1.
Overall, the length of a day is consistently within a few milliseconds of 86,400 seconds, which is equivalent to 24 hours. However, it's not exactly 86400 seconds, and over months and years, these small differences in milliseconds can add up and put our clocks out of sync with the Earth’s spin. This is where we use the concept of leap seconds!
A Leap Second is a one-second adjustment that is occasionally applied to Coordinated Universal Time (UTC), to accommodate the difference between precise time (International Atomic Time (TAI), as measured by atomic clocks) and imprecise observed solar time (UT1), which varies due to irregularities and the long-term slowdown in the Earth’s rotation.
Leap second is applied on the last day of June/December, at 23:59:59 when the difference between TAI & UTI approaches 1 second(~> 0.9sec).
Leap second can be positive or negative depending on whether the earth was spinning slower or faster than its expected speed. A positive leap second adds an extra second to the time & a negative leap second skips a second.
In the next article, we’ll talk about different ways a leap second can be applied to our clocks. If our computers/servers are running on Quartz clocks, how can they give accurate times & how in a distributed system, all nodes can remain in sync about the time?