Picture a shopper adding shoes to her cart on her phone while an old laptop tab still holds yesterday's cart. A second later the laptop saves. If your database resolves conflicts with "keep the newest timestamp", the shoes are gone, and nobody sees an error.
That is the quiet failure of last-write-wins (LWW), and it is one of the first things interviewers poke at when you design a distributed key-value store. This post walks through why it fails and the two ideas that replace it: quorums and version vectors.
The problem with "newest wins"
LWW looks attractive because it is one line of logic: tag every write with a timestamp and keep the largest. It has two flaws.
- Clocks lie. Machines in a cluster drift apart by milliseconds or more. "Latest" is really "the machine with the fastest clock", which has nothing to do with what the user did.
- Even perfect clocks drop data. When two writes are truly concurrent (the phone and the laptop both edited the cart from the same starting point), there is no correct "last" one. Picking either throws the other away.
LWW is fine for data where losing a concurrent update costs nothing, like a "last seen at" field. For carts, documents or counters, it silently corrupts state.
Step one: make reads and writes overlap
Before we can detect conflicts, replicas need a rule for how many of them must agree. Store every key on N replicas, wait for W acknowledgements on a write and R responses on a read.
The key inequality is simple:
If W + R > N, every read touches at least one replica that saw the latest acknowledged write.
With N = 3, the usual default is W = 2 and R = 2. You can tune it per use case:
| Setting | Behaviour | Good for |
|---|---|---|
| W=1, R=1 | fastest, may read stale data | metrics, last-seen times |
| W=2, R=2 | overlapping quorums | carts, sessions |
| W=3, R=1 | slow writes, fresh fast reads | read-heavy config |
| W=1, R=3 | fast writes, slow fresh reads | write-heavy logs |
Overlap guarantees a read sees the newest write. It does not tell you what to do when a read returns two different values. That is the next piece.
Step two: track history, not time
Instead of a timestamp, attach a version vector to each value: a small map from node ID to a counter. When a node coordinates a write, it bumps its own counter in the vector the client last read.
Comparing two vectors gives one of three answers:
- every counter in X is less than or equal to Y's, so X is an ancestor and can be dropped,
- the reverse, so Y is older,
- neither dominates, so the writes were concurrent and both must be kept.
The comparison fits in a few lines:
def compare(a: dict, b: dict) -> str: nodes = set(a) | set(b) a_le_b = all(a.get(n, 0) <= b.get(n, 0) for n in nodes) b_le_a = all(b.get(n, 0) <= a.get(n, 0) for n in nodes) if a_le_b and b_le_a: return "equal" if a_le_b: return "a is older" if b_le_a: return "b is older" return "concurrent" print(compare({"A": 1}, {"A": 2})) # a is older print(compare({"A": 2}, {"A": 1, "C": 1})) # concurrent print(compare({"A": 3, "C": 1}, {"A": 2})) # b is older In the cart story, the phone's write carries {A: 2} and the laptop's write carries {A: 1, C: 1}. Neither dominates, so the store returns both carts as siblings. The cart service applies its business rule (merge = union of items), writes back the merged cart, and the new vector {A: 3, C: 1} supersedes both. The shoes survive.
Keeping it practical
Two follow-up questions come up almost every time:
- Don't vectors grow forever? Only by one entry per node that coordinates writes for a key. Routing a key's writes to one of its replicas keeps vectors at three or four entries, and you can prune old entries past a limit. Pruning can occasionally create a false conflict, which is safe because the merge handles conflicts anyway.
- Do application teams have to write merge code? Not always. CRDTs (conflict-free replicated data types) such as counters and observed-remove sets merge automatically and always converge. Offer a few built in and most teams never see siblings.
Takeaways
- Last-write-wins is a policy for data you are willing to lose, not a default.
- W + R > N makes reads see the latest acknowledged write. It does not resolve conflicts.
- Version vectors detect true concurrency. Timestamps cannot.
- Conflicts are resolved by a merge rule that lives with the business logic, or by a CRDT.
- In an interview, say all of this out loud: it is exactly the reasoning the question is testing.
The full deep dive covers the rest of the design: the hash ring and replica placement, read repair, gossip-based failure detection, hinted handoff, Merkle-tree anti-entropy and tombstones, with diagrams and a cheat sheet.
Read the full story on Medium: Designing a Distributed Key-Value Store
