The University of Sydney Page 1
COMP3221: Distributed
Systems
Time Synchronization
Unit Coordinator
Dr Nguyen Tran
School of Computer Science
The University of Sydney Page 2
Time matters…ex. (1)
– A bank replicates two copies of an account database in New York City (NYC) and San Francisco (SF)
so that a query is always forwarded to the nearest city while updates must be carried out at both
sites
– In a centralized system,
– Process A makes a system call to get the time.
– Later, process B makes a system call for time, which is will be higher or equal to the time returned to process A.
– A client in SF adds $100 to his account which currently contains $1000 while the bank add 1%
interest in NYC.
– Updating a replicated database and leaving it in an inconsistent state: $1111 in SF and $1110 in
NYC
– The problem is that both sites perceive the updates in different order leading to inconsistencies
SF NYC
The University of Sydney Page 3
Time matters…ex. (2)
– In a distributed system, if there is no global agreement on time,
each machine may have slightly different time.
– Compiler will not run as the time of last complied version is
greater than latest source code.
The University of Sydney Page 4
Outline
– Time
– Physical Time
– Logical Time
– Multicast
– Mutual exclusion
– Leader election
The University of Sydney Page 5
Physical Time
Synchronization
The University of Sydney Page 6
Time
– How time is actually measured ?
– Time has been measured astronomically
• Solar day: Interval between two consecutive events of the sun’s
reaching its highest apparent point in the sky = 24 hours = 3600
seconds.
• Basis for the Greenwich Mean Time (GMT) standard
The University of Sydney Page 7
Time
– Earth is slowing down. Period of earth’s rotation is not constant
– Due to tidal friction, atmospheric drag.
– Astronomers introduced Mean solar second (averaged over
large # of days)
– Physicists took over in 1958 with the invention of the atomic
clock.
– By counting the transitions of the cesium 133 atom
First atomic clock [http://www.npl.co.uk/people/louis-essen]
The University of Sydney Page 8
Time
– Second = Time it takes the cesium 133 atom to make exactly
9,192,631,770 transitions.
– Major laboratories in the world periodically update the Bureau
of International de l’Heure (BIH) in Paris on how many times its
clock has ticked. BIH averages these to produce International
Atomic Time (TAI).
– 9,192,631,770 number of transitions was selected to make the
atomic second was equal to mean solar second.
– Can you think of any potential problem ?
The University of Sydney Page 9
Time
– Atomic seconds are of constant length, unlike solar seconds.
– Leap seconds are introduced when necessary to keep in phase
with the sun.
– If the discrepancy between atomic time and solar time grows to
800msec, BIH introduces a leap second.
– This corrected time system is called Universal Coordinated
Time (UTC).
The University of Sydney Page 10
Leap Seconds
– Computerized systems could malfunction if leap seconds are
not handled properly.
– RedHat crash in 2012. THE INSIDE STORY OF THE EXTRA SECOND THAT
CRASHED THE WEB, [https://www.wired.com/2012/07/leap-second-
glitch-explained/]
– Operating systems must have special software to account for
leap seconds as they announced.
– Electric power companies raise their frequency to 61Hz to
synchronize the timing of their 60Hz clocks to UTC.
– 37 leap seconds were added so far.
– The most recent one was on 31/12/2016
The University of Sydney Page 11
Accurate time broadcasters
– Atomic clocks
– Expensive to have one in every computer
– GPS
– 29 satellites circulating around the orbit
– Originally developed for US military
– Each GPS satellite has up to four atomic clocks
– Satellites continuously broadcasts its position and time
– Short radio wave stations transmit a short pulse at the start of
every UTC second
– NIST WWV station from Fort Collins, Colorado
The University of Sydney Page 12
Computer Clocks (Timers)
– Precisely machined quartz crystal.
– When kept under tension, quartz crystals oscillate at the
defined frequency depending on;
– The kid of crystal
– How it is cut
– The amount of tension
The University of Sydney Page 13
Computer Clocks (Timers)
– Hardware clock
– At each clock tick, one is added to the time stored in a battery-backed
up CMOS RAM
– When the CPU is turned off, the time in the CMOS RAM keep being
incremented
– Software clock
– It starts running at boot time and synchronize to the hardware clock
– Used by the operating system to indicate a rough approximation of time
– Multiple CPUs means multiple clocks
– The frequency of two crystals cannot be exactly the same
– Software clocks progressively get out of synch
– The difference returned by two clock values is called clock
skew
The University of Sydney Page 14
Computer Clocks (Timers)
– Real timers cause an interrupt
(ticks) H times every second.
– e.g. H=60 should generate 216,000
ticks per hour. In practice, it will be in
the range of 215,998 – 216,002.
– Maximum drift rate is specified by
the manufacturer
– The relation between clock time
and UTC when clocks tick at
different rates (Ideally, C(t) = t)
– The skew of the clock is dC/dt-1
– Two problems
1. How do we synchronize them with real-world clocks ?
2. How do we synchronize the clocks with each other ?
The University of Sydney Page 15
Algorithm 1 : Network Time Protocol
– Clients synchronize its time with a time server which have an
accurate time, e.g. an atomic clock.
– How do we account for propagation delays ?
– Cristian’s algorithm [1989]
1. A sends a message to B with its timestamp T1 set to its local clock value
2. Upon reception, B records the current time T2 of its local clock
3. B returns a response message to A with T2 and the current time T3 of its clock
4. Upon reception, A records the current time T4 of its local clock
The University of Sydney Page 16
Algorithm 1 : Network Time Protocol
Cristian’s algorithm [1989]
– If clock rate increases monotonically (without even decreasing) and
– If both message transmissions take roughly the same time
– Round-trip time = (T2-T1)+(T4-T3)
– Offset of A relative to B = T4 – T3 – RTT/2
– A can re-adjust its time to T3 + RTT/2
– This result is not accurate but gives an acceptable approximation
The University of Sydney Page 17
Algorithm 1 : Network Time Protocol
– NTP is set up pair-wise between servers
– i.e. B will also probe A for its current time
1. Run Cristian’s algorithm 8 times and records the 8 output pairs
illustrate the clock value after the reception of an ack.
– 7.3 represents the message was received at process 3 at clock 7. Then, max{local,
received} is taken to reach the new local clock.
– Messages are delivered in the order of their clocks, B, C, A, everywhere (lower
timestamps and IDs order)
3
2
4
A
A
AB
B
B
C
C
C
B
B
B
A
A
A
C
C
C
4
3
5
5 6
4
6 7
6.1->6 7
7.2->7 7.3->7
7.3->7
7.2->7
7.2->7
6.1->7
6.1->7
7.3->7
p1
p2
p3
The University of Sydney Page 37
Multicast
– Goal: broadcasting messages that got delivered only if all
causally preceding messages have already been delivered
– Each process maintains a vector clock (similar as before) and a
priority queue of messages (waiting to be delivered to the top
layer)
– Process pi increments VCi[i] only when sending a message (not when
receiving) and pj adjusts VCj when receiving a message.
– pj postpones delivery of m from pi until both conditions are met:
1. ts(m)[i] = VCj[i] + 1
2. ts(m)[k] ≤ VCj[k] for k ≠ i
Causal ordered multicast
The University of Sydney Page 38
Multicast
– Take VC2 = [0,2,2], ts(m) = [1,3,0] from p0. What information does p2
have, and what will it do when receiving m (from p0)?
Causal ordered multicast (con’t)
The University of Sydney Page 39
Mutual Exclusion
Synchronization
The University of Sydney Page 40
Mutual exclusion
– Concurrent access to the same resource may corrupt the resource, or
make it inconsistent.
– Problem: Multiple processes in a distributed system want exclusive
access to some resource
The University of Sydney Page 41
Mutual exclusion
– Fair and easy to implement
– Requires only three messages per use of resource (request, grant,
release)
– No process ever waits forever à no starvation
– But, single point of failure
Centralized algorithm
The University of Sydney Page 42
Mutual exclusion
– Principle. Assume every resource is replicated n times, with each
replica having its own coordinator ⇒ access requires a majority
vote from m > n/2 coordinators. A coordinator always responds
immediately to a request.
– Assumption: When a coordinator crashes, it will recover quickly,
but will have forgotten about permissions it had granted.
– Problem: A coordinator may grant permission to some node
whereas it already did it to another node
Decentralized algorithm
The University of Sydney Page 43
Mutual exclusion
– Principle. The same as before except that acknowledgments are not
sent. Instead, replies (i.e., grants) are sent only when
– The receiver has no interest in the shared resource or
– The receiver is waiting for the resource, but has lower priority (known through
comparison of timestamps)
– Two processes want to access a shared resource at the same moment
– Process 0 has the lowest timestamp, so it wins.
– When process 0 is done, it sends an OK also, so 2 can now go ahead.
Distributed algorithm – Ricart and Agrawala algorithm
The University of Sydney Page 44
Mutual exclusion
– Single point of failure is replaced by n-point of failures
– More message exchanges
– Getting permission from everyone is really an over-kill
– Distributed algorithm – Ricart and Agrawala algorithm
The University of Sydney Page 45
Mutual exclusion
– Principle. Organize processes in a logical ring, and let a token be
passed between them. The one that holds the token is allowed to
accessing the resource (if it wants to).
Token ring algorithm
The University of Sydney Page 46
Mutual exclusion
– Messages and delay are necessary to get granted the permission
to access the resource (this means entering the critical section) or to
release the permission (exiting it)
Comparison of the last four solutions
The University of Sydney Page 47
Conclusion
– Time matters in distributed systems
– Physical time
– It is expensive to have accurate physical time on every computer
– Synchronization with someone who knows the accurate time
• Network Time Protocol and Berkeley Algorithm
– Logical time
– Keeping track of the order of events is what matters !
– To make the “happens-before” relation visible to processes à Logical clocks
– To make also the “did-not-happen-before” relation visible à Vector clocks
– Ordered multicasting deliver messages in the same order at all
nodes
– Providing exclusive access and leader election are practical
challenges for many distributed systems
The University of Sydney Page 48
What’s Next ?
– Tutorial 5 on Wednesday.
– Assignment 1 is available – Good Luck !
– Next week: Consistency in distributed systems
– See you all next week !