Sync & conflict resolution

Change tracking, last-write-wins vs CRDTs vs server-authoritative merges, idempotent sync, and handling clock skew. "Two devices edit offline — who wins?" is the design centerpiece.

About 24 min read3-question knowledge check

The mental model

Conflict resolution is what happens when two truths collide. Offline-first buys you instant local writes, but it also means two devices can edit the same record while neither can see the other, and when they finally sync, the system must decide — deterministically — what the reconciled state is. The whole discipline is answering one question well: "Two devices edited the same thing offline. Who wins, and did anyone lose data they will notice?"

The analogy that sticks is a shared paper document passed between two people in separate rooms. Each writes on their own photocopy. When the copies come back together, you need a rule for merging them: maybe "the most recent edit wins" (last-write-wins), maybe "combine both people's changes line by line" (a merge / CRDT), maybe "there is one official copy and everyone else's edits are proposals the official copy accepts or rejects" (server-authoritative). None of these is universally right — they trade simplicity against not losing anyone's work.

The one idea that prevents the classic disaster: wall-clock time is not a reliable ordering. Device clocks drift, get set manually, jump across timezones, and lie. If your rule is "the edit with the later timestamp wins" and you trust the *device's* clock, a phone whose clock is five minutes fast will silently win every conflict, destroying edits made *later* in real time on a correct device. Serious sync systems order changes by something the system controls — a server-assigned version/sequence or a logical clock — precisely so a wrong device clock cannot silently eat data. "Last-write-wins" is fine; "last-device-timestamp-wins" is a data-loss bug.

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision