Try
↑ ↓ move↵ openesc close
Quorums5 / 7
In this lesson · topic 5 of 7
  1. 1. What if there is no leader
  2. 2. Overlap, and the k-th fastest reply
  3. 3. Tune R and W against slow and dead replicas
  4. 4. What R plus W above N still does not promise
  5. 5. Two writes at once, and which one wins
  6. 6. Merkle trees, expiring hints, and deleted data coming back
  7. 7. Check the quorum arithmetic

Topic 5

Two writes at once, and which one wins

6 min read

A quorum read returns the newest version among the replies. That only makes sense if “newest” is well defined. With no leader to order the writes, two clients can update the same key at the same moment, and neither write is later than the other.

Last write wins

The simplest rule stamps every write with a timestamp and keeps the highest. Take a cart holding [milk]. Client X adds eggs, and client Y adds bread, both having read [milk]. X writes [milk, eggs] at 10:00:00.120. Y writes [milk, bread] at 10:00:00.125.

Every replica ends up with [milk, bread]. The eggs are gone, and X was told the write succeeded. If Y’s clock runs 50 ms ahead of X’s, then Y’s write wins even when it happened first. Last-write-wins is simple and always converges, and it loses data without any sign. It is a fair choice for values where overwriting is the intent, such as a profile photo, and a poor one for a cart.

Version vectors

A version vector replaces the timestamp with a counter per node that has coordinated writes for the key. A version labelled {A:2, B:1} means “two writes coordinated by A and one by B went into this value”.

One version descends from another if its counters are all at least as large. Then it replaces the other. If neither descends from the other, the writes were concurrent.

Walk the cart through it:

  1. X writes [milk] via node A. Version {A:1}.
  2. Y reads that, adds eggs, and writes via node B. Version {A:1, B:1}. It descends from {A:1}, so it replaces it.
  3. Z had also read {A:1} earlier, and adds bread via node C. Version {A:1, C:1}.

Compare {A:1, B:1} with {A:1, C:1}. Neither has all counters at least as large, because one has B and the other has C. They are concurrent, so the store keeps both as siblings, and the next read returns both with their version labels.

Predict first

Replica 1 holds a value with version {A:2, B:1}. Replica 2 holds one with {A:1, B:2}. A quorum read sees both. What happens?

Merging is the application’s job

The store cannot know what the right merge is. For a cart, the application takes the union: [milk, eggs, bread]. It writes that back with a version that descends from both siblings, which retires them.

Union has its own flaw. If X had removed milk on one branch, the union brings milk back. The Dynamo paper accepts this: an item deleted from a cart can reappear, which was judged better than losing an added item. Other data types need other merge rules. A counter can sum the deltas, and a set that must support removals needs to remember what was removed.

Keeping vectors small

Each node that coordinates a write for the key can add an entry, so vectors grow. The Dynamo paper bounds them. When a vector passes a threshold, 10 entries in the paper, the oldest entry is dropped. A dropped entry can make a descendant look concurrent, which only costs an extra merge, not lost data. The paper reports no problems from this in production.

Why the quorum is not enough here

Sources

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms