LOAD-BEARINGfree
High-level designTier 8 · unit 4 of 5 · 16 min read

Sync engines and conflict on the device

Strip away the mobile framing and state the problem plainly: two replicas, both accepting writes, intermittently connected, one of them untrusted. That is multi-leader replication from Tier 4, running over the worst network in the system. Every warning Lesson 6.02 gave you about concurrent writes applies here, with the added detail that one of the writers is a phone in someone's pocket.

What a sync protocol has to answer

One question, in both directions: what has changed since I last spoke to you? Three ways to answer it, in ascending order of honesty:

  • Timestamps. "Give me everything modified after 14:03." Tempting and wrong, for the reason 8.01 gave — the client's clock is user-settable, and even server timestamps collide and go backwards under clock adjustment. It also cannot distinguish "changed after" from "changed concurrently with".
  • A server-assigned sequence. A monotonic cursor over a change feed: "give me everything after change 84,102." Simple, robust, and enough for the very common case where the server is the only authority on ordering. Most production sync engines are this.
  • Version vectors. Each replica tracks what it has seen from every other. This is the only mechanism that can tell you two writes were concurrent rather than one following the other — and that distinction is the whole basis of conflict handling.

Which surfaces the split that most designs blur: detection and resolution are different problems. A version vector detects a conflict. It has no opinion about what should win — that is a domain decision, and the failure mode is letting the transport layer make it silently by defaulting to whichever write arrived last.

The resolution ladder

  1. Last-write-wins. Silent data loss, and on a client with an adjustable clock, data loss with an arbitrary winner. Legitimate for genuinely disposable state — a scroll position, a UI preference — and for nothing a user typed.
  2. Field-level merge. Two people edited the same record but different fields, so take both. This is by a wide margin the best value on the ladder: it eliminates most real-world conflicts for the cost of tracking changes per field rather than per record. If you do exactly one thing from this lesson, do this.
  3. Domain-specific merge rules. A counter merges by summing the deltas. A tag set merges by union. A status field has a precedence order — "cancelled" beats "pending" regardless of arrival order. Cheap, obvious in hindsight, and it requires you to have modelled operations rather than states.
  4. CRDTs. Data types whose merge is mathematically guaranteed to converge, no matter what order updates arrive in. Real, and constrained: your data must fit one of the available shapes — counters, sets, registers, sequences. The sequence CRDT is what makes two people typing in the same paragraph work.
  5. Ask the user. The honest fallback when the conflict is genuinely semantic and no rule can be right. Expensive, and sometimes the only correct answer.

The CRDT reality check

CRDTs guarantee convergence — every replica ends up with the same value. They do not guarantee that value is what any human wanted. Two people rewriting the same sentence converge on a deterministic interleaving of both rewrites, which is a merged sentence that may be nonsense. Convergence is a safety property; usefulness is not implied.

They also cost. Tombstones and causal metadata accumulate, and a text CRDT's document can be several times the size of the text it represents — a real problem on a device with limited storage that must sync over a limited connection. Garbage collection exists and is one of the harder parts of any implementation.

The move that removes most conflicts

Before reaching for any of that: send what the user did, not what the result was.

state-based:      quantity = 5        two devices → one wins, one lost
operation-based:  quantity += 1       two devices → +2, both kept

An enormous fraction of "we need conflict resolution" dissolves here. Intents merge; snapshots overwrite. This is Lesson 7.04's insight — store the facts, derive the state — applied at the sync boundary, and it is available to you without adopting a single new library.

Deletes need tombstones, and tombstones need a retention policy

You cannot simply remove a row. A device that was offline still has it, and on reconnection it will helpfully sync it back — a resurrection bug that looks like the database is haunted. So a delete is a record marked deleted: a tombstone.

Which creates a question with a real answer required: how long do you keep them? Long enough to cover your longest plausible offline window, because a device that returns after the tombstone was purged will resurrect the record. Three months of tombstones for a user who reinstalls after six is a bug you will see rarely and never reproduce. Lesson 3.07's retention thinking, with a specific correctness deadline attached.

The server still decides

Local-first does not mean the device is authoritative. Every sync request is authorised per record, server-side, on the assumption that the client is lying — because a modified client is a normal thing that exists. Rejection therefore has to be a first-class outcome the sync protocol expresses and the client handles gracefully, which loops directly back to 8.03: a rejected mutation is a failed optimistic update, arriving possibly hours after the user made it.

The trap

Reaching for CRDTs because merging sounded hard. You acquire strong convergence guarantees over a data model that may not match your domain, a metadata tax on storage and bandwidth, and a library that one person on the team understands and cannot debug when it misbehaves. Field-level merge plus operation-based writes handles the overwhelming majority of real applications. Before adopting a CRDT you should be able to name the specific conflict in your product that those two do not solve — and if the answer is collaborative rich text, then yes, it genuinely is a CRDT, and that is precisely the case they were invented for.

The tradeoff sheet

Problem solved
Two devices editing the same data while apart, and the requirement that neither edit silently disappears when they meet.
Rejected, and why
Server-wins is trivial to implement and discards user work without telling anyone. Locking a record for editing is honest and unusable when the lock holder is offline.
The trade
Metadata proportional to the number of writers, merge semantics that must be defined per data type, and a class of bug that only appears with specific interleavings.
First to break at 10x
Metadata growth and merge coverage. At 10x devices, per-writer causality tracking is the payload, and the merge cases nobody enumerated start arriving daily.
What it does to the bill
Storage per document for version metadata, and bandwidth for the deltas. Modest, and dwarfed by the engineering cost of getting merges right.
Day-2 operations
Instrument conflict rate and merge outcomes. A rising conflict rate is a product signal — usually two people editing the same thing because the product invited them to.