Distributed Systems Time, Clocks, and Ordering of Events Björn Franke University of Edinburgh 2016/2017
Today Last lecture: Basic Algorithms Today: Time, clocks, NTP Ref: CDK Causality, ordering, logical clocks: Ref: VG, CDK 2
Time Ordering of events are important: Which happened first Need synchronisation between sender and receiver Coordination of joint activity etc.. 3
Coordinated Universal Time Coordinated universal time (UTC) Time maintained for civil use (on atomic clock) Kept within 0.9 seconds of exact mean time for Greenwich 4
Clocks Piezoelectric effect: Squeeze a quartz crystal: generates electric field Apply electric field: crystal bends Quartz crystal clock: Resonation like a tuning fork Accurate to parts per million Gain/lose ½ second per day 5
Challenges Two clocks do not agree perfectly Skew: The time difference between two clocks Quartz oscillators vibrate at different rates Drift: The difference in rates of two clocks If we had two perfect clocks: Skew = 0 Drift = 0 6
When we detect a clock has a skew Eg: it is 5 seconds behind Or 5 seconds ahead What can we do? 7
When we detect a clock has a skew e.g. it is 5 seconds behind We can advance it 5 seconds to correct Might skip over event scheduled in-between Or 5 seconds ahead Pushing back 5 seconds is a bad idea Message was received before it was sent Document closed before it was saved etc.. We want monotonicity: time always increases We want continuity: time doesn t make jumps 8
When we detect a clock has a skew e.g. it is behind Run it faster until it catches up It is ahead Run it slower until it catches up This does not guarantee correct clock in future Need to check and adjust periodically 9
How clocks synchronise Obtain time from time server: Client Request time Time : 00:05:20 Server 10
How clocks synchronise Obtain time from time server: Request time Client Server Time : 00:05:20 Time is inaccurate Delays in message transmission Delays due to processing time Server s time may be inaccurate 11
Christian s algorithm Compensate for delays Request sent at T 0 Reply received at T 1 T server Request Reply T 0 T 1 Assume delays are symmetric 12
Christian s algorithm T new = T server + (T 1 T 0 )/2 T server Request Reply T 0 T 1 13
Christian s algorithm T server Request Reply T 0 T 1 T new = T server + (T 1 T 0 )/2 Example: T 0 = 5:05:08.100, T 1 = 5:05:9.500 T server = 5:05:9.100 T new = 5:05:09:800 14
Christian s algorithm If minimum message transit time T min is known Range = T 1 T 0-2T min Accuracy of result: (T 1 T 0-2T min )/2 T server Request Reply T 0 T 1 T min T min 15
Berkeley algorithm Assumes no machine has perfect time Takes average of participating computers Sync all clocks to average 16
Berkeley algorithm One computer is elected as server (master) Others are slaves Master polls each machine for time Compute average Key idea: average will cancel out skews Send each clock the offset by which it needs to adjust time Sending time itself is susceptible to network delays 17
Berkeley algorithm Fault tolerance Ignore readings of clocks with too large skews If master fails: run an election algorithm and a slave becomes master 18
Network time protocol (NTP) Enable clients to synchronise to UTC with reasonable accuracy Reliable: Redundant servers and paths Scalable: Enable many clients to synchronise frequently Security Authenticate sources 19
Network time protocol Servers in strata 1: directly connected to atomic, GPS etc clock May inter-communicate for cross checks 2: few microseconds of level 1 etc 20
Network time protocol Uses multiple rounds of messages to get better time Large number of servers Uses a multiple spanning tree (MST) for inter- server synchronisation 21
Time and Synchronisation Important topic in distributed systems Many different methods Depending on systems, requirements No perfect solution 22
Special Relativity Light cone: Things that e can have cause/influence e Things that could have caused/influenced e 23
GPS Satellites: Have very accurate atomic clocks Transmit signals: satid, time T 0, Receivers measure distance: (T 1 T 0 )*c [c = speed of light] Distance from multiple satellites gives location Complex computation, taking into account possible errors, clock drift and skew etc.. Needs relativistic computation Special relativity: Clocks on fast moving satellites run slow (microseconds per day drift for satellites) General relativity: Clocks far from heavy bodies run fast (microseconds per day) 24