Learn
Lesson · Replication and Consistency · Clocks and order

Lamport Clocks

A Lamport clock is one counter per process and one rule: L(e) = max(L_local, L_msg) + 1. It produces a total order that never contradicts causality — if a happened before b then L(a) < L(b) — and the converse is false. On an eleven-event diagram there are 32 ordered pairs and 17 further pairs whose stamps are ordered while the events are not, which is exactly the gap the next lesson closes.

L(e) = max(L_local, L_msg) + 1 a send carries the sender's L a → b ⟹ L(a) < L(b) sound, and useful L(a) < L(b) ⟹ a → b FALSE 11 events, 3 messages: 32 pairs are ordered by happens-before and 17 further pairs have L(a) < L(b) with a not before b
One rule, and the implication that does not hold

Core ideas

What this lesson establishes

The rule has three cases, the guarantee has one direction, and the counterexample is on the same diagram you stamped.

1

The rule is three cases and a counter

Each process keeps one integer. On a local event it increments it. On a send it increments and attaches the new value to the message. On a receive it takes the maximum of its own counter and the one on the message, then increments. The max is what carries information across processes, and the + 1 is what keeps the stamp of the receive strictly after the send.

2

The stamps are sound, not complete

If a happened before b — by process order, by a message, or by a chain of those — then L(a) < L(b), because every step of the chain strictly increases the stamp. That is the guarantee and it is genuinely useful: sorting by L, with process identifiers breaking ties, gives a total order that never puts an effect before its cause.

3

A smaller stamp is not evidence of anything

The converse fails and it fails often. On this diagram L(B1) = 1 and L(A2) = 2, yet no chain of process order and messages runs from B1 to A2 — they are concurrent. There are 17 such pairs here. What L(a) < L(b) does rule out is b → a, by the contrapositive of soundness, and that is all it rules out.

The material

The rule, the guarantee, and the direction that fails

How the counters propagate along a diagram, what the resulting order promises, and a pair of events it gets the wrong way round.

Definition · Happens-before, and the Lamport timestamp

Event a happens before b, written a → b, if they are on the same process and a is earlier, or a is the send of a message whose receive is b, or there is a chain of such steps from a to b. The Lamport timestamp L assigns each event max(L_local, L_msg) + 1, where L_msg is the stamp carried by a received message and is absent otherwise.

Happens-before is a partial order, not a total one: it is the transitive closure of “same process, earlier” and “sent, then received”, and any two events not connected by such a chain are unrelated by it. A Lamport clock takes that partial order and produces integers consistent with it.

processes A (4 events), B (4 events), C (3 events) messages A2 → B2 B3 → C2 C1 → A4 A A1 = 1 A2 = 2 A3 = 3 A4 = 4 B B1 = 1 B2 = 3 B3 = 4 B4 = 5 C C1 = 1 C2 = 5 C3 = 6 the three receives, worked B2 = max(L(B1), L(A2)) + 1 = max(1, 2) + 1 = 3 C2 = max(L(C1), L(B3)) + 1 = max(1, 4) + 1 = 5 A4 = max(L(A3), L(C1)) + 1 = max(3, 1) + 1 = 4

The third receive is the instructive one. The message from C1 carries the stamp 1, which is smaller than A's own counter at that point, so the local clock wins the maximum and A4 becomes 4 rather than 2. A reader who adds one to the message stamp instead of to the maximum gets a stamp that violates A's own process order, which is the first thing to check when a diagram comes out wrong.

The direction that fails

L(B1) = 1 and L(A2) = 2, so L(B1) < L(A2) but is there a chain B1 → … → A2 ? B1 → B2 → B3 → C2 → C3 and C3 sends nothing the only message into A is from C1, which B cannot reach so no: B1 and A2 are concurrent on this diagram ordered pairs, a → b 32 pairs with L(a) < L(b) and a NOT before b 17

Seventeen against thirty-two is not a rare corner case, it is a third of the ordered-looking pairs on a small diagram. A Lamport stamp is a one-dimensional summary of a partial order, and the information it cannot carry is exactly the information about which pairs are unrelated.

Example · Why a total order is useful anyway

Break ties by process identifier and the stamps give a total order on all events, consistent with causality. That is enough for a distributed mutual-exclusion queue, for a deterministic replay of a log, and for any rule of the form “process requests in a fixed order that everyone agrees on”. What it is not enough for is detecting that two updates were concurrent, because the total order cheerfully orders pairs that nothing ordered — and a merge that trusts it will silently pick a winner between two updates that should both have been kept.

So the honest summary is: Lamport stamps are a cheap way to get an order everyone agrees on, and a wrong way to decide whether one event caused another. One integer per process is all they cost and it is also all they can carry. “Vector Clocks and Concurrency” spends n integers instead and gets the missing direction back.

Interactive

Lamport clocks

A total order consistent with happens-before — and only one way round

Lamport clocksL(e) = max(Llocal, Lmsg) + 1
an event, with its stampa messageL(a) < L(b) with a NOT before b

Method

Stamping a diagram

Draw first, stamp second. Most errors in this arithmetic are errors about which edges exist, not about the maximum.

1

Draw the processes as lines and the messages as arrows between them

One horizontal line per process, events as dots along it in order, an arrow from each send to its receive. Everything that follows reads off this picture, and a missing arrow is invisible in a list of stamps.

2

Work through the events in a topological order

An event can be stamped once every event with an edge into it has been. Going left to right on each process, deferring a receive until its send is stamped, is enough.

3

Apply the rule case by case

Local event: L + 1. Send: L + 1, and put that value on the message. Receive: max(own L, message L) + 1. The commonest slip is adding one to the message's stamp rather than to the maximum, which breaks process order whenever the local clock was ahead.

4

Look for a pair the stamps order that the diagram does not

Pick two events on different processes with different stamps and try to trace a chain from the smaller to the larger. If you cannot, you have found one of the pairs where the total order is inventing an order — and knowing that such pairs exist is the point of the exercise.

Stamps, and what they prove

Score 0 / 0

Stamping eleven events, then finding the false pair

Three processes, eleven events, three messages. The stamps are mechanical; the interesting part is the last third of the page, where a pair of stamps says something the diagram does not support.

A: 4 events B: 4 events C: 3 events messages A2 → B2 B3 → C2 C1 → A4 process A A1 = 0 + 1 = 1 A2 = 1 + 1 = 2 (send, message carries 2) A3 = 2 + 1 = 3 A4 = max(3, 1) + 1 = 4 (receive from C1, which carried 1) process B B1 = 0 + 1 = 1 B2 = max(1, 2) + 1 = 3 (receive from A2) B3 = 3 + 1 = 4 (send, message carries 4) B4 = 4 + 1 = 5 process C C1 = 0 + 1 = 1 (send, message carries 1) C2 = max(1, 4) + 1 = 5 (receive from B3) C3 = 5 + 1 = 6 check soundness on the three messages A2 = 2 < 3 = B2 B3 = 4 < 5 = C2 C1 = 1 < 4 = A4 all hold now the counterexample L(B1) = 1 < 2 = L(A2), but no chain runs B1 → A2 such pairs on this diagram: 17

The stamps are correct and the guarantee holds in the direction it claims: every message is stamped so that the receive is strictly after the send, and every process's own events increase. Sorting all eleven by stamp, with ties broken by process name, gives an order no observer could prove wrong.

And B1 before A2 is a fiction that order contains. Both are first or second events on their own processes, neither has heard of the other, and the stamps nonetheless place one before the other because integers are totally ordered and the events are not. Seventeen of the pairs here are like that.

For a faded rehearsal, add a fourth message A3 → C3 to the diagram. The supplied first move is that C3 is now a receive rather than a local event, so its stamp becomes max(L(C2), L(A3)) + 1. Restamp every event that changes, say whether the number of false pairs should rise or fall and why, then check both against the lab.

Common mistakes

1
Reading a smaller stamp as “earlier”The implication runs one way only. L(a) < L(b) means b did not happen before a, which is weaker than a happened before b and very much weaker than “a is earlier in time”. Any code that decides which of two updates to keep by comparing Lamport stamps is silently resolving concurrency by an arbitrary rule.
2
Incrementing the message's stamp rather than the maximumOn a receive the rule is max(own, message) + 1, and the max matters precisely when the receiving process is ahead. Getting it wrong produces stamps that decrease along a process line, which is both obviously wrong on the diagram and easy to miss in a table of numbers — checking that each process's own stamps increase catches it immediately.
3
Expecting Lamport stamps to detect a conflictThey cannot, by construction: a total order has no way to say “these two are unrelated”. Two concurrent updates always get different stamps and the larger always looks later. If the question is whether two writes conflict, the answer needs one counter per process rather than one in total, which is the next lesson.

Check your understanding

✓
Finish when you can stamp a diagram in one pass and name a pair the stamps order wrongly.

You should be able to apply the three cases of the rule without hesitation, check soundness along every message, produce a total order by breaking ties on process identity, and exhibit two concurrent events whose stamps are nevertheless ordered.

Note: One integer per process cannot record which processes have heard of an event, only how many steps have happened somewhere. “Vector Clocks and Concurrency” spends one integer per process instead, which is enough to answer the question Lamport stamps cannot — and turns “are these two updates in conflict” into a count.

Kept in this browser only, for you. Nothing is sent anywhere and nothing checks it. Carry it to another device.

Feedback and recommendations

Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.

No recommendations yet for this lesson.