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.
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?
Tighter NTP narrows the window but cannot close it. Lamport stamps fix the causal case, where an edit made after seeing another now wins, but two truly concurrent edits still lose one silently. For notes, losing someone’s text is the real harm, so detect concurrency with version vectors and keep both versions for a merge. That costs merge logic and metadata. Lamport stamps alone suit data where picking one winner is acceptable.
Use this as a guide for self review; written answers are not graded.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.