Skip to content

Logical clocks: Lamport and vector clocks

The last lesson ended with a clock that could not be trusted to order events. Lamport’s answer was to stop asking what time it was and ask what could have influenced what. Event a happened before event b if a came earlier in the same process, or if a is the sending of a message and b is its receipt, or if the two are linked by a chain of such steps. If neither happened before the other, they are concurrent: nothing that happened in one could have affected the other. Lamport notes that this makes “happened before” only a partial ordering.

Concurrent does not mean simultaneous. It means unrelated by any chain of messages, and so there is no fact to be found about which came first.

Mental model. A family tree. Parents come before children and grandparents before grandchildren, whatever the birth certificates say about dates. Two cousins who never met are not ordered at all.

Lamport clocks: a counter that respects causes

Section titled “Lamport clocks: a counter that respects causes”

A logical clock is a counter in each process. Lamport’s two rules are: each process increments its clock between any two successive events, and when a message arrives, the receiver sets its clock greater than or equal to its present value and greater than the timestamp the message carried. A message carries the sender’s clock at the time of sending.

With these rules the clock obeys the Clock Condition: if a happened before b, then the clock value of a is less than that of b. Lamport also notes the converse cannot hold. A smaller number does not prove an earlier cause: the converse would force any two concurrent events to carry the same time.

A vector clock keeps one counter for each process and passes the whole list along with messages. In the lab, a process increments its own entry at every event, and on receipt first takes the larger of each pair of entries. Dynamo describes its version as a list of (node, counter) pairs attached to every version of every object: if the counters of the first clock are less than or equal to those of the second in every entry, the first is an ancestor of the second. Otherwise the two are considered to be in conflict and need reconciling.

Here is the same run as in the last lesson. The table has the Lamport and vector clock of each event.

Three processes exchange messages. Skew one clock, and compare what physical timestamps, Lamport clocks and vector clocks say about which event came first.

-50 ms
60 ms
P1P2P311 [1, 0, 0]22 [2, 0, 0]31 [0, 0, 1]43 [2, 1, 0]54 [2, 2, 0]65 [2, 3, 0]76 [2, 3, 2]

Each dot is an event, numbered in true-time order. Next to it: its Lamport clock, then its vector clock. An arrow is a message.

P3 receives a message from P2. Its own clock reads 60 ms, Lamport 6, vector [2, 3, 2].

Results of the run

Pairs of events where one caused the other
16
Of those, put in the wrong order by physical clocks
6
Of those, put in the wrong order by Lamport clocks
0
Pairs of concurrent events
5
Concurrent pairs that Lamport clocks still put in order
4
Physical clocks put the reply before the post
Yes
Events drawn so far, with the three kinds of timestamp
ProcessEventTrue timeOwn clockLamportVector
P1publishes a post10 ms10 ms1[1, 0, 0]
P1sends a message to P212 ms12 ms2[2, 0, 0]
P3does something of its own20 ms20 ms1[0, 0, 1]
P2receives a message from P130 ms-20 ms3[2, 1, 0]
P2replies to the post40 ms-10 ms4[2, 2, 0]
P2sends a message to P342 ms-8 ms5[2, 3, 0]
P3receives a message from P260 ms60 ms6[2, 3, 2]
Every event in words
Every event of the run, in time order
At secondEvent
10 msP1 publishes a post. Its own clock reads 10 ms, Lamport 1, vector [1, 0, 0].
12 msP1 sends a message to P2. Its own clock reads 12 ms, Lamport 2, vector [2, 0, 0].
20 msP3 does something of its own. Its own clock reads 20 ms, Lamport 1, vector [0, 0, 1].
30 msP2 receives a message from P1. Its own clock reads -20 ms, Lamport 3, vector [2, 1, 0].
40 msP2 replies to the post. Its own clock reads -10 ms, Lamport 4, vector [2, 2, 0].
42 msP2 sends a message to P3. Its own clock reads -8 ms, Lamport 5, vector [2, 3, 0].
60 msP3 receives a message from P2. Its own clock reads 60 ms, Lamport 6, vector [2, 3, 2].

Illustrative values, not benchmarks of any real product.

Model notes

Assumptions

  • P1 publishes a post and tells P2. P2 replies. P3 does something of its own, then hears from P2. The reply is caused by the post.
  • Every message takes 18 ms. The clocks of P1 and P3 are exact, and only the clock of P2 is off, by the amount you choose.

Simplifications

  • A Lamport clock numbers events so that a cause always has a smaller number than its effect. The reverse does not hold: a smaller number does not mean a cause.
  • The vector clocks here have one slot per process, three in all. A system with many clients needs ways to keep them small.

Parameter values are illustrative. They are not benchmarks of any real product.

Event Lamport Vector
P1 publishes a post 1 [1, 0, 0]
P1 sends a message to P2 2 [2, 0, 0]
P3 does something of its own 1 [0, 0, 1]
P2 receives the message from P1 3 [2, 1, 0]
P2 replies to the post 4 [2, 2, 0]
P2 sends a message to P3 5 [2, 3, 0]
P3 receives the message from P2 6 [2, 3, 2]

Move Clock of P2 compared with true time to any value: these columns do not change, because neither counter reads a physical clock. The Lamport clocks put 0 of the 16 cause-and-effect pairs in the wrong order at every skew, where the physical clocks put 6 in the wrong order at the starting skew.

Now the concurrent pairs. There are 5, and every one of them involves P3’s own event, which no message links to the others until the end. Look at the reply (4) and the thing P3 did on its own (1). The Lamport numbers say the second is earlier. But the vectors [2, 2, 0] and [0, 0, 1] are not comparable: the first is larger in its first two entries and the second in its last. Neither caused the other. The lab shows that the Lamport clocks still put 4 of the 5 concurrent pairs in an order, and only one pair, the post and P3’s event, ends up with the same number. A vector clock calls all 5 concurrent.

What happens if you need a single order anyway?

Section titled “What happens if you need a single order anyway?”

Some jobs need every process to agree on one order, even for concurrent events, such as deciding who gets a resource first. Lamport’s paper does it by breaking ties with an arbitrary, fixed order of the processes: events are ordered by their clock value, and equal values by process. The order of concurrent events is arbitrary, but it is the same for everybody. In the run above, the post (1, P1) and P3’s own event (1, P3) tie, and P1 goes first.

And the cost of a vector clock? One entry per process, sent with every message, which grows with the number of processes. Dynamo says that when writes are handled by nodes outside the usual few, the size of a vector clock can grow, so it keeps a timestamp with each pair and, when the number of pairs reaches a threshold (it says 10 as an example), removes the oldest. It admits that this loses accuracy in telling descendants apart. So the choice is: a Lamport clock if you only need an order consistent with causality, a vector clock if you must detect conflicts, and a bit of care about size.

  • Happened-before orders events by cause and effect. Events that no chain of messages links are concurrent.
  • A Lamport clock increments at every event and jumps past the timestamp of a received message. If a caused b, a’s number is smaller, but a smaller number does not prove a cause.
  • In the lab, the Lamport clocks misorder 0 of 16 cause-and-effect pairs at every skew. They still order 4 of the 5 concurrent pairs, and the vector clocks call all 5 concurrent.
  • A vector clock has one counter per process. It tells cause from concurrency, at the price of a size that grows with the number of processes.

That is the end of the module’s lessons. The next page gathers them: a recap, a checklist for reasoning about consistency and time, and a quiz that covers all four.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.