Offline-first apps: local database, sync queue and a conflict policy you wrote down
Reads and writes hit Room first, a pending-operations table holds the queue, WorkManager syncs with constraints and backoff - and the conflict policy is a product decision, not a library.
Phones lose signal in lifts, trains and the back of every building. An app that treats the network as always available shows spinners and loses the user's work at exactly those moments. Offline-first flips the assumption: the app reads and writes a local database first, and a background process syncs changes with the server whenever connectivity allows. Done well, the app feels instant and the network becomes a detail.
The three moving parts
1. A real local database. On Android, Room is the recommended persistence layer: it provides an abstraction over SQLite with compile-time verification of queries and streamlined migration paths, and Google's own guidance is to use Room rather than the raw SQLite APIs. Room is designed for on-device persistence - that is the point. The server copy is for sharing and backup, not for keeping the UI alive.
2. A queue of pending changes. Every user edit writes to the local database immediately and records a pending operation (created note X, edited field Y, deleted item Z). The UI never waits on the network. The queue is just another table, which means pending work survives process death and reboots.
3. A reliable sync worker. This is where WorkManager fits on Android. WorkManager is the recommended API for persistent work - tasks that remain scheduled through app restarts and device reboots. It supports deferrable work that runs later and can run periodically, and it lets you declare constraints declaratively: run only on an unmetered network, only while charging, or only when the device is idle. Failed work retries with a configurable exponential backoff policy, and related tasks can be chained sequentially or in parallel. Those four features - persistence, constraints, backoff, chaining - are almost exactly the feature list a sync engine needs, which is why hand-rolling your own scheduler on top of AlarmManager is rarely worth it.
Conflicts: decide the policy before you need it
Two devices can edit the same record while one is offline. When both sync, one write must lose, or the writes must merge. The common policies, in rising order of effort:
- Last write wins. Each record carries a timestamp; the server keeps the newest. Simple, and fine for single-user data like preferences. It silently discards edits when two devices are active, so be honest about that trade-off.
- Per-field merging. Instead of whole records, sync field-level changes with their own timestamps. More work, but an offline phone editing a contact's phone number no longer overwrites a web edit to the same contact's email.
- Explicit conflict surfacing. For anything where losing an edit would hurt (documents, money), flag the conflict and let the user pick. This is a product decision, not a library feature.
Whichever policy you choose, write it down in the data layer and test it with two simulated clients before you ship. Conflict logic that exists only in a diagram will not survive contact with a real server.
Failure modes to design for
Partial sync. A sync run that uploads half the queue and dies must resume cleanly. Mark operations as synced only after the server confirms, so a retry replays them idempotently - the server should treat a repeated operation as a no-op, not a duplicate.
Auth expiry mid-sync. The token can expire while the queue is long. Refresh credentials inside the sync worker and retry; do not surface a login wall over data that is safely stored locally.
Deleting synced tombstones. A deleted record cannot simply vanish locally or the next sync will resurrect it from the server. Keep a tombstone until the deletion has been acknowledged.
A pragmatic starting point
Room for local storage, a pending-operations table, one periodic WorkManager worker constrained to a network connection, and last-write-wins on a single entity type. That is a weekend-sized first version that already makes the app usable on a train. Everything beyond it - field merging, selective sync, push-triggered sync - is refinement you can add when usage data says it matters.
Sources
Prepared with AI assistance and reviewed before publication. Platform APIs described from Google's documentation, checked on 19 November 2026.