Lecture 2 - Time and Ordering
Lecture 2 - Time and Ordering
Computing
Fall 2022
Dr. Zeshan Iqbal
Topic: Time and Ordering
Lecture 2: Introduction and Basics
Why Synchronization?
• You want to catch a bus at 6.05 pm, but your watch is off by
15 minutes
– What if your watch is Late by 15 minutes?
• You’ll miss the bus!
– What if your watch is Fast by 15 minutes?
• You’ll end up unfairly waiting for a longer time than
you intended
• Time synchronization is required for both
– Correctness
– Fairness
2
Synchronization In The Cloud
Why is it Challenging?
4
Some Definitions
6
How often to Synchronize?
8
Next
Basics
• External time synchronization
• All processes P synchronize with a time server S
RTT
Set clock to t
Time
P What’s the time?
10
What’s Wrong
11
Cristian’s Algorithm
• P measures the round-trip-time RTT
of message exchange
RTT
Set clock to t
Time
P What’s the time?
12
Cristian’s Algorithm (2)
• P measures the round-trip-time RTT of message exchange
• Suppose we know the minimum P à S latency min1
• And the minimum S à P latency min2
– min1 and min2 depend on Operating system overhead to buffer
messages, TCP time to queue messages, etc.
RTT
Set clock to t
Time
P What’s the time?
13
14
Cristian’s Algorithm (4)
• The actual time at P when it receives response is between [t+min2, t+RTT-min1]
• P sets its time to halfway through this interval
– To: t + (RTT+min2-min1)/2
• Error is at most (RTT-min2-min1)/2 ------- Bounded!
• The width of this range is (RTT - 2*min). This gives an accuracy of (RTT/2 - min).
– Considering min1=min2
RTT
Set clock to t
Time
P What’s the time?
15
Gotchas
• Allowed to increase clock value but should never decrease clock value
– May violate ordering of events within the same process
16
NTP = Network Time Protocol
17
Secondary servers
Tertiary servers
Client
18
NTP Protocol
Message 1 recv time tr1
Message 2 send time ts2
Time
Child
Let’s start protocol
Message 2 ts1, tr2
Message 1
Parent
Message 2 recv time tr2
Message 1 send time ts1
19
20
Why o = (tr1 - ts1 + ts2 - tr2)/2?
• Offset o = (tr1 – ts1 + ts2 – tr2)/2
• Let’s calculate the error
• Suppose real offset is oreal
– Child is ahead of parent by oreal
– Parent is ahead of child by -oreal
• Suppose one-way latency of Message 1 is L1 (L2 for Message 2)
• No one knows L1 or L2!
• Then
tr1 = ts1 + L1 + oreal
tr2 = ts2 + L2 – oreal
21
22
NTP in real systems
23
2 reception,
t2 is the server's timestamp of the response packet
• and the round-trip delay δ by transmission and
t3 is the client's timestamp of the response packet
𝛿 = 𝑡3 − 𝑡0 − (𝑡2 − 𝑡1) reception.
• The values for θ and δ are passed through filters and subjected to statistical
analysis. Outliers are discarded and an estimate of time offset is derived
from the best three remaining candidates.
• The clock frequency is then adjusted to reduce the offset gradually
24
And yet…
• We still have a non-zero error!
• We just can’t seem to get rid of error
– Can’t, as long as message latencies are non-zero
• Can we avoid synchronizing clocks altogether, and still be able to
order events?
25
Lamport Timestamps
26
Ordering Events in a Distributed System
• To order events across processes, trying to sync clocks is one approach
• What if we instead assigned timestamps to events that were not absolute time?
• As long as these timestamps obey causality, that would work
If an event A causally happens before another event B,
then timestamp(A) < timestamp(B)
Humans use causality all the time
E.g., I enter a house only after I unlock it
E.g., You receive a letter only after I send it
27
28
Logical (or Lamport) Ordering(2)
• Define a logical relation Happens-Before among pairs of events
• Happens-Before denoted as ®
• Three rules
1. On the same process: a ® b, if time(a) < time(b) (using the local
clock)
2. If p1 sends m to p2: send(m) ® receive(m)
3. (Transitivity) If a ® b and b ® c then a ® c
• Creates a partial order among events
– Not all events related to each other via ®
29
Example
A B C D E
P1
Time
E F G
P2
P3 H I J
While P1 and P3 each have an event
Instruction or step
labeled E, these are different events as
they occur at different processes. Message
30
Happens-Before
A B C D E
P1
Time
E F G
P2
H I J
P3
• AàB Instruction or step
• BàF Message
• AàF
31
Happens-Before (2)
A B C D E
P1
Time
E F G
P2
H I J
P3• HàG
• FàJ Instruction or step
• HàJ Message
• CàJ
32
In practice: Lamport timestamps
• Goal: Assign logical (Lamport) timestamp to each event
• Timestamps obey causality
• Rules
– Each process uses a local counter (clock) which is an integer
• initial value of counter is 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
33
Example
P1
Time
P2
P3
Instruction or step
Message
34
Lamport Timestamps
P1 0
Time
P2 0
P3 0
Instruction or step
Initial counters (clocks)
Message
35
Lamport Timestamps
P1 0
ts = 1
Time
P2 0
Message carries
P3 0 ts = 1
ts = 1
Message send Instruction or step
Message
36
Lamport Timestamps
P1 0
1 ts = max(local, msg) + 1
= max(0, 1)+1 Time
=2
P2 0
Message carries
P3 0 ts = 1
1
Instruction or step
Message
37
Lamport Timestamps
P1 0 2
1
Message carries Time
ts = 2
P2 0
2
max(2, 2)+1
=3
P3 0
1
Instruction or step
Message
38
Lamport Timestamps
max(3, 4)+1
=5
P1 0 2
1 3
Time
P2 0
2 3 4
P3 0
1
Instruction or step
Message
39
Lamport Timestamps
P1 0 2 5 6
1 3
Time
P2 0
2 3 4
P3 0
1 2 7
Instruction or step
Message
40
Obeying Causality
A B C D E
P1 0 2 5 6
1 3
Time
E F G
P2 0
2 3 4
H I J
P3 0
1 2 7
• A à B :: 1 < 2 Instruction or step
• B à F :: 2 < 3
• A à F :: 1 < 3 Message
41
H I J
P3 0
1 2 7
• H à G :: 1 < 4 Instruction or step
• F à J :: 3 < 7
• H à J :: 1 < 7 Message
• C à J :: 3 < 7
42
Not always implying Causality
A B C D E
P1 0 2 5 6
1 3
Time
E F G
P2 0
2 3 4
H I J
P3 0
1 2 7
• ? C à F ? :: 3 = 3 Instruction or step
• ? H à C ? :: 1 < 3
• (C, F) and (H, C) are pairs of Message
concurrent events
43
Concurrent Events
• A pair of concurrent events doesn’t have a causal path from one event to
another (either way, in the pair)
• Lamport timestamps not guaranteed to be ordered or unequal for concurrent
events
• Ok, since concurrent events are not causality related!
• Remember
E1 à E2 Þ timestamp(E1) < timestamp (E2), BUT
timestamp(E1) < timestamp (E2) Þ
{E1 à E2} OR {E1 and E2 concurrent}
44
Next
45
Vector Clocks
46
Vector Timestamps
• Used in key-value stores like Riak
• Each process uses a vector of integer clocks
• Suppose there are N processes in the group 1…N
• Each vector has N elements
• Process i maintains vector Vi[1…N]
• jth element of vector clock at process i, Vi[j], is i’s knowledge of
latest events at process j
47
48
Example
A B C D E
P1
Time
E F G
P2
H I J
P3
Instruction or step
Message
49
Vector Timestamps
P1(0,0,0)
Time
P2
(0,0,0)
P3
(0,0,0)
Initial counters (clocks)
50
Vector Timestamps
P1(0,0,0) (1,0,0)
Time
P2
(0,0,0)
Message(0,0,1)
P3
(0,0,0) (0,0,1)
51
Vector Timestamps
P1(0,0,0) (1,0,0)
Time
P2
(0,0,0) (0,1,1)
Message(0,0,1)
P3
(0,0,0) (0,0,1)
52
Vector Timestamps
P1(0,0,0) (1,0,0) (2,0,0)
Message(2,0,0) Time
P2
(0,0,0) (0,1,1) (2,2,1)
P3
(0,0,0) (0,0,1)
53
Vector Timestamps
P1(0,0,0) (1,0,0) (2,0,0) (3,0,0) (4,3,1) (5,3,1)
Time
P2
(0,0,0) (0,1,1) (2,2,1) (2,3,1)
P3
(0,0,0) (0,0,1) (0,0,2) (5,3,3)
54
Causally-Related …
• VT1 = VT2,
iff (if and only if)
VT1[j] = VT2[j], for all j = 1, … , N
• VT1 ≤ VT2,
iff VT1[j] ≤ VT2[j], for all j = 1, … , N
• Two events are causally related iff
VT1 < VT2, i.e.,
iff VT1 ≤ VT2 &
there exists j such that
1 ≤ j ≤ N & VT1[j] (Not Equal) VT2 [j]
55
… or Not Causally-Related
56
Obeying Causality
A B C D E
P1(0,0,0) (1,0,0) (2,0,0) (3,0,0) (4,3,1) (5,3,1)
Time
E F G
P2
(0,0,0) (0,1,1) (2,2,1) (2,3,1)
H I J
P3
(0,0,0) (0,0,1) (0,0,2) (5,3,3)
• A à B :: (1,0,0) < (2,0,0)
• B à F :: (2,0,0) < (2,2,1)
• A à F :: (1,0,0) < (2,2,1)
57
H I J
P3
(0,0,0) (0,0,1) (0,0,2) (5,3,3)
• H à G :: (0,0,1) < (2,3,1)
• F à J :: (2,2,1) < (5,3,3)
• H à J :: (0,0,1) < (5,3,3)
• C à J :: (3,0,0) < (5,3,3)
58
Identifying Concurrent Events
A B C D E
P1(0,0,0) (1,0,0) (2,0,0) (3,0,0) (4,3,1) (5,3,1)
Time
E F G
P2
(0,0,0) (0,1,1) (2,2,1) (2,3,1)
H I J
P3
(0,0,0) (0,0,1) (0,0,2) (5,3,3)
• C & F :: (3,0,0) ||| (2,2,1)
• H & C :: (0,0,1) ||| (3,0,0)
• (C, F) and (H, C) are pairs of concurrent events
59
• Lamport timestamps
– Integer clocks assigned to events
– Obeys causality
– Cannot distinguish concurrent events
• Vector timestamps
– Obey causality
– By using more space, can also identify concurrent events
60
Time and Ordering: Summary
• Clocks are unsynchronized in an asynchronous distributed
system
• But need to order events, across processes!
• Time synchronization
– Cristian’s algorithm
– NTP
– Berkeley algorithm
– But error a function of round-trip-time
• Can avoid time sync altogether by instead assigning logical
timestamps to events
61