Most mobile bugs blamed on "flaky users" are actually architectural failures. A field engineer submits a safety checklist in a concrete basement, sees a loading spinner hang for 30 seconds, closes the app, and loses twenty minutes of work. Or worse: the phone briefly regains one bar of LTE, fires a retry twice, and creates two duplicate invoices on the server.
Adding a "No internet connection" banner is not offline support. An offline-first application treats the network as an asynchronous background optimization—never as a prerequisite for the UI to respond.
This guide lays out the exact local storage, outbox queue, and server reconciliation patterns we use when building resilient mobile apps at SmitDev.
1. Make Local SQLite the Single Source of Truth
In a standard online-only app, UI widgets call an API repository directly. In an offline-first architecture, UI screens only read from and write to a local SQLite database (Drift in Flutter, op-sqlite or WatermelonDB in React Native).
[ UI Widgets ] <── reactive stream ──> [ Local SQLite DB ]
│
(Outbox Table)
│
[ Background Syncer ] ── HTTPS ──> [ Backend API ]
Following the unidirectional data flow principles outlined in the Android app architecture guide, your repository layer becomes clean and predictable:
- Reads subscribe to reactive SQLite queries (
watch()streams), so UI updates instantly whether data came from a user tap or a background delta sync. - Writes execute a single local SQLite transaction that updates the domain table and appends a job row to a local
sync_outboxtable atomically.
Because both writes happen inside one SQLite transaction, an app crash right after the user taps Save can never leave a record saved locally without a queued sync job.
2. Schema Design: Domain Tables + Transactional Outbox
Every syncable table needs three metadata columns beyond your business fields:
client_id(UUIDv4 generated on device)version(integer incremented by the server on each accepted mutation)sync_status(synced,pending_create,pending_update,conflict)
Here is a compact SQLite schema for the outbox queue:
CREATE TABLE sync_outbox (
id INTEGER PRIMARY KEY AUTOINCREMENT,
idempotency_key TEXT NOT NULL UNIQUE,
entity_type TEXT NOT NULL, -- e.g. 'inspection', 'task_note'
entity_id TEXT NOT NULL, -- client UUID
operation TEXT NOT NULL, -- 'UPSERT' | 'DELETE'
payload_json TEXT NOT NULL,
retry_count INTEGER NOT NULL DEFAULT 0,
next_retry_at TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'queued', -- 'queued' | 'in_flight' | 'failed_permanent'
created_at TEXT NOT NULL
);
CREATE INDEX idx_outbox_pending ON sync_outbox (status, next_retry_at);
Why idempotency_key Is Mandatory
Mobile networks love to drop packets right after the server commits a transaction and before the HTTP 200 OK reaches the handset. When the sync worker wakes up five minutes later and replays that outbox row, the backend must recognize idempotency_key—either via a unique database index or a Redis/SQL idempotency table—and return the original success response without re-running side effects like charging cards or sending emails.
3. Handling Dependencies and Media Attachments
Real offline apps rarely sync flat text rows. A technician snaps four high-resolution photos, signs on a canvas, and submits an inspection form.
If you encode four 4 MB photos as base64 inside your JSON sync payload, the request times out on weak 3G connections and blows up your API server's PHP/Node memory limit.
Split the pipeline into two strict phases:
- Compress and store locally: Resize photos on-device (e.g., max
1600pxwide, JPEG 80%) and save the file path inside apending_attachmentsSQLite table. - Upload blobs before committing metadata: When the sync worker runs, it first processes
pending_attachments—requesting a pre-signed S3/Cloudflare R2 upload URL, streaming the binary file directly to object storage, and marking the attachmentuploadedwith itsremote_object_key. - Flush parent records: Only once all referenced attachments for an
entity_idare uploaded does the outbox worker send the parent JSON payload referencing thoseremote_object_keystrings.
4. Conflict Resolution Without Silent Data Loss
"Last-write-wins" using client timestamps (updated_at) is dangerous because device clocks drift—users travel across time zones or manually alter phone clocks.
Instead, pick a conflict strategy matched to your domain:
| Strategy | How It Works | Best Used For |
|---|---|---|
| Server Version Check (Optimistic Locking) | Client sends base_version. Server rejects with 409 Conflict if server.version !== base_version. |
Forms, CRM records, job tickets |
| Append-Only Event Log | Never mutate rows; append timestamped events (stock_adjusted +5, note_added). |
Inventory counts, time tracking, audit logs |
| Field-Level Merge | Server compares changed fields against base_version snapshot and flags only overlapping keys. |
Collaborative field inspections |
When a 409 Conflict occurs, do not drop the user's offline work or overwrite it silently. Move the outbox item to failed_permanent, mark the local row sync_status = 'conflict', store the server's latest copy alongside it, and surface a lightweight "Review changes" badge in the UI.
5. Background Sync Workers & Delta Pulls
Triggering sync only when the user opens the app leaves stale data sitting on phones for days. Combine three triggers:
- Foreground connectivity listener: Flush the outbox immediately when connectivity transitions from offline to online (debounced by 2 seconds to survive rapid Wi-Fi/cellular flapping).
- OS background schedulers: Register
WorkManageron Android andBGAppRefreshTaskon iOS so pending outbox items still upload after the user locks their screen. - Cursor-based delta pulls: Never re-download the entire catalog on every sync. Pass
?updated_since=2026-03-31T08:12:00Z&cursor=...(using a server-issued monotonic sync cursor, not the phone's clock) and soft-delete removed records withdeleted_attimestamps so mobile SQLite caches stay accurate.
Exponential Backoff with Jitter
If your backend returns 503 Service Unavailable during a deploy, 5,000 offline devices retrying simultaneously every 5 seconds creates a thundering herd. Always increment retry_count and compute next_retry_at with exponential backoff plus random jitter (min(300s, 2^retry_count) + random(0..15s)). For permanent 422 Validation Error responses, stop retrying immediately and surface the validation message to the user.
For more on pairing clean mobile sync layers with versioned Laravel or FastAPI backends, read our mobile app development guide for global startups or browse our shipped work.
Need Help Architecting an Offline-First Mobile App?
Retrofitting an online-only app for offline reliability usually costs twice as much as designing the SQLite outbox and versioned sync API cleanly from week one. At SmitDev, I work directly with engineering teams and founders across the US, UK, Europe, UAE, Australia, and Singapore to design and ship offline-capable Flutter, React Native, and backend sync systems.
Whether you need a focused architecture review through Launch Consulting or hands-on senior engineering at $55/day (8–9 hours of focused work) or $1,999/month for dedicated delivery, we build systems that never lose user data in the field.
Get in touch with your data model and sync requirements to map out a reliable offline architecture.