Professional Documents
Culture Documents
CE739-Mobile Pervasive
Computing
Time & Synchronization
Dareen K. Halim
Hargyo Tri Nugroho
Samuel
2
Contents
• Introduction
• Why time synchronization
• System model for time synchronization
• Physical clock synchronization
• Cristian algorithm
• Berkeley algorithm
• Network Time Protocol (NTP)
• Logical clock synchronization
• Lamport timestamp
• Lamport vector timestamp
3
Introduction to time
synchronization
4
Why Synchronization
• You want to catch the 6.05 pm shuttle bus at one
shopping mall, but your watch is off by 15 minutes
• What if your watch is Late by 15 minutes?
• What if your watch is Fast by 15 minutes?
9
Physical Clocks & Synchronization
• In a DS, each process has its own clock. • Max drift rate between two clocks with similar
• Clock Skew versus Drift MDR is 2 * MDR Why? Consider the extreme
• Clock Skew = Relative Difference in clock of one drifting right and another drifting left
values of two processes Let’s do some math!
• Clock Drift = Relative Difference in clock
frequencies (rates) of two processes
• A non-zero clock drift causes skew to increase • Max-Sync-Interval (when to sync) =
(eventually). (MaxAcceptableSkew - CurrentSkew) /
• Assume a Maximum Drift Rate (MDR) of a (MDR * 2)
clock
(i.e., distance/speed = time)
• Absolute MDR is defined relative to
Coordinated Universal Time (UTC) Think of it as “Ouch, we have x seconds before the
• MDR of a process depends on the currentSkew reaches MaxAcceptableSkew”
environment.
Synchronizing Physical Clocks – “Accurate”
Ci(t): the reading of the software clock
at process i when the real time is t.
mr
mt
p Time server,S
Cristian’s Algorithm
• Uses a time server to synchronize clocks
• Time server keeps the reference time (say UTC)
• A client asks the time server for time the server responds
with its current time T and the client uses this received value
to set its clock
• But network round-trip time introduces an error…
Cristian’s Algorithm
• Let RTT = response_received_time – request_sent_time (measurable at client)
• Also, suppose we know:
1. the minimum value min of the client-server one-way transmission time (Depends
on what?)
2. and that the server timestamped the message at the last possible instant before
sending it back (what if it does not?)
• Then, the actual time could be between [T + min, T + RTT - min]
T + min server timestamp right before sending response to client
T + RTT - min server timestamp right after the request arrived
Cristian’s Algorithm
• Client sets its clock to halfway between T+min and T+RTT - min i.e., at
(T+RTT)/2
Expected (i.e., average) skew in client clock time should be significantly smaller than the
RTT
• Can increase clock value, but should never decrease it – Why?
• Future events may have smaller timestamp than previous events
• Can adjust speed of clock too (take multiple readings) – either up or down is ok.
• For unusually long RTTs, repeat the time request
• For non-uniform RTTs, use weighted average
avg-clock-errorn = (w * latest-clock-error) + (1 – w) * avg-clock-error n-1
Cristian’s Algorithm
Works for smaller RTT
Using single time server
Abnormal time server is out of scope
Berkeley Algorithm
• Uses an elected master process to synchronize among clients, without
the presence of a time server
• The elected master regulates the time synchronization process
• 1 process, the master, polls others (slaves) by broadcast
• Slaves reply with their times
• Master estimates their local times using round-trip times (as above)
• Master averages all these (and its own time) eliminating any times with excessive
RTT
• Also eliminates any wrong clocks
• Rather than send back correct time, master sends back to each slave its own delta
(+/-)
Berkeley Algorithm
(3:25 + 2:50 + 3:00)/3 = 3:05
Berkeley Algorithm
• Multiple leaders may also be used.
Secondary servers,
synched by the 2 2 2
primary server Stratum 3,
synched by the
3 3 3 3 3 secondary
3
servers
Messages Exchanged between
NTP server and client
Each message bears
• timestamps of recent message events
• local time when the previous NTP message was sent and received
• local time when the current message was transmitted.
Server T1 T2
Time
m m'
Time
Client T0 T3
Start ‘End’
Quick glance on NTP synchronization
Client calculates these and adjust Server T1 T2
Time
locally
• The time offset between server and m m'
client
Time
Client T0 T3
• Round trip delay
T0 = client’s timestamp when sending m (request)
T1 = server’s timestamp when receiving m (request)
• These steps may be repeated, with value
T2 = server’s timestamp when sending m’ (reply)
and being statistically analyzed and
filtered T3 = client’s timestamp when receiving m’ (reply)
• Then the algorithm will determine which
peers to use and what value for
synchronization
24
Logical clock synchronization
25
Logical Clocks
• Is it always necessary to give absolute time to events?
• Suppose we can assign relative time to events, in a way that does not violate their
causality
• Well, that would work – we humans run our lives without looking at our watches for
everything we do
• First proposed by Leslie Lamport in the 70’s (Wow!)
• Define a logical relation Happens-Before () among events:
• On the same process: a b, if time(a) < time(b)
• If p1 sends m to p2: send(m) receive(m)
• (Transitivity) If a b and b c then a c
Logical Clocks
• Lamport Algorithm assigns logical timestamps to events:
• All processes use a counter (clock) with initial value of zero
• A process increments its counter when:
• a send or an instruction happens at it.
• The counter is assigned to the event as its timestamp.
• A send(message) event carries its timestamp
• For a receive(message) event, the counter is updated by
max(local clock, message timestamp) + 1
Events Occurring at Three Processes
A process increments its counter when a send or an instruction happens at it
p1
a b m1
p2 Physical
c d time
m2
p3
e f
Lamport Timestamps
A process increments its counter when a send or an instruction happens at it
0 1 2
p1
a b m1
0 3 4
Physical
p2
time
c d m2
0 1 5
p3
e f
Find the Mistake: Lamport Logical Time
Physical Time
p 1 0 1 2 3 4
1 2 4
p 2 0 3
2
3
6
p 3 0 4
5 6
8
3
4
7
p 4 0 6 7
5
n
Clock Value
timestamp
Message
Corrected Example : Lamport Logical
Time
Physical Time
p 1 0 1 2 7 8
1 2 8
p 2 0 3
2
3
6
p 3 0 4
5 9
10
3
4
7
p 4 0 6 7
5
n
Clock Value Assume 3 and 7 are
timestamp logically concurrent
Message events
Vector Logical Clocks
• With Lamport Logical Timestamp
• e f timestamp(e) < timestamp (f), but
• timestamp(e) < timestamp (f) {e f} OR {e and f concurrent}
• Vector Logical time addresses this issue:
• N processes. Each uses a vector of counters (logical clocks), initially all zero
• ith element is the clock value for process i
• Each process i increments the ith element of its vector upon an instruction or send
event.
• Vector value is timestamp of the event
• A send(message) event carries its vector timestamp (counter vector)
• For a receive(message) event, Vrecv[j] Max(Vrecv[j] , Vmsg[j]), if j is not self
Vrecv[j] + 1 otherwise
Vector Timestamps
(1,0,0) (2,0,0)
p1
a b m1
(2,1,0) (2,2,0)
Physical
p2
time
c d m2
(0,0,1) (2,2,2)
p3
e f
Example: Vector Timestamps
Physical Time
35
Vector Timestamps
(1,0,0) (2,0,0)
p1
a b m1
(2,1,0) (2,2,0)
Physical
p2
time
c d m2
(0,0,1) (2,2,2)
p3
e f
Summary of Synchronization
• Time synchronization important for distributed systems
• Cristian’s algorithm
• Berkeley algorithm
• NTP
• Relative order of events enough for practical purposes
• Lamport’s logical clocks
• Lamport’s vector clocks
Additional references
• http://www.ntp.org/ntpfaq/NTP-s-algo.htm#Q-ALGO-BASIC-SYNC
• Taylor & Francis. Computer Network Time Synchronization: The Network Time Protocol.
pp. 12–. ISBN 978-0-8493-5805-0.
38