Offline-first is a queue problem, not a caching problem
Most "offline support" implementations are caches. They store the last response and show it when the network is down. That works for reading. It fails for writing, because a write has to survive, in order, and reach the server later. An offline-first app is a queue with a UI attached. The shape that works The user action writes to local storage first. The same write is appended to a queue with a…
Most "offline support" implementations are caches, storing the last response and displaying it when the network is unavailable. This works for reading but fails for writing, as a write must reach the server in the order it was made and survive crashes. An offline-first application acts as a queue with a user interface. The user action writes to local storage first, which is then appended to a queue with a unique local identifier.
The UI confirms immediately, without waiting for the network. A sync worker processes the queue in order when connectivity is restored, retrying each item until it succeeds or is explicitly marked as failed. The server uses the local identifier as an idempotency key, preventing duplicate retries after timeouts. The crucial factor is completing step 1 (local storage) before step 3 (network sync).
If the UI displays "saved" but the write is only in memory, a crash will result in data loss. If the write is on disk, the operation succeeds. Queues ensure proper ordering and prevent duplicate retries, unlike parallel firing. Idempotency keys prevent duplicate writes when the network times out on the device. To test, put the app in airplane mode, create three records, force-quit and reopen, or disable the network mid-request.
All should sync once, in order. Create a record, disable the network mid-request, then re-enable, ensuring the record appears exactly once. When two devices edit the same record offline, establish a conflict resolution rule beforehand; last-write-wins is a choice, not a default. If the app crashes during sync, the queue should resume without skipping items.
Developers should test edge cases; otherwise, "offline support" may only show a spinner and warning. Daily Cash, an Android ledger built with Flutter and Supabase, exemplifies this pattern. Entries are written locally first, replayed when connectivity returns, and exported to PDF and Excel, with row-level security ensuring user data isolation. The source costs $15 with a commercial license.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to