Clocks in Distributed System
Clocks in Distributed System
Clocks in Distributed System
Types of Clocks
Physical Clocks
Tied to the notion of real time
Can be used to order events, find time difference between
two events,..
Logical Clocks
Derived from the notion of potential cause-effect between
events
Not tied to the notion of real time
Can be used to order events
Different types
Lamports Logical Clock
Vector Clocks
…
Physical Clocks
Each node has a local clock used by it to timestamp
events at the node
Local clocks of different nodes may vary
Need to keep them synchronized (Clock
Synchronization Problem)
Perfect synchronization not possible because of
inability to estimate network delays exactly
But still useful, synchronization requirements vary
Kerberos: requires synchronization of the order of minutes
GPS: requires synchronization of the order of milliseconds
Clock Synchronization
Internal Synchronization
Requires the clocks of the nodes to be
synchronized to within a pre-specified bound
However, the clock times may not be
synchronized to any external time reference, and
can vary arbitrarily from any such reference
External Synchronization
Requires the clocks to be synchronized to within a
pre-specified bound of an external reference clock
How Computer Clocks Work
Astronomical
traditionally used
based on earth’s rotation around its axis and
around the sun
solar day : interval between two consecutive
transits of the sun
solar second : 1/86,400 of a solar day
period of earth’s rotation varies, so solar second
is not stable
mean solar second : average length of large no of
solar days, then divide by 86,400
Atomic
Based on the transitions of Cesium 133 atom
1 sec. = time for 9,192,631,770 transitions
about 50+ labs maintain Cesium clock
International Atomic Time (TAI) : mean no. of ticks
of the clocks since Jan 1, 1958
Highly stable
But slightly off-sync with mean solar day (since
solar day is getting longer)
A leap second inserted occasionally to bring it in
sync.
Resulting clock is called UTC – Universal
Coordinated Time
UTC time is broadcast from different sources around
the world, ex.
National Institute of Standards & Technology
(NIST) – runs WWV radio station, anyone with a
proper receiver can tune in
United States Naval Observatory (USNO) –
supplies time to all defense sources
National Physical Laboratory in UK
Satellites
Many others
Accuracies can vary (< 1 milliseconds to a few
milliseconds)
Synchronizing with UTC Time
Can use a Cristian-like algorithm with the
time server sync’ed to a UTC source
Not scalable for internet-scale
synchronization
Solution: Use a hierarchical approach
NTP : Network Time Protocol
Protocol for time sync. in the internet
Hierarchical architecture
Primary time servers (stratum 1) synchronize to
national time standards via radio, satellite etc.
Most accurate
Secondary servers and clients (stratum 2, 3,…)
synchronize to primary servers in a hierarchical
manner (stratum 2 servers sync. with stratum 1,
stratum 3 with stratum 2 etc.)
Lower stratum means more accurate
Reliability ensured by synchronizing with redundant
servers
Communication by multicast (usually within LAN
servers), symmetric (usually within multiple
geographically close servers), or client server (to
higher stratum servers)
Complex algorithms to combine and filter times
Sync. possible to within tens of milliseconds for most
machines
But just a best-effort service, no guarantees
http://www.ntp.org for more details
Ordering Events
Given two events in a distributed system (at same or
different nodes), can we say if one happened before
another or not?
Common requirement, for example, in applying updates to
replicas in a replicated system
Physical clocks can be used with synchronization in
many cases
Fails to order when events happen too fast (faster
than the maximum possible skew between two
clocks)
Are physical clocks needed at all for ordering
events?
Causality and Ordering
Can what happened in one event at one node
affect what happens in another event in the
same or another node?
Because if not, ordering them is not important
Can we capture this notion of causality
between events and build a local clock
around it?
Use the causality to synchronize the local clocks
No relation to time synchronization as we have
seen so far, no real notion of time
Lamport’s Ordering
Lamport’s Happened Before relationship:
( x || y)
Lamport’s Logical Clock