LESSON 03 · Ordering

Order events without trusting clocks

Machine clocks disagree enough to reverse cause and effect, so systems that need order use counters passed along with messages.

SYSTEM VIEW

The agreement path

Diagram view
A’s stamp—
B’s stamp—
Price kept—

Watch two writes to one price in real time, stamped by clocks that disagree, then replay them with counters that travel with messages.

  1. Nodes A and B settle conflicts with last-write-wins: the highest timestamp counts as the latest write. But clocks drift, and A’s runs 30 ms fast. Client 1 writes price = 90 through A at 12:00:00.070, and A stamps it .100.

    A’s clock
    30 ms fast
    Axis 0 ms
    12:00:00.065
    Real time
    .070

    What changed

    • A’s clock 30 ms fast
    • 90 stamped .100
  2. Client 2 reads that 90, then writes price = 80 through B, 15 ms after A’s write. B’s clock is accurate, so it stamps .085. The later write, made after seeing 90, carries the lower stamp.

    A’s write
    at .070, stamp .100
    B’s write
    at .085, stamp .085

    What changed

    • Client 2 reads 90
    • 80 stamped .085
  3. The nodes swap writes, and each keeps the one with the higher stamp. .100 beats .085, so both keep 90. The later, better-informed write disappears, and nobody gets an error.

    Real order
    90, then 80
    By stamp
    80, then 90

    What changed

    • Both nodes keep 90
    • Client 2’s 80
  4. Replay it with a Lamport clock. Each node keeps a counter, adds one on every event, and attaches it to every message. A’s counter is at 7, so Client 1’s write becomes event 8: stamp 8, whatever A’s clock says.

    Each event
    counter + 1
    A’s write
    stamp 8

    What changed

    • Stamps from counters
    • 90 stamped 8
  5. Client 2 reads 90 with its stamp 8 and carries the 8 to B with its write. B sets its counter to the larger of its own 5 and that 8, plus one, so the write gets stamp 9.

    On receiving
    max(own, stamp) + 1
    B’s counter
    max(5, 8) + 1 = 9

    What changed

    • Client 2 carries 8
    • 80 stamped 9
  6. The nodes swap writes again. 9 beats 8, so both keep 80, the write made after seeing 90. If one event could have caused another, the cause has the lower stamp.

    Real order
    90, then 80
    By stamp
    90, then 80

    What changed

    • Both nodes keep 80
    • A’s 90 → 80
  7. Later, Clients 1 and 2 claim the username ada at the same moment, through A and B. A stamps 12; B, having counted more events, 15. Neither saw the other, yet the stamps order them: a lower stamp doesn’t prove a cause. Spotting concurrency takes version vectors.

    By stamp
    12 before 15
    Real time
    the same moment
    Spots this
    version vectors

    What changed

    • ada claimed at A, 12
    • ada claimed at B, 15
  8. Each node answers from what it has seen, so both clients hear yes. B can’t rule out a lower-stamped claim it hasn’t heard of: A’s, stamp 12, arrives afterwards and sorts first, so B’s yes was wrong. Stamps order history after the fact; deciding now takes agreement.

    Stamps give
    order afterwards
    Deciding now
    takes agreement

    What changed

    • Both clients told yes
    • A’s 12 sorts first

Space plays · ← → step · Home resets

THE IDEA

How it works

Many systems resolve conflicts with last-write-wins: the highest timestamp counts as the latest write. That assumes machine clocks agree, and they do not. Quartz oscillators drift, NTP corrections can jump a clock forward or backward, and machines can end up tens of milliseconds apart. A monotonic clock is safe for measuring durations on one machine, but its values mean nothing elsewhere.

Node A’s clock runs 30 ms fast. A client writes price = 90 through A, stamped 12:00:00.100. Another client reads that 90 and, 15 ms later, writes price = 80 through node B, whose accurate clock says 12:00:00.085. Last-write-wins keeps 90, because its stamp is higher. The later, better-informed write disappears with no error.

A Lamport clock orders events without reading time. Each node keeps a counter, adds one on every event, and attaches it to every message. On receiving a stamp, a node sets its counter to the larger of its own value and the stamp, plus one. Ties break by node ID. Replay the example: A’s write gets stamp 8, the second client carries 8 to B, and B’s write gets 9 and wins. If one event could have caused another, the cause has the lower stamp.

The reverse does not hold. A lower stamp does not prove causation, and unrelated events still get an order; detecting true concurrency takes version vectors. Stamps also help only in hindsight. A node answering “is this username free?” cannot decide from them, since another node may hold a lower-stamped claim it has not heard about. Deciding now takes agreement.

DECISION POINT

A collaborative notes service stores edits on three nodes with last-write-wins on server time, and customers report edits vanishing. One engineer wants to tighten NTP polling, another wants Lamport stamps, and a third wants to keep concurrent edits as separate versions. Which do you back?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Last-write-wins on wall clocks can silently discard a causally later write.
  • Lamport stamps respect causality but cannot show that two events were concurrent.
  • Stamps order history after the fact; deciding now takes agreement.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

Node B’s Lamport counter is 4 when a message stamped 11 arrives. Counting the receipt as an event, what stamp does B give it?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.