You are on page 1of 12

Distributed Systems

Logical Time and Logical clocks


Why logical clocks?

• In single process, events are ordered by local physical time. Since


we cannot synchronize physical clocks perfectly across a distributed
system, we cannot use physical time to find out the order of any
arbitrary pair of events.

• We will use logical time to order events happened at different nodes.


Two simple points:
– If two events occurred at the same process, then they occurred
in the order in which pi observes them
– Whenever a message is sent between processes, the event of
sending the message occurred before the event of receiving the
message.
Happened before Relation/
Causal Ordering
Lamport defined the happened before relation (denoted as “->”), which
describes a causal ordering of events:

1. if a and b are events in the same process, and a occurred before b,


then a->b

2. if a is the event of sending a message m in one process, and b is the


event of receiving that message m in another process, then a -> b

3. Concurrent events • Two distinct events a and b are said to be


concurrent (denoted “a || b”), if neither a-> b nor b-> a.
Lamport’s Logical Clock

• To implement “->” in a distributed system, Lamport


(1978) introduced the concept of logical clocks
• Each process Pi has a logical clock Ci •
• Clock Ci can assign a value Ci (a) to any event a in
process Pi •
• The value C(a) is called the timestamp of event a in
whatever process it occurred.
• The logical clocks assign monotonically increasing
timestamps, and can be implemented by simple counters
Lamport’s Logical Clock Algorithm

Lamport invented a logical clock Li, which is a monotonically


increasing software counter

• LC1:
Li is incremented before each event is issued at process pi:
Li = Li +1

• LC2:
a. Pi sends a message m, it piggybacks on m the value t = Li
b. On receiving (m,t), a process pj computes Lj=max(Lj,t)
and then applies LC1 before timestamping the event
receive(m).
Lamport timestamps for the events

1 2
p1
a b m1

3 4
Physical
p2
c d time
m2

1 5
p3
e f

a →1 b and c →2 d a →e and e → a
b → c and d → f concurrent a || e
combing them, a → f
Problem with logical clock ?
It can be easily shown that:
• If e->e’ then L(e) < L(e’).
• However, the converse is not true. If L(e) < L(e’), then we
cannot infer that e->e’. E.g b and e
• L(b)>L(e) but b||e
• How to solve this problem?
1 2
p1
a b m1

3 4
Physical
p2
time
c d m2

1 5
p3
e f
Vector Clocks

• Lamport’s clock: L(e)<L(e’) we cannot conclude


that e->e’.

• Vector clock overcomes the above problem.

• Maintain a vector of values for every event that


happens in all processes.

• Update happens for group of values in every


event.
Vector Clock - Algorithm
N processes is an array of N integers. Each process
keeps its own vector clock Vi, which it uses to timestamp
local events.
1. VC1: initially, Vi [j] = 0, for i, j = 1,2…N
2. VC2: just before pi timestamps an event, it sets
Vi [i] = Vi [i]+1
3. VC3: pi includes the value t= Vi in every message it
sends
4. VC4: when pi receives a timestamp t in a message, it
sets Vi [j]=max(Vi [j], t [j])for j =1,2…,N. Merge
operation.
Vector timestamps for the events

(1,0,0) (2,0,0)
p1 • Events a and
a b m1 f
V(a)<V(f)
(2,1,0) (2,2,0)
Physical implies a->f
p2
c d time
m2 •Events c and e
V(c) ≤ V(e)
(0,0,1) (2,2,2) V(e) ≤ V(c)
p3
implies c||e
e f

• To compare vector timestamps, we need to compare each bit. Concurrent


events cannot find a relationship.
• V ≤ V'iff V[j] ≤ V'[j] for j= 1,2,…,N
• Drawback compared with Lamport time - amount of storage and message
payload proportional to N.
Summary
• Physical clocks

• Physical clock synchronization

• Logical clocks

• Lamport’s logical clock algorithm

• Vector clocks
Test your understanding

1. What is clock skew and clock drift?


2. What are the two modes of synchronization?
3. What is the main disadvantage of Cristian’s method?
4. An NTP server B receives server’s A’s message at
18:47:56.76, bearing a timestamp 18:46:33.46, and
replies to it. A receives the message at 18:49:15.72,
bearing B’s timestamp, 18:48:25.72. Estimate the offset
between B and A, and the accuracy of the estimate.

You might also like