Skip to content
SEE Exam Prep

Question 8 of 15 · CIE-1 QB · Set 1 · 7 marks

Open in ChatGPT

What are Logical Clocks? Explain Lamport's logical clock algorithm with a suitable example. Explain Lamport logical clocks and derive the rules for updating timestamps during local events, message sending, and message receiving.


Introduction

Physical clocks on different machines drift, so two events on two machines cannot be ordered by their timestamps. Lamport's insight was that a distributed system needs only a partial order based on causality, and that a simple counter is enough to capture it.

The happened-before relation

a → b (a happened before b) if:

  1. a and b are in the same process and a occurs first, or
  2. a is the sending of a message and b is its receipt, or
  3. there is a c such that a → c and c → b (transitive).

If neither a → b nor b → a, the events are concurrent (a || b). Causal order is a partial order, not a total one.

Lamport's algorithm

Each process keeps a counter C. Three rules:

  1. Local event: before an event, C = C + 1.
  2. Sending a message: send the message with the current value of C, i.e. (m, C).
  3. Receiving a message with timestamp t: C = max(C, t) + 1.

A timestamp is then the integer on each event, and for every a → b the algorithm guarantees C(a) < C(b).

Diagram

Two processes, each with a logical clock axis; a message from e12 at time 2 arrives at e23, which takes the timestamp 3, so the receive is stamped after the send

Worked example

  • P1 sends m1 with timestamp 2.
  • P2 receives it, so P2's next event is max(0, 2) + 1 = 3.
  • P2 later sends m2 with timestamp 5.
  • P1 receives it, so P1's next event is max(3, 5) + 1 = 6.
  • P1's later event therefore carries 6, which is greater than every event it causally follows.

Limitation

  • The converse is not true: C(a) < C(b) does not prove a → b.
  • Two concurrent events can receive any relative timestamps.
  • So Lamport clocks cannot detect concurrency.
  • Vector clocks fix this by keeping one counter per process.

Conclusion

Lamport clocks give a cheap, total timestamp order that respects causality and is enough for mutual exclusion and total-order multicast, at the cost of being unable to tell concurrent events apart.