skip to content

Write Conflicts

Two devices edit one record offline: the client sends the base revision it saw, and the server merges by field, keeps the last write or asks the user. Interviewers probe which data needs which policy.

on this pageshow

explore

questions

6

In a household app synced across two offline phones, what must each edit carry so the server can tell it used a stale copy?

level: juniorimportance: must knowfreq 50%

answer

  1. the version the edit started from
  2. issued by the server, not the phone
  3. a conditional write: apply only if unchanged
  4. send changed fields, not the whole record
  5. the next edit takes the returned revision

basics

~20 s

The revision it was made from: the server-assigned version number or entity tag the phone stored with its copy. The server applies the edit only if that base still matches; otherwise it merges or rejects instead of silently overwriting.

solid answer

~50 s

Every shared record carries a revision the server assigns: a counter bumped on each accepted write, or an entity tag. The phone stores it beside its local copy and sends it as the edit's `baseRevision`, which makes the edit a conditional write. If the base matches the server's current revision, the server applies the edit and returns the new revision, which the phone stores. If it does not, another write landed in between, and the server runs that data type's conflict policy: merge, keep one, or reject with its current copy so the phone can resolve. Without a base, whichever phone syncs second silently overwrites the first, the classic lost update. Two details trip people up: send only the fields the user changed, and give the next queued edit the revision returned for the previous one, or the phone conflicts with itself.

code

json · 12 lines
json
{
  "request": {
    "record": "item-7f3a",
    "baseRevision": 41,
    "changes": { "quantity": 2 }
  },
  "responseWhenStale": {
    "outcome": "conflict",
    "currentRevision": 43,
    "current": { "name": "oat milk, barista", "quantity": 1, "checked": false }
  }
}

go deeper

for a junior

Recall that every edit names the version it was made from, and that a mismatch means someone else wrote in between.

for a middle

Explain the loop: store the server's revision with the row, send it as the base, store the revision that comes back, and send only the changed fields.

for a senior

Show the traps: queued edits that must chain revisions, a pulled change moving the base, and a replay whose first response was lost.

for a principal

Decide where revisions live as the app grows, per record or per field, and which data types justify the bookkeeping at all.

## The problem: a blind overwrite Picture a **shared household app**: two partners keep shopping lists, chore checklists and notes on their own phones, and both phones often work offline. Alex changes the quantity of "oat milk" from 1 to 2 on the train; meanwhile Sam, at home and online, renames it to "oat milk, barista". Alex's phone reconnects an hour later and sends the item. If the server simply stores whatever arrives, it has no way to know that Alex's edit was made on a copy that is now out of date. Depending on what Alex's phone sends, Sam's rename either disappears or survives by luck. The HTTP specification calls this the **lost update** problem: two clients act on the same resource in parallel and the second write erases the first without anyone being told. ## The revision: what it is and who assigns it The fix is to make every edit say which version of the record it was made from. That version label is the **base revision**. - **A revision** is a value the server changes on every accepted write to a record: an integer counter (`41`, `42`, ...) or an opaque **entity tag** (ETag), often derived from the stored content. - **The server issues it.** Only the server sees every write in one order, so only its label means the same thing to both phones. The phone's clock or a counter kept on the phone cannot play this role. - **The phone stores it with the row.** When the phone pulls a record, or gets a write accepted, it saves the revision next to its local copy. - **The edit sends it back** as `baseRevision`. This turns a plain write into a **conditional write**: apply it only if the record is still at that revision. ## The conditional write, step by step 1. The phone loads the item at revision 41 and stores `rev = 41` with it. 2. The user edits offline. The phone records the change and its base, 41. 3. On sync, the phone sends the changed fields plus `baseRevision: 41`. 4. The server compares 41 with the record's current revision. 5. **Match:** it applies the change, sets the revision to 42, and returns 42. The phone stores 42 as the row's new base. 6. **Mismatch** (say the record is at 43): another write landed in between. The server does not blindly apply the edit. It runs the policy chosen for that data type: merge the fields, keep one value, or reject with its current copy so the phone (and maybe the user) can resolve it. Over plain HTTP the base often travels as a precondition, and a mismatch is typically reported as 412 Precondition Failed; a sync API may instead return a conflict answer with the current state in its body, which is what a 409 Conflict is meant to carry. The header mechanics are not the point here. The point is that the server receives a base and compares it. ## What breaks the scheme in practice - **Sending the whole record.** If the phone sends every field, the untouched ones carry old values. Sam's rename looks like something Alex changed back, so it is either overwritten or flagged as a conflict that never happened. Send only the fields the user changed. - **Not chaining revisions.** If two edits to one item are queued offline, the second must use the revision returned for the first. Sending the original base again makes the phone conflict with its own earlier write. - **Forgetting pulled changes.** When the phone pulls a partner's change to a row, the stored base must move to the new revision, or the next local edit is judged stale. - **A lost response.** The write succeeded but the reply never arrived, so the replay's base now looks stale. The server must recognise the repeat, through the idempotency key the queue stored with the write, or because HTTP allows a success answer when the requested change has already been applied, rather than reporting a conflict. - **Treating a mismatch as failure.** A stale base is a signal, not an error. For most fields the server can still merge; only some cases need a person. ## Choosing the label | Label | How it is made | Good for | Watch out for | |---|---|---|---| | Revision counter | Server increments it per record on each write | Easy to log, compare and debug | Must be per record, not one global number | | Entity tag | Server derives it, often from content | Fits HTTP conditional requests | Opaque: the phone can only compare it for equality | | Phone timestamp | Phone's clock at edit time | Nothing, as a base | Says when, not which version was seen | A stale base is detected the same way for every data type in the app. What happens next, whether that is a merge, last-write-wins or the user's choice, is decided per data type, and that is where the real design work starts.

  • The phone's write succeeded but the response was lost. On replay its base looks stale. What should happen?
    The server already applied this exact change, so a conflict here would make the phone fight itself. Either the replay carries the same idempotency key the queue stored with the write, and the server returns the recorded outcome, or the server sees the requested change is already in place and answers success, which HTTP explicitly allows for a conditional state-changing request. The conflict policy must not fire on it.
  • Should the phone store a revision counter or an entity tag?
    Either works if the server issues it and changes it on every accepted write to that record. A counter is easy to log and reason about; an entity tag is opaque and only compared for equality. What fails is a label the phone makes up, such as its clock time or its own counter, because it names nothing the server issued.

Marking up a printed copy stamped 'version 41': the office accepts your changes only if the master is still version 41; otherwise someone compares your page with the newer master before anything is filed.

  • Without a base revision the server can still tell that a partner edited first.
  • The phone's clock time at the edit tells the server which version it saw.
  • Sending the whole record is harmless because the untouched fields did not change.
  • A stale base means the edit must be thrown away.
  • The stored base only needs updating after a full resync.
open as a page

In a household app where partners edit shared lists offline, which conflict policy fits a checked flag, a note body, item order and a counter?

level: middleimportance: must knowfreq 52%

basics

~20 s

Choose per data type by what losing an edit costs: last-write-wins for a checked flag or a title, per-field merge for items, increments for counters, a sort key per item for order, and the user's choice for a note body.

open as a page

In a household app resolving offline edits by last-write-wins, why is the phone's clock a weak way to pick the winner, and what is safer?

level: middleimportance: should knowfreq 46%

basics

~20 s

A phone's clock can drift or be set by hand, so a phone whose clock runs ahead wins ties it should lose. Let the server decide order by arrival or by the revision it assigns, which every device sees the same way.

open as a page

In a household app, when one partner deletes a shopping item while the other edits it offline, what should happen once the edit syncs?

level: seniorimportance: should knowfreq 44%

basics

~20 s

Decide by what the edit carries: a small change to a deleted item, such as a tick, is dropped and the editor told; a substantive one restores the record or asks. A server tombstone makes the case recognisable.

open as a page

In a household app syncing two partners' phones, why should conflict rules run on the server, and when must a phone still resolve one with the user?

level: seniorimportance: should knowfreq 42%

basics

~20 s

The server sees every write in one order, so both phones get one verdict and rules change without a release. A phone settles only what needs a person, keeping the user's text, showing both and sending the choice as a new write.

open as a page

In a household app with shared lists and notes, when does a CRDT or operational transform earn its cost, and for which data?

level: seniorimportance: nice to knowfreq 30%

basics

~20 s

Only for free text two people really edit at once, such as a note written together. Lists, ticks and counters are covered by item ids, per-field merge and increments, without the history and server work a text-merging type adds.

open as a page