Keep Offline Changes In A Small Durable Outbox
An edit appears immediately, but the network request fails. Keeping a closure in memory is enough to retry while the app lives. It is not enough when the process ends before the request succeeds.
A small durable outbox records what still needs delivery next to the local state it represents. The central invariant is simple: either the edit and its queued operation both persist, or neither does.
Store an operation, not a reference to mutable UI state
For an app-owned SQLite store, start with a deliberately small schema:
CREATE TABLE notes (
id TEXT PRIMARY KEY,
title TEXT NOT NULL
);
CREATE TABLE outbox (
operation_id TEXT PRIMARY KEY,
note_id TEXT NOT NULL,
payload TEXT NOT NULL
);
When the user saves, generate an operation ID and serialize the exact intended payload. Update the note and insert the operation inside one SQLite transaction:
BEGIN IMMEDIATE;
UPDATE notes SET title = ? WHERE id = ?;
INSERT INTO outbox(operation_id, note_id, payload)
VALUES (?, ?, ?);
COMMIT;
Bind parameters, check the expected record exists, and roll back on failure. A duplicate operation ID in this example fails the insert and must also roll back the note edit.
The payload belongs to that operation. Do not reconstruct it later by reading the current note: a newer local edit may have changed the note while the older request was in flight.
Keep the ID stable across retries
Suppose operation A updates the title, the server applies it, and the response is lost. Retrying with a fresh ID can apply the same logical mutation twice.
Use the same operation ID and payload for every retry of A. The server must implement an appropriate deduplication or idempotency contract if duplicate side effects matter. An arbitrary custom request header does nothing unless the server understands it.
HTTP defines idempotency for particular method semantics, but a custom mutation still needs an explicit contract. Our outbox does not create exactly-once delivery across a network. It makes unfinished local intent durable so at-least-once attempts can be handled safely.
For an existing backend SDK that already persists offline writes, reuse its supported queue and conflict behavior where it satisfies the requirement. Adding a second outbox can duplicate operations and make ownership less clear.
Acknowledge exactly what succeeded
While A is in flight, the user creates operation B for the same note. When A succeeds, remove A:
DELETE FROM outbox WHERE operation_id = ?;
Do not delete every queued operation for that note. B is newer work and has not been acknowledged. Deleting A a second time should be harmless.
Likewise, do not blindly replace the current UI model with A’s older server response. Reconcile the acknowledged revision against later local edits. The queue and the editor need a shared account of which version each result represents.
Decide ordering and conflicts before adding workers
The tiny schema demonstrates durability and exact acknowledgement. A production queue also needs account ownership, operation ordering, attempt metadata, and a retry policy appropriate to the API.
If edits to one note depend on one another, send them in order or use a server revision precondition. A database row’s incidental retrieval order is not a queue order; add an explicit sequence and ORDER BY. Do not let a faster response from an older mutation overwrite a newer one.
Coalescing is possible only when semantics allow it. Several not-yet-sent “set title” operations may be replaceable; increments, purchases, and append operations generally cannot be collapsed the same way. Never change a payload after sending it while reusing its idempotency identity.
Retry transient failures with backoff. Keep authentication failures and conflicts visible as distinct states rather than hot-looping them. Logout must quarantine or remove account-scoped pending work according to the product’s data policy.
Test a restart between commit and delivery
The fixture writes an edit and operation, closes the connection, and reopens the database. Both survive. It then queues a second edit, acknowledges the first twice, and confirms the second remains. Finally, a duplicate insert triggers rollback of the accompanying note change.
September 2026 review: those persistence and rollback checks passed on a temporary SQLite file. The fixture does not implement a server, network deduplication, worker leases, or device crash-durability testing; those must match the application’s real synchronization contract.