The offline-first decision guide: when to use a library (always)
The short version
A hand-rolled system for keeping multiple devices in agreement produced weeks of bugs that could never be reproduced on demand: rows appearing on one device and not another, vanishing, returning, and two devices confidently disagreeing.
It was all deleted and replaced with an existing offline-first library. Keeping distributed copies of data in agreement is a solved problem with genuinely hard edge cases, and it is one of the few places where writing it yourself is almost always the wrong call.
Copy these prompts
Paste them into Claude Code (or any AI that can see your project), in this order. Each one answers a different question, and the first is usually the one that matters.
1. Ask what already exists before writing anything
My app needs data to stay in agreement across multiple devices, including offline. Before I write any of it, show me the established libraries for this in my stack and what each assumes about my data.
2. Ask what happens when somebody goes offline
Walk me through exactly what happens in my app when someone loses connection, keeps using it, and then comes back. Show me where changes are held and in what order they are sent.
3. Ask what happens when two people clash
Tell me what my app does when two people change the same thing at the same time on different devices. Show me who wins, whether anyone is told, and let me decide whether I actually like that answer.
The most expensive code I ever wrote is code I deleted. I hand-rolled offline sync (queue + cache + merge) and got weeks of "ghost bugs": rows appearing, vanishing, disagreeing across devices, none of it reproducible. I ripped the whole thing out for an offline-first library (Brick: SQLite + REST) in one brutal rewrite. It was the most satisfying diff of the project: the whole hand-rolled layer deleted, one data layer left standing.
The decision guide
Should you write your own sync engine? No. That's the guide.
Sync is a solved-by-libraries problem the same way crypto is: the failure modes are subtle, adversarial, and invisible in development. Here's what "queue + cache + merge" actually requires you to get right:
- Durable write queue: survives app kill, retries with backoff, replays in order, dedupes on retry (idempotency keys).
- Reconciliation: merging server truth into local state without erasing in-flight local writes (my vanishing-expense bug lived exactly here).
- Conflict policy: same row edited on two devices; last-write-wins, merge, or surface to user, chosen per field, not globally.
- ID strategy: client-minted IDs the server echoes back, so replays and merges reconcile by identity instead of guesswork.
- Failure taxonomy: a 400 (permanent, drop + tell user) is not a 500 (retry) is not a timeout (retry with caution: it may have committed).
- Reactivity: every local write must repaint every screen that renders derived state, or devices "disagree" with themselves.
Each item looks like a weekend. Together, interacting, they're the ghost-bug factory.
What to do instead
- Pick a library that owns the whole loop: local store + queue + replay + reconcile in one opinionated layer. Flutter: Brick, PowerSync; JS: Replicache, RxDB, Electric; or backend-as-sync (Firebase/Supabase realtime) if your model fits.
- One data layer. The rewrite's real win was going from three overlapping sources of truth (in-memory + local DB + server overlay) to ONE repository that everything reads through, more than the library itself.
- Keep your domain math out of the sync layer. Balances/derived numbers get computed from synced rows, never stored-and-synced themselves.
- Migrate in one cut, not gradually. Running old and new sync side-by-side doubles the ghost surface. Brutal rewrite, feature-frozen week, done.
Steal this for your app
- The tell that you're hand-rolling sync without admitting it: you have a "retry helper," a "cache service," AND a "refresh on foreground". Three fragments of a sync engine, coordinating by luck.
- Unreproducible cross-device bugs are almost never model/UI bugs; budget your debugging time toward the data layer immediately.
- Deleting weeks of your own work is a skill. The sunk cost is already sunk. The ghost bugs are the ongoing bill.
Run this on your codebase
Paste this into Claude Code in your repo:
Audit this repo for a hand-rolled sync engine hiding in plain sight.
Find the fragments: retry helpers, cache services, write queues, refresh-on-foreground hooks, and manual merge or reconcile code.
Count the sources of truth for user data (in-memory state, local DB, server overlay) and flag anything beyond one repository.
Check the write queue: does it survive app kill, retry with backoff, replay in order, and dedupe with idempotency keys?
Check reconciliation for paths that can erase in-flight local writes, and whether conflict policy is defined per field.
Check that 400, 500, and timeout failures are handled as three different cases.
Recommend whether an offline-first library (Brick, PowerSync, Replicache, RxDB, Electric) should replace the fragments.
Report findings as a checklist before changing anything.