Prompt
I'm building an app with offline users and need reliable sync on reconnect
Latest observation
Nov 4, 2026
OpenAI APIWeb search: off
A solid offline-first sync design usually needs 5 pieces:
-
Local source of truth
- Store all user writes locally first (SQLite/IndexedDB/etc.).
- UI reads from local state, not the network.
- Every mutation gets an ID, timestamp, and a sync status.
-
Operation log / outbox
- Record each change as an operation, not just final state.
- Example:
createTask,updateTaskTitle,deleteTask. - Keep an ordered queue of unsynced ops to send when online.
-
Server-side versioning
- Each record should have a version, revision, or updated-at token.
- On sync, client sends:
- changes since last sync
- last seen server cursor/version
- Server responds with:
- accepted ops
- conflicts
- remote changes missed while offline
-
Conflict handling
- Pick a strategy per data type:
- Last write wins for simple fields
- Mergeable fields for lists/sets
- Manual resolution for important user edits
- Use deterministic rules so reconnects are repeatable.
- Pick a strategy per data type:
-
Idempotency and retries
- Every mutation should have a unique client mutation ID.
- Server must ignore duplicates if the same mutation is retried.
- Sync should be safe to replay after crashes or timeouts.
Recommended sync flow
- App opens offline → user edits locally
- Changes go into outbox
- On reconnect:
- Push local outbox to server
- Server validates and applies what it can
- Server returns conflicts + remote updates
- Client applies remote updates locally
- Client resolves conflicts and resends if needed
- Repeat until both sides converge
Practical tips
- Keep sync cursors per collection or per user, not one global blob.
- Batch operations to reduce network overhead.
- Add exponential backoff for reconnect attempts.
- Log sync events for debugging.
- Consider schema changes early; offline clients can be far behind.
- If data is collaborative, look at CRDTs or OT; if not, simpler versioning is usually enough.
A simple robust pattern
If you want a pragmatic design:
- local DB
- mutation outbox
clientMutationId- server
recordVersion - pull cursor for remote changes
- conflict policy per field
That’s enough for most apps.
If you want, I can also sketch:
- a database schema
- a sync API design
- or a step-by-step algorithm for reconnect/sync.