Lesson 05

Concurrent Writes and Conflicts

Building block

Two writes are concurrent when neither knew of the other, however far apart they happened. Any design that accepts writes in more than one place will get them: many leaders, a leaderless store, a phone that edits offline. Something must then decide what survives.

The interview question is who decides and what is lost. Last writer wins is an answer only when losing a write is acceptable.

What Concurrent Means

Concurrency is about knowledge, not the clock. A phone edits a note offline for a day while a laptop edits the same note online. The two edits are hours apart and still concurrent, because neither saw the other.

Two designs avoid conflicts altogether. One leader puts every write in one order, as the first lesson of this chapter showed. A home for each record, one place that takes all of its writes, does the same record by record, at the price of the round trip to that home from wherever the writer is.

Last Writer Wins

Last writer wins stamps each write with a time and keeps the highest stamp. Cassandra resolves concurrent updates to each column this way. The other write is dropped with no error, and last means whatever the clocks said.

Assume server B's clock runs 50 milliseconds behind server A's. A takes a write. Thirty milliseconds later, B takes a write to the same record. B's stamp reads 20 milliseconds earlier than A's, so the store keeps A's write and drops the later one. Clocks and why they drift are Chapter 5's subject.

Last writer wins with B's clock 50 ms behind
A writesat 0 ms, stamped 0
→
B writesat 30 ms, stamped -20
→
Stamps compared0 beats -20
→
A's older write keptB's later write lost

Use it when a write replaces the whole value and losing one is harmless: a profile photo, a cached result. Move off it as soon as a value is built up by several writers, such as a cart, a counter or a document.

Version Vectors

A version vector detects concurrency instead of guessing an order. Each replica that takes writes keeps a counter, and every version carries the counter of each replica it has seen.

Comparing two versions gives one of two answers. If one has seen everything the other has, it is newer, and the older one can go. If neither has seen all of the other, they are concurrent, and the store keeps both as siblings for someone to merge.

Two version-vector comparisons
One is newerA:2, B:1 against A:1, B:1
The first has seen every write the second has, and one more on A.
The store: keeps the first and drops the second.
ConcurrentA:2, B:1 against A:1, B:2
Each has a write the other has not seen.
The store: keeps both as siblings to merge.

Amazon's Dynamo worked this way, though its paper calls them vector clocks. Its shopping cart merged siblings so that an add to cart was never lost, though deleted items could come back. The cost is that a version grows with the number of writers, and every reader must handle siblings. Lamport and hybrid clocks order events rather than detect concurrency, and they are Chapter 5's.

Types That Merge Themselves

A CRDT, a conflict-free replicated data type, is a data type whose merge is part of the type, so replicas converge whatever order the updates arrive in.

A counter keeps one count per replica, and its value is the sum. A counter that can also go down keeps two such counters, one for increments and one for decrements. A set has to decide what a concurrent add and remove of one element give, and the observed-remove set lets the add win. Text types let several people edit one document at once.

They cost extra metadata on every value, they offer only the operations the type defines, and they hold no rule across values. A balance that must never go negative cannot be a counter merged after the fact: two replicas can each approve a withdrawal that the sum cannot cover.

Who Resolves It

Three places can resolve a conflict, and what a lost write costs picks between them. The store, with last writer wins or a CRDT merge, needs no code, and the product accepts its losses or its limits. The application merges siblings on read with rules it knows, as for a cart or a set of tags.

The user is shown both versions as a conflict copy when only a person can decide, such as a document edited offline on two devices; the file sync problem in Chapter 20 is built on that case. You might say: "Likes are a counter type that merges itself; the note body keeps both versions and asks the user."

Who resolves a conflict, by requirement
Each write replaces the value, and a lost one is harmless→Store: last writer wins
A count or a membership that many writers touch→Store: a CRDT
The application knows how to combine both versions→Application, on read
Only a person can tell which version is right→User: a conflict copy
Last Writer Wins vs Version Vectors

Last writer wins keeps one version by timestamp, so storage stays simple and a concurrent write vanishes. Use it when each write replaces the whole value and a lost one is harmless.

Version vectors detect that two writes were concurrent and keep both, so nothing is lost and someone must merge. Use them when every writer's change matters, as in a cart or a shared document.

How Interviewers Probe This

A strong answer here walks one path: how the design detects a conflict, who resolves it, and what the user sees when it happens.

The first probe is "Two users edit the same record in two regions at once. What happens?" Say how the conflict is detected, who resolves it, and what each user sees afterwards.

The second is "Why not just keep the last write?" Name the silent loss and the clock that decides what last means, then give one kind of data in this design where that is fine, such as a profile photo, and one where it is not, such as a cart.

The third is "How would you count likes across regions?" Keep a count per replica and sum them, and say the total is right once the regions have exchanged their counts. Then say why one shared row would not do: every like in every region would wait for the round trip to that row's home, and all of them would compete for the same row.

Misconceptions
  • "Conflicts are rare, so last writer wins is fine." The loss is silent, so its rate is unknown until a user reports a vanished change.
  • "Synchronised clocks make last writer wins safe." Clocks still disagree by some amount, and any two writes closer together than that are ordered by chance.
  • "CRDTs make any data conflict-free." They cover the types they define. A rule across records, such as a balance kept above zero, still needs one place that decides.
  • "A version vector tells you which write is right." It tells you that two writes were concurrent. Choosing between them is still the store's, the application's or the user's job.
  • "Keeping both versions is always safest." Siblings that nothing merges pile up, and every reader then has to handle them.
Why It Matters
  • Say where writes to one record can happen, and so whether it can conflict at all.
  • Name the resolver per kind of data, store, application or user, with what each one loses.
  • Use counter and set types for counts and memberships that many writers touch.
  • Give a record a home that takes all its writes when no merge rule is acceptable, and say the round trip it costs.
RelatedLeaders, Followers and Everyone where concurrent writes come fromQuorums siblings on a leaderless readConsistency Models That Come Up eventual consistency, the convergence a merge providesClocks and Ordering why timestamps drift

Knowledge Check

Server B's clock runs 50 ms behind server A's. A takes a write, and B takes one to the same record 30 ms later. Under last writer wins, what is kept?

  • A's write, because B's later write carries a stamp 20 ms earlier
  • B's write, because it happened 30 ms after A's write in real time
  • Both writes, kept as siblings for the application to merge on read
  • Neither write, because the store rejects stamps that disagree

Version X carries A:3, B:1 and version Y carries A:2, B:2. What does the store conclude?

  • X is newer, because its counter for A is higher than Y's counter
  • Y is newer, because its counter for B is higher than X's counter
  • They are concurrent, so the store keeps both as siblings to merge
  • They are the same version, because both add up to four writes

A like counter is a CRDT replicated across three regions. What does each replica store?

  • One count per region, and the value is the sum of the three counts
  • One shared total, replaced by whichever region wrote it last
  • Every like event in full, replayed in timestamp order on each read
  • One count per region, and the value is the largest of the three

A note was edited offline on a phone and on a laptop, and both edits changed the same paragraph. Who should resolve the conflict?

  • The store, keeping the edit with the later timestamp on its clock
  • The user, shown both versions as a conflict copy to choose from
  • The application, merging the paragraphs by joining their text
  • The store, keeping the edit from the device that synced first

When should a record get one home that takes all of its writes?

  • When no merge rule is acceptable, such as a balance kept above zero
  • When the record is a counter that many regions increment at the same time
  • When writes must be answered at local latency in every single region
  • When the record is a set of tags that any device may add to

You got correct