The Problem with Physical Time

In a single machine, we can confidently use the system clock to determine the order of events. In a distributed system, this is impossible because:

  • Clocks drift apart.
  • Network latency is variable, making synchronization imperfect (e.g., NTP).
  • A node might pause (Garbage Collection, VM migration) and lose track of time.
Clock Offset DistributionThe probability distribution of clock offsets across a cluster of nodes.
Intuition
Why time matters

Without a shared notion of time, how do we agree on which user made an update first? How do we detect causality between events across different machines?

Logical Time

Since physical time is unreliable, Leslie Lamport proposed a solution in 1978: Logical Clocks. Instead of measuring absolute time (seconds/minutes), logical clocks measure the ordering of events.

DDefinition 2.1
Happens-Before Relation (→)

An event AA happens before event BB (written as ABA \rightarrow B) if:

  1. AA and BB occurred on the same process, and AA happened before BB.
  2. AA is the sending of a message by one process, and BB is the receipt of that message by another process.
  3. Transitivity: If ABA \rightarrow B and BCB \rightarrow C, then ACA \rightarrow C.

Lamport Timestamps

Each process PiP_i maintains a local counter, CiC_i.

Event Ordering with Lamport Clocks

Process 1Process 2Message m1 (C=1)Message m2 (C=3)
1.

Before executing an event, process PiP_i increments its counter: Ci=Ci+1C_i = C_i + 1.

2.

When sending a message mm, PiP_i piggybacks its current counter value on the message: (m,Ci)(m, C_i).

3.

Upon receiving a message (m,Cmsg)(m, C_{msg}), a process PjP_j updates its counter to be greater than both its current value and the message's value: Cj=max(Cj,Cmsg)+1C_j = \\max(C_j, C_{msg}) + 1.

TTheorem 2.2
Lamport Clock Guarantee

If event aa happens before event bb (aba \rightarrow b), then the Lamport timestamp of aa is strictly less than the Lamport timestamp of bb (L(a)<L(b)L(a) < L(b)).

!Common pitfall
The Reverse is Not True

If L(a)<L(b)L(a) < L(b), we cannot conclude that aba \rightarrow b. They might be concurrent events that just happened to be assigned these timestamps. This limitation led to the development of Vector Clocks.