A user taps "Save" four times on a train with no signal. Two minutes later the phone reconnects and your backend receives four writes in an order you didn't choose, from a clock you don't trust, one of which will throw a 422 forever. If your Flutter offline queue is a list in SQLite plus a retry loop, you've built a distributed system without any of the guarantees. It just hasn't bitten you yet.
Here is the framing we use at orithLabs when building mobile apps: the local write queue is a single-producer log, the server is a consumer across an unreliable network, and every classic failure mode of replicated logs applies. Below are three that show up in production, and the data structures that contain them.
Failure mode 1: reordered mutations
The naive queue is a table with an auto-increment id, drained by a loop that fires requests concurrently, or sequentially but with per-request retry that re-enqueues at the tail. Both reorder. Concretely: create_task(A), then rename_task(A). If the create fails transiently and gets pushed behind the rename, the server sees a rename for an entity that doesn't exist and returns 404. Your retry logic then treats a 404 as permanent and drops the rename. The user's data is silently wrong.
The fix is to be explicit about what must be ordered. Total ordering of the whole queue is usually too strict, because one stuck item blocks unrelated work. What you need is ordering per entity:
- Each queue row carries a monotonic
seq(local, gap-free is not required) and anentity_keysuch astask:uuid. - The drainer picks the lowest-
seqpending row perentity_key, and may run different keys in parallel with a small bounded pool. - Retries update the row in place (
attempts,next_attempt_at). They never re-insert, soseqnever changes.
Use client-generated UUIDs for entity ids so a dependent mutation can be enqueued before the create has reached the server. Without that, you'll be tempted to block the UI on a round trip, which defeats the point of offline-first.
Also make every request idempotent. Send the row's own id as an idempotency key and have the backend store it with a unique constraint. A request that succeeded but whose response was lost will be replayed; the key turns the replay into a no-op instead of a duplicate. This is the single cheapest correctness win in the whole design, and it belongs in your REST API contract from day one, not as a retrofit.
Failure mode 2: poison items blocking the queue
Strict per-entity ordering creates a new hazard: head-of-line blocking. One mutation the server will never accept, such as a payload that fails a validation rule added in a newer backend version, sits at the head of its entity's lane and every later write behind it waits. We've seen audits where a single malformed item had been retried every app launch for months while the user's subsequent edits piled up unsent.
The remedy is the same one message brokers use: classify errors and bound retries.
- Transient (timeouts, 502/503/504, 429): exponential backoff with jitter, stored as
next_attempt_atso it survives app restarts. RespectRetry-After. - Permanent (400, 409 unresolvable, 422): no retry. Move the row to a
dead_lettertable with the response body and app version. - Auth (401/403): pause the whole queue, not the row. Refresh the token, then resume. Burning attempts on an expired session is how you create fake poison.
The dead-letter table matters because it makes the failure visible. Decide up front what happens to dependents of a dead-lettered mutation: usually you mark the entity as "diverged" and surface it in the UI, rather than pushing later edits that assume the earlier one landed. Ship a way to export the dead-letter rows with your crash reporting; it's the fastest route to finding the backend/client contract drift that produced them.
Failure mode 3: clock skew and last-write-wins
Conflict resolution is where teams reach for updatedAt and last-write-wins. The problem is whose clock. A device that's three minutes fast, or a user who has manually set their time, will win every conflict against a correct device, indefinitely. Worse, a queued edit made at 09:00 offline and replayed at 11:00 carries a timestamp older than a server-side change made at 10:00, so the stale-looking write loses even though the user made it with full knowledge of the prior state.
Wall-clock time answers the wrong question. The right question is "what version of this entity did the writer see?" So:
- Each entity has a server-assigned integer
version, incremented on every accepted write. - Each queued mutation carries
base_version, the version the client last observed. - The server compares: if
base_version == current, apply. If not, it's a real concurrent edit and the server returns 409 with the current state.
On a 409 the client doesn't guess. For field-level independence (user A edits the title, user B edits the note) do a three-way merge using the stored base snapshot, the server state, and the local change. For fields that genuinely collide, apply an explicit policy: server wins, local wins, or ask the user. Keeping the base snapshot costs storage, but it's what makes the merge decidable. If you truly need convergence without a coordinator, look at hybrid logical clocks or CRDTs, but most business apps with a central backend are better served by versions than by clever clocks.
What to test
None of this is exercised by happy-path widget tests. The tests that earn their keep are property-style: generate a random sequence of mutations, inject random failures (timeouts, duplicated deliveries, killed processes between "request sent" and "row deleted"), and assert the server's final state equals a sequential model. Kill-the-app-mid-drain is the case that finds the most bugs, because it's where non-atomic "send then delete" logic leaks. Do the delete and the state update in one SQLite transaction, keyed by the idempotency key.
orithLabs builds and audits mobile and backend systems like this, and the offline queue is a common place where an otherwise solid Flutter codebase turns out to be fragile. If you're debugging a sync problem you can't reproduce, or want a second pair of engineers to review your queue design before it hits production, you can talk directly to the people who write the code.