Guides · Architecture

How to build an offline-first mobile app: local storage, sync queues and conflicts.

Design a mobile app that works without internet: local database, sync queue, idempotent requests, conflict handling, UI states and testing in airplane mode.

Key takeaways

  • Offline-first means the app reads and writes local data first and syncs with the server when it can.
  • Use a local database, a queue of pending changes and unique ids that make retries safe.
  • Decide in advance how conflicts are resolved.
  • Show the user what is saved locally and what is synced.

When you need it

Many apps run where signal is poor: parking lots, fields, hospitals, mountain villages and basements. If a user must complete work without waiting for the network, offline support is not optional. If the network is nearly always available, a simple online app is easier.

The basic pattern

  1. The user performs an action.
  2. The app writes it to a local database and updates the screen at once.
  3. The change is added to a sync queue.
  4. A background process sends queued changes when the network is available.
  5. The server confirms, and the local record is marked synced.

Local storage

Choose a real database for structured data, such as SQLite, or a library built on it. Preferences or key-value stores are fine for small settings but not for records. Store enough data to complete the task offline, such as price lists or reference data.

Use client-generated ids

If the server assigns ids, an offline record has no id. Generate a unique id on the device, such as a UUID, and send it with the request. This also makes retries safe, because the server can recognise a repeated request.

Make requests idempotent

Networks fail halfway. The app may not know whether the server received the change. Send the same unique id on retry, and make the server ignore duplicates. Otherwise a retry can create a second record or charge twice.

Conflict handling

Conflicts arise when two devices edit the same record. Options:

  • Last write wins. Simple but may lose data.
  • Server wins for sensitive data such as prices, and the server calculates what matters. In a parking app, the server owning pricing prevents a phone from inventing a fee.
  • Merge fields that do not overlap.
  • Ask the user in rare, important cases.

Choose per type of data.

Pull changes down

Besides pushing, the app must fetch updates. Use a "changed since" timestamp or version number, and fetch only changes. Handle deleted records with a flag instead of removing them silently.

User interface

  • Show clear states: saved on this device, syncing, synced, failed.
  • Never block the main task on the network.
  • Let users retry failed items and see why they failed.
  • Warn before logout if unsynced data exists.

Authentication offline

Tokens expire. Decide how long an offline session may last and how the app re-authenticates when back online. Do not store passwords on the device.

Printing and hardware

Offline apps often use local hardware, such as a Bluetooth printer. Queue print jobs and show their state, so a failed print does not block the workflow.

Test it

  • Use airplane mode in the middle of operations.
  • Kill the app during a sync.
  • Test slow and flaky networks.
  • Create conflicting edits on two devices.
  • Test an upgrade with unsynced data.

Frequently asked questions

Is offline-first harder?

Yes, mainly because of sync and conflicts. Start with the minimum set of data that must work offline.

Can I do this in a web app?

Yes, with service workers and IndexedDB, which have their own limits.

Do I need a special backend?

A normal API works if it supports idempotent writes and incremental fetches.

A parking app has to work at the gate even when the network is weak.

See the parking app