FSD Frontend System Design

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?

  • Time16 min
  • DiagramTwo-device conflict timeline + choosing the rule
  • Chapter9 · Offline Support

Diagram

diagram
diagram

Lessons in this diagram11.11 HLD – Google Docs

How it works

  1. Detect, don't guess: every record carries a server-issued version, and every queued edit sends the version it started from (9.3).
  2. Only the same field changed on two devices is a real conflict — different fields merge automatically (a three-way merge against the base).
  3. "Last" means last to reach the server. Device clocks drift and can be changed by hand; never compare them.
  4. When a person must decide, the server returns 409 with its copy; show both and keep the loser recoverable.
  5. For text and lists edited live together, use a CRDT (Yjs, Automerge) so edits merge instead of competing.
js
// 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

StrategyFitsCost
Last write winsSettings, status flagsSilent data loss — fine only where nobody would notice
Field-level mergeNotes, forms, list metadataKeep a base copy for every pending edit
Version check (409)Money, stock, legal textInterrupts the user; needs a compare screen
CRDTShared text, lists, boardsA 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.