Lamport Logical Clocks
Lamport Logical Clocks
Lamport Logical Clocks
Logical Clocks
• If two machines do not interact, there is no
need to synchronize them
• What usually matters is that processes agree
on the order in which events occur rather than
the time at which they occurred
– Absolute time is not important
– Use logical clocks
Event Ordering
• Problem: define a global ordering of all events that
occur in a system
• Events in a single processor machine are locally ordered
• In a distributed system:
– No global clock, local clocks may be unsynchronized
– Can not order events on different machines using local
times
• Key idea [Lamport]
– Processes exchange messages
– Message must be sent before received
– Send/receive used to order events and synchronize logical
clocks
1
2/13/2012
Happens‐Before (HB) Relation
• If A and B are events in the same process and
A occurs before B, then A B
• If A represents sending of a message and B is
the receipt of this message, then A B
• Relation is transitive:
– A B and B C implies A C
• Unordered events are concurrent
– A ! B and B ! A implies A || B
Lamport’s Logical Clocks
• Goal: assign timestamps to events such that
If A B then timestamp(A) < timestamp(B)
• Lamport’s Algorithm
– Each process i maintains a logical clock Li
– Whenever an event occurs locally at i, Li = Li+1
– When i sends message to j, piggyback Li
– When j receives message from i, Lj = max (Li, Lj) + 1
• In this algorithm, A B implies L(A) < L(B), but
L(A) < L(B) does not necessarily imply A B
4
2
2/13/2012
Lamport’s Logical Clocks: An Example
1 2 3 4 7
P2
E21 E22 E23 E24 E25
Totally Ordered Multicast
• We need to guarantee that concurrent updates on a replicated database
are seen in the same order everywhere. This requires a totally‐ordered
multicast
– Update 1: add $100 to an account (initial value = $1000)
– Update 2: add 1% interest to account
– In absence of proper synchronization: replica 1 = $1111, replica 2 = $1110
• Lamport’s logical clocks can be used to implement totally‐ordered
multicast in a completely distributed fashion
3
2/13/2012
Implementing Totally Ordered
Multicast
• Assumptions:
– No messages are lost
– Messages from the same sender are received in the order they were
sent
• Process Pi sends timestamped message mi to all others. The message
itself is put in a local queuei.
• Any incoming message at Pj is queued in queuej, according to its
timestamp, and ACKed to every other process.
• Pj passes a message mi to its application if
– mi is at the head of queuej
– mi has been ACKed by each other process
• Observation: all processes will eventually have the same copy of the
local queue, therefore, all messages are delivered in the same order
everywhere
Causality
• Lamport’s logical clocks:
– If A B then L(A) < L(B)
– Reverse is not true!!
• Nothing can be said about events
by comparing timestamps!
• Need to capture causality
– If A B then A causally
precedes B Event A: m1 is received at t=16
Event B: m2 is sent at t=20
– Need a timestamping L(A)<L(B), but A does not causally
mechanism such that: precede B.
• T(A) < T(B) iff A causally precedes B
8
4
2/13/2012
Vector Clocks
• Each process i maintains a vector clock Vi of size N, where N is the number of
processes
– Vi[i] = number of events that have occurred at process i
– Vi[j] = number of events i knows have occurred at process j
– Initially, Vi[j]= 0 for all i and j
• Update vector clocks as follows
– Local event at pi: increment Vi[i] by 1
– When a message is sent from pi to pj : piggyback vector Vi
– Receipt of a message from pi by pj :
Vj[k] = max(Vj[k],Vi[k]), j≠k
Vj[j] = Vj[j]+1
Receiver is told about how many events the sender knows occurred at another process k
• We have V(A)<V(B) iff A causally precedes B!
– V(A)<V(B) iff for all i, V(A)[i] ≤ V(B)[i] and there exists k such that V(A)[k] < V(B)[k]
• A and B are concurrent iff V(A) !< V(B) and V(B) !< V(A)
1 E31 E32 2
P3
(0,0,1) (0,0,2)
Example contrasting vector and scalar clocks
10
5
2/13/2012
Causally‐Ordered Multicasting
• Causally‐ordered multicasting: a message is delivered only if all
messages that causally precede it have also been delivered
• Implementing causally‐ordered multicasting:
If Pj receives a message from Pi , delay delivery of the message until
1. ts(m) [i] == VCj[i] + 1 (m is the next expected message from Pi)
2. ts(m) [k] <= VCj[k] for all k ≠ i (Pj has seen all messages seen by Pi when it
sent m)
11