In this lesson · topic 5 of 7
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:
- X writes
[milk]via node A. Version{A:1}. - Y reads that, adds eggs, and writes via node B. Version
{A:1, B:1}. It descends from{A:1}, so it replaces it. - 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
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
- Giuseppe DeCandia and colleagues, Dynamo: Amazon’s Highly Available Key-value Store, ACM SOSP, 2007. Sections on data versioning and vector clock truncation.
- Martin Kleppmann, Designing Data-Intensive Applications, O’Reilly, 2017. Chapter 5 covers detecting concurrent writes, version vectors and siblings.