Part IV — The Reach · Lesson 9.4
Conflict Resolution
In one lineTwo offline edits to the same thing will collide — choose the rule before it happens, per field, not per app.
Where you've seen itA shared shopping list. You tick "milk" on your phone underground while your partner renames the list at home. Both reconnect — what does it say?
Diagram
Lessons in this diagram11.11 HLD – Google Docs
How it works
- Detect, don't guess: every record carries a server-issued version, and every queued edit sends the version it started from (9.3).
- Only the same field changed on two devices is a real conflict — different fields merge automatically (a three-way merge against the base).
- "Last" means last to reach the server. Device clocks drift and can be changed by hand; never compare them.
- When a person must decide, the server returns
409with its copy; show both and keep the loser recoverable. - For text and lists edited live together, use a CRDT (Yjs, Automerge) so edits merge instead of competing.
// three-way merge: base is the copy the offline edit started from
function merge(base, mine, theirs) {
const out = { ...theirs };
for (const key of Object.keys(mine)) {
const iChanged = mine[key] !== base[key];
const theyChanged = theirs[key] !== base[key];
if (iChanged && !theyChanged) out[key] = mine[key];
else if (iChanged && theyChanged && mine[key] !== theirs[key]) return { conflict: key };
}
return { merged: out };
}Trade-offs
| Strategy | Fits | Cost |
|---|---|---|
| Last write wins | Settings, status flags | Silent data loss — fine only where nobody would notice |
| Field-level merge | Notes, forms, list metadata | Keep a base copy for every pending edit |
| Version check (409) | Money, stock, legal text | Interrupts the user; needs a compare screen |
| CRDT | Shared text, lists, boards | A library, growing metadata, a server that speaks it |
Deletes are the nasty case: an offline edit to an item someone else deleted — restore it or drop the edit? Decide up front; tombstones (deleted IDs kept for a while) make it answerable.
Interview angle
"Two devices edit the same note offline. What happens when both reconnect?"
- Each edit carries the version it was based on, so the second to arrive is visibly stale — a conflict, not a silent overwrite.
- Fields that don't overlap merge automatically; for the same field, choose per field — last write wins for low stakes, the user decides for content that matters.
- For genuinely collaborative text, use a CRDT so concurrent edits always converge and there is nothing to resolve.
Recap
- Conflicts are detected with base versions, never with device clocks.
- Choose the rule per field: last write wins, merge, ask, or CRDT.
- Whatever loses must be recoverable — silent loss is the real bug.