Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Logical and vector clocks

How to order events in distributed systems using Lamport (logical) clocks and vector clocks, what they guarantee, how to compute them, and exam-style worked traces.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Two or three sentences: In distributed systems there is no single global physical clock you can trust for ordering events. Engineers use logical clocks (Lamport clocks) and vector clocks to reason about causality between events, detect concurrent operations, and implement correctness in protocols such as distributed snapshots, replication, and ordering in messaging.

Key ideas

Start from first principles and simple definitions.

  • Event: an occurrence at a process (local computation, send, or receive). Each process has a sequence of events.

  • Process: a single thread of control in a distributed system that executes events and sends/receives messages.

  • Happens-before relation (→): defined by Leslie Lamport as the smallest relation satisfying:

    1. If e and f are events in the same process and e occurs before f, then e → f. (local order)
    2. If e is the sending of a message m by one process and f is the receipt of that same message m by another process, then e → f. (message send→receive)
    3. If e → g and g → f then e → f. (transitivity) This relation is a strict partial order on events.
  • Concurrent events: Two events a and b are concurrent (written a || b) if neither a → b nor b → a.

  • Lamport (logical) clock: a scalar counter assigned to events; to produce a total order consistent with →, compare pairs (timestamp, processID) lexicographically to break ties. Definition and rules:

    • Each process P_i maintains an integer counter C_i, initially 0.
    • For any local event, increment: C_i := C_i + 1; timestamp event with C_i.
    • For a send event, increment, timestamp the message with C_i, then send.
    • When receiving a message with timestamp t, set C_i := max(C_i, t) + 1; timestamp receive event with C_i.

    The crucial property (Lamport's clock property): if a → b then L(a) < L(b). This shows logical clocks are consistent with causality. Note: the converse is false: L(a) < L(b) does not imply a → b.

  • Vector clock (VC): a vector of N integers for a system of N processes that captures causality exactly (gives a partial order). Definitions and rules:

    • Each process P_i maintains vector V_i of length N, initialized to all zeros.
    • For a local event at P_i: V_i[i] := V_i[i] + 1; timestamp event with copy of V_i.
    • For a send event from P_i: increment as for local event, attach copy of V_i to message, send.
    • For a receive event at P_j with attached vector T: for each k from 1..N: V_j[k] := max(V_j[k], T[k]); then V_j[j] := V_j[j] + 1; timestamp receive event with copy of V_j.

    Define componentwise order on vectors: for two vectors x and y, x ≤ y iff for all i, x[i] ≤ y[i]. Say x < y iff x ≤ y and x ≠ y (i.e. at least one component strictly less). Then vector clocks give the exact relation: e → f iff VC(e) < VC(f) (componentwise). Thus vector clocks can distinguish concurrency: if neither VC(e) < VC(f) nor VC(f) < VC(e), then e || f.

Assumptions behind both schemes:

  • Reliable FIFO delivery is not required for the definitions, but message matching (each receive corresponds to that send) is needed to reason about the message relation.
  • Processes increment counters atomically before timestamping events.

Derivation universities commonly ask for (Lamport property):

  1. Suppose e → f. By definition, there is a chain e = e0 → e1 → ... → ek = f where each step is either same-process next-event or send→receive.
  2. Show by induction on chain length that L(e0) < L(e1) < ... < L(ek). Base: for local next-event, counter increments ensure strict increase. For send→receive step with message timestamp t, receiver sets C := max(C, t) + 1, so receive timestamp > t which was send timestamp; thus strict increase. Induction gives L(e) < L(f).
  3. Therefore e → f implies L(e) < L(f).

Why Lamport scalar cannot detect concurrency: provide a counterexample politely in worked examples.

Diagram to draw for exam answers (describe exactly):

  • Draw three vertical lines labelled P1, P2, P3 (time flows down). Place events as dots on each line: e1,e2,e3... Draw arrows from send event on one line to matching receive event on another; label each arrow with message name m1, and with attached timestamp when required. Beside each event write its Lamport timestamp or vector timestamp in a small box.

Formulas

Each formula on its own line with meaning. Lamport increment on local/send: C_i := C_i + 1 (C_i is integer logical clock at process i, unit: scalar count; apply before timestamping event) Lamport on receive: C_i := max(C_i, t) + 1 (t is timestamp attached to received message; unit: scalar count) Vector local/send: V_i[i] := V_i[i] + 1 (V_i is vector clock at process i; V_i[i] is i-th component) Vector on receive: for k=1..N: V_i[k] := max(V_i[k], T[k]); then V_i[i] := V_i[i] + 1 (T is vector attached to message) Vector comparison: x ≤ y ↔ (∀k, x[k] ≤ y[k]) ; x < y ↔ (x ≤ y and ∃j: x[j] < y[j]) (order used to compare VCs)

No SI units apply because timestamps are dimensionless counters.

Worked examples

Example 1 — Lamport clocks on 3 processes (numerical trace): System: P1, P2, P3. Events timeline description to draw: P1 has e1(internal), e2(send m to P2); P2 has e3(receive m), e4(send n to P1); P1 has e5(receive n). Initial counters C1=C2=0. Step-by-step with formulas:

  1. P1 e1: C1 := 0+1 = 1. Timestamp e1 = 1.
  2. P1 e2 (send m): C1 := 1+1 = 2. Attach t=2 and send m. Timestamp e2 = 2. (formula used: C := C+1)
  3. P2 e3 (receive m with t=2): C2 := max(0,2)+1 = 3. Timestamp e3 = 3.
  4. P2 e4 (send n to P1): C2 := 3+1 = 4. Attach t=4. Timestamp e4 = 4.
  5. P1 e5 (receive n with t=4): C1 := max(2,4)+1 = 5. Timestamp e5 = 5. Final timestamps: P1 events 1,2,5; P2 events 3,4. Check happens-before: e2 → e3 and 2 < 3, e4 → e5 and 4 < 5. But if there was an event at P3 with timestamp 6 that is unrelated, Lamport order may say 5 < 6 but we cannot conclude causality. Answer: final Lamport timestamps as above (bold: final table). This shows L(e) < L(f) when e → f.

Example 2 — Vector clocks showing exact causality and concurrency (trace): System with 3 processes P1,P2,P3. Use events:

  • P1: a (local), b (send m to P2)
  • P2: c (receive m), d (send n to P3)
  • P3: e (receive n) Initialize V1=V2=V3=[0,0,0]. Step-by-step:
  1. P1 a: V1 := [1,0,0]. Timestamp a = [1,0,0].
  2. P1 b (send m): V1 := [2,0,0]. Attach [2,0,0]. Timestamp b = [2,0,0].
  3. P2 c (receive m with T=[2,0,0]): first componentwise max: V2 := max([0,0,0],[2,0,0]) = [2,0,0]; then V2[2] := V2[2] + 1 → V2 := [2,1,0]. Timestamp c = [2,1,0]. (Note indexing: P2 is index 2)
  4. P2 d (send n): V2 := [2,2,0]. Attach [2,2,0]. Timestamp d = [2,2,0].
  5. P3 e (receive n with T=[2,2,0]): V3 := max([0,0,0],[2,2,0]) = [2,2,0]; then V3[3] := V3[3] + 1 → V3 := [2,2,1]. Timestamp e = [2,2,1]. Now compare: b has VC [2,0,0], c has [2,1,0], and e has [2,2,1]. We can check b < c < d < e using componentwise order; that matches causal chain. If there were an independent event f at P3 with VC [0,0,1] then f and b would be concurrent because neither vector ≤ the other. Show one concurrency case: if P3 had f: V3 becomes [0,0,1] with no messages; compare b=[2,0,0] and f=[0,0,1]: neither ≤ the other, so b || f. This demonstrates exact detection: VC(a) < VC(b) iff a → b.

Common mistakes

  • In vector clocks, updating V[i] before merging incoming T (order matters only that you take max first, then increment own component). Doing increment then max gives incorrect results.
  • Comparing vector clocks incorrectly by comparing only sums or single components instead of componentwise relation.
  • Assuming Lamport timestamp order implies causal order (converse error). Many students write "L(a) < L(b) ⇒ a → b" — this is incorrect.
  • Using vector clocks with wrong length or not initializing all components to zero.

For your exam

Kinds of questions:

  • Definitions: defines happens-before, concurrency, Lamport clock, vector clock, partial order.
  • Short notes: explain why Lamport clocks are not sufficient to detect concurrency, why vector clocks are stronger, complexity trade-offs.
  • Derivations: prove Lamport property (if a → b then L(a) < L(b)) by induction on chain length.
  • Traces (algorithmic): given a small event diagram, compute Lamport timestamps or vector timestamps step-by-step.
  • Diagram questions: draw process timelines and message arrows and annotate timestamps.
  • Complexity/analysis: memory and message overhead of vector clocks is O(N) per timestamp (per process state and per message); discussion of scalability and alternatives (briefly mention but do not teach here: refer to topics like clock synchronisation and consensus).

What a full-marks answer includes:

  • Precise definitions with bold terms and clear assumptions.
  • Correct algorithm rules (increment before send; receive rule exact formula).
  • A correct, stepwise trace for examples with all intermediate values and final timestamps.
  • A short proof or counterexample where asked (Lamport property proof; counterexample showing L(a) < L(b) does not imply a → b).
  • Clear diagram when required: vertical lines for processes, labeled events, arrows for messages, timestamps beside events.

Quick check

  1. State the happens-before relation three defining rules.
  2. Give the Lamport receive update rule.
  3. How do you decide from two vector timestamps x and y whether the corresponding events are concurrent? Answers: 1) local order, send→receive, transitivity. 2) C := max(C, t) + 1 where t is message timestamp. 3) Events are concurrent if neither x < y nor y < x under componentwise order (i.e., not (∀k x[k] ≤ y[k]) and not (∀k y[k] ≤ x[k])).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?