This document discusses timekeeping in distributed systems. It begins by explaining how different types of clocks work, from pendulum clocks to atomic clocks. It then discusses key concepts like UTC, leap seconds, and how time is represented in Unix. The document also covers challenges of keeping time across distributed systems and algorithms like NTP, vector clocks, and logical clocks that help order events in a distributed system.
13. UNIX TIME
Number of seconds since epoch (1970/01/01)
It has exactly 86 400s per day…
14. • Day does not take 24h…
• … it’s not even a constant.
A TRUTH ABOUT DAY
15. UTC
ATOMIC TIME
BASED ON ATOMIC CLOCK
SOLAR/ASTRONOMICAL TIME
BASED ON EARTH ROTATION
UTC
A COMPROMISE BASED ON ATOMIC
TIME IRREGULARLY ADJUSTED TO
SOLAR TIME
20. NETWORK
TIME
PROTOCOL
1. Skew – speedup/slowdown clock tick
rate
2. Step – set a new value when difference
is too big (128ms)
21. NTP PROBLEMS
▪ Stepping interferes with happen-before
relationships.
▪ It’s slow.
▪ It assumes, that network is symmetric.
PRECISION TIME
PROTOCOL
▪ Available in Windows Server 2019
(opt-in)
▪ Takes into account a latency per
each network device
22. OS TIME
API
WINDOWS
wall-clock time
LINUX
wall-clock time
WINDOWS
monotonic time
LINUX
monotonic time
clock_gettime(CLOCK_REALTIME)
clock_gettime(CLOCK_MONOTONIC)QueryPerformanceCounter(&time)
GetSystemTimePreciseAsFileTime(&date)
24. SEQUENCERS
Set X = 1 ts = 1
Set Y = 2 ts = 2
Set X = 3 ts = 3
Delete Y ts = 4
Set Y = 1 ts = 5
Delete X ts = 6
Monotonically incrementing counter
timeline
Node A
25. DOTS
Set X = 1 A:1
Set X = 3 A:2
Set Y = 1 A:3
Delete X A:4
timeline
Node A
Set Y = 2 B:1
Delete Y B:2
timeline
Node B
26. VECTOR
CLOCKS
Set X = 1
Set X = 3
Set Y = 1
Delete X
Node A
Set Y = 2
Delete Y
Node B
A 1
A 1
B 1
A 2
B 1 A 2
B 2
A 3
B 2
A 4
B 2
37. Keeping time in real systems: https://www.youtube.com/watch?v=BRvj8PykSc4
Windows plans for leap seconds: https://blogs.technet.microsoft.com/networking/2018/07/18/top10-ws2019-hatime/
Dotted version vectors: http://basho.com/posts/technical/vector-clocks-revisited-part-2-dotted-version-vectors/
REFERENCES
Fun fact: this issue didn’t happened in Google, because they have used so called “leap second smearing” – when they knew that leap second is getting closer, they started to drift their atomic clocks on purpose to slowly compensate for that leap second. The problem is that this is not official way of doing things, so Google’s internal reference clocks were not inline will any other reference clocks for some time.
Sometimes providers (like Amazon) officially announce leap seconds incoming.
NTP also can send a packet informing about incoming leap second.