Back to journal Which event happened first?

Which event happened first?

Follow one order between two services to see what a logical clock can prove.

Orders accepts at 1 and sends at 2. The warehouse receives the message at 3 and packs at 4.

An order service logs “approval sent” at 12:00:05. A warehouse logs “approval received” at 12:00:03. Sort those lines by time and the warehouse appears to receive an approval two seconds before it exists. Yet if the entries describe the same message, you already know which came first. What gave you better evidence than either clock?

It was the message. A receive depends on a send, even when the computers disagree about the time. Leslie Lamport built a way to number events from that simple observation. You can do it with an integer on each machine. The numbers preserve every ordering you can establish by following messages. Reading the numbers in reverse to infer who influenced whom is where things get interesting.

Start with arrows

Put each service on its own line. From left to right, write the events a single process carried out in order. Draw a forward arrow between successive events on that line. When one process sends a message, draw an arrow from its send to the matching receive on the other line. A matching message identifier would tell you which endpoints belong together in a real trace.

Orders       accept → send
                       ↘ approval
Warehouse                receive → pack

Start at “accept”. You can walk forward to “send”, across the message arrow to “receive”, then forward to “pack”. Lamport calls this relation “happened before”. Its three ingredients are local order, send before receive, and the ability to join such steps into a path. Even without a direct arrow from accept to pack, the chain establishes their order.

Add a third process that refreshes a weather display. Suppose it exchanges no messages with either service during the trace and has no other causal links to them. Its refresh and the packing event are concurrent in this model. They could have been hours apart on a wall clock. Concurrency here means that neither event has a forward path to the other. You could give the weather display a perfect clock and the missing arrow would still be missing.

Number the drawing

Start each process counter at zero. For an ordinary local event, add one and record the result. For a send, add one and include that number in the message. For a receive, look at your current counter and the number attached to the message. Take the larger, add one, and record it. Treat each send or receive as one event when doing the counting.

Orders accepts at one and sends at two. Warehouse has done nothing yet, so its counter is zero. The approval arrives carrying two. Warehouse takes the larger of zero and two, adds one, and records the receive at three. Its next event, pack, gets four. These are small enough numbers to check against the drawing. Every arrow goes from a smaller number to a larger one.

What if the warehouse counter were already nine when the approval arrived? The receive would get ten. Setting it to the sender’s two plus one would move the warehouse backward past events it had already recorded. The maximum protects the receiver’s own history. Adding one makes the receive strictly later than both the send and the receiver’s previous event.

The inference that works in only one direction

Following any arrow increases the number. Following a whole path therefore increases it too. If event A happened before event B, its number must be smaller. This gives a useful way to reject an explanation. If someone says an event stamped eight caused an event stamped five through recorded process steps and messages, that claim contradicts the rule.

The easy mistake is to turn that reasoning around. Give the independent weather process a single refresh at one. The warehouse packs at four. One is smaller than four, yet the weather report never reached the warehouse. For an even sharper example, give the weather process three more local events. Its fourth event and packing now both have stamp four. A stamp can occur on several processes, even though each process increments its own counter correctly.

A numerical gap is equally easy to misread. A receive might jump from zero straight to ten because a busy sender attached nine to a message. That jump takes one event at the receiver. You cannot read ten seconds, ten network hops, or ten warehouse operations off that timestamp. To measure delivery time you need a timing method with known clock assumptions. To recover the path you need the message links.

What a sorted log promises

Suppose a developer wants everyone inspecting the same set of events to see the same list. Give each process a unique identifier and agree on how to order identifiers. Sort by the pair consisting of logical timestamp first and process identifier second. All observers with the same events and the same rule now get the same list. Paths you established from messages remain in order because their timestamps were already strictly increasing.

Try it with the two events stamped four. Call the processes “warehouse” and “weather”. Alphabetical order puts warehouse first. Rename the processes “Z” and “A”, respectively, and the weather event wins the tie. No message has moved. This is a good test for what the sorted log can support. An order that changes when you rename the machines cannot, on its own, explain a dependency between the events.

There is also a difference between sorting a complete log and acting while messages are still arriving. Imagine you have already seen the event stamped four when a delayed message brings a previously unseen event stamped two. Sorting a file afterward is easy. A service that already acted on four has a different problem. A timestamp by itself supplies no evidence that all earlier messages have arrived. Lamport’s paper adds messages and acknowledgments to address that issue in its mutual-exclusion example.

A small trace you can change

The Python below follows the approval message on the two counters. The warehouse records one local event before receiving it. The last line stands for packing the order. Predict each value before running it. Then raise the warehouse counter to nine just before the receive. Only the receiver and its later events should change.

def receive(local, incoming):
    return max(local, incoming) + 1

orders = 0
orders += 1                # Accept the order.
orders += 1                # Send the approval.
approval_stamp = orders

warehouse = 1              # One earlier local event.
warehouse = receive(warehouse, approval_stamp)
assert (approval_stamp, warehouse) == (2, 3)

warehouse += 1             # Pack the order.
assert warehouse == 4
assert receive(9, approval_stamp) == 10

Now add a reply message. Starting from four, Warehouse increments to five and sends “packed”. Orders has stayed at two while waiting. When it receives the reply, its counter becomes six. You can trace an uninterrupted path from accepting the order to receiving confirmation. No physical clock reading was needed. If Orders performed three unrelated local events while waiting, the receive still obeys the same maximum rule.

Reading a real incident

Return to the impossible-looking log at the beginning. First match the send and receive by message identifier. Next check the local order on each participating process. Only then build the path whose existence you want to establish. If a service hands work to another worker through a queue, that handoff belongs in the trace too. Leaving it out can make connected events look independent.

A missing log entry leaves two possibilities that a complete theoretical drawing keeps separate. Two events might truly be concurrent, or a real message connecting them might have gone unrecorded. A smaller logical timestamp can be consistent with either story. It tells you where an arrow could point. The matching send and receive are what let you draw it.

Sources

Claims to verify

  • Trace the approval and reply. Every message arrow ends at a larger number.
  • Give the independent weather process four events and compare its last timestamp with packing.
  • Reverse the process names and sort the tied events again.