skip to content

Outbox & Queued Writes

Offline writes go into a durable queue that survives a kill, replays in order with idempotency keys, and parks items the server keeps rejecting. Interviewers ask what happens to a write in a tunnel.

on this pageshow

explore

questions

5

In an offline field-inspection mobile app, why must a checklist change and its outbox entry be written to disk in one local transaction?

level: juniorimportance: must knowfreq 55%

answer

  1. who decides when the process ends
  2. memory dies with the process
  3. two commits leave a crash window
  4. same database, one commit
  5. saved here is not synced

basics

~20 s

A backgrounded app can be killed with no shutdown code, so an in-memory queue dies with it. Committing the record change and its outbox entry in one transaction means no kill leaves a change shown but unsent, or queued but not shown.

solid answer

~40 s

Both mobile platforms may end a backgrounded app's process to reclaim resources without running teardown code, and users swipe apps away and batteries die, so a queue held only in memory is lost. The outbox therefore lives as a table in the same embedded database as the inspection data. If the tick and its queue entry were committed separately, a kill between the two commits leaves either a tick the server will never hear about, or a queued tick the screen no longer shows. One transaction makes them commit or roll back together. Only after that commit does the app treat the change as saved on the device; sending is a later, separate step that reads the queue.

code

pseudocode · 16 lines
pseudocode
function tick_item(item_id, checked):
  begin transaction
    update checklist_items
      set checked = checked
      where id = item_id
    insert into outbox
      entry_id = new_uuid()
      seq = next_local_sequence()
      op = "set_item"
      record_id = item_id
      payload = { checked: checked }
      idempotency_key = new_uuid()
      attempts = 0
      state = "pending"
  commit
  wake_sync_step()

go deeper

for a junior

Recall that the app can be killed with no warning, so a change is real only once it is on disk, and that the record and its queue entry are written together.

for a middle

Explain the crash window between two separate commits, what each write order breaks after a kill, and why the outbox is a table in the same embedded database.

for a senior

Show that sending is a separate drain step, that 'saved' and 'synced' are different states, and what a local commit cannot protect against, such as an uninstall.

for a principal

Weigh making the shared data layer enforce this rule for every feature team, so no screen can change a record without its queue entry, against the cost of a stricter write API.

## The situation A technician inspecting pumps in a plant basement ticks "pressure relief valve tested" on a checklist. There is no signal. The app has to do two things with that tap: change the checklist item in its **local database**, so every screen shows it ticked, and remember that the server has not heard about it yet. The second thing is the **outbox** - a durable queue of pending writes that a sync step drains when the network returns. This question is about where that queue lives and how the two writes are tied together. ## Why a queue in memory is not a queue A mobile app does not decide when its process ends. Several things end it with no warning and no shutdown code: - **The OS reclaims memory.** Both native platforms may end a backgrounded or suspended app's process to free resources, and neither promises that teardown code runs first. - **The user swipes the app away** from the app switcher. - **The battery dies or the app crashes** while work is still in memory. In a field app all of these are routine: the technician pockets the phone, opens the camera, takes a call, and the app is gone before the van reaches coverage. A queue held in a list in memory is lost every time one of these happens; one written to disk only when the app goes to the background is lost whenever a crash or a flat battery comes first. So the queue must be on persistent storage before the app treats a change as saved. It must also be in the app's **persistent** storage, not in the OS's purgeable cache folder: the storage guides of both platforms say the system may delete cache files when the device runs low on space. ## Why two durable writes are still not enough Persisting the queue in a separate file, or committing the record and the entry in two separate transactions, opens a crash window between them. Which failure you get depends on the order: | Write order | A kill lands between the writes | What the app finds after relaunch | |---|---|---| | Record first, then queue entry | after the record commit | The item shows ticked, but nothing will ever tell the server | | Queue entry first, then record | after the entry commit | The server will receive a tick the local screen does not show | | Both in one transaction | anywhere | Either both exist or neither does; at worst the technician taps again | The window is short, but an app used all day by many technicians, on older phones under memory pressure, will hit it. Both failures are silent: no error appears, and the office simply receives a report that differs from what the technician saw. ## The design: one store, one transaction The fix is to keep the outbox as a table in the **same embedded database** as the inspection data, and to write both rows in one transaction: 1. Open a transaction. 2. Update the checklist item's row. 3. Insert the outbox entry describing the change - operation, the record's id, payload, an idempotency key, an attempt count of zero. 4. Commit. Only now does the UI treat the change as saved on this device. 5. Nudge the sync step. The nudge is only a hint: if the app dies before it fires, the entry is still in the table and the next drain finds it. Sending is therefore never part of the tap. The tap ends at the commit; delivery is a separate step that reads the queue, which is what lets the app keep working for hours offline. The same rule covers every write the technician makes - a new finding, a photo caption, closing the job. ## "Saved" and "synced" are different words Once the commit lands, the change is **saved on this phone**: it survives a kill, a reboot and a flat battery. It is not yet **synced**: the office cannot see it, and no copy exists anywhere else. Good designs keep those two states apart in what they show. How one record displays its pending state belongs to the design of optimistic state; the queue's job is to guarantee that what the screen shows and what will be sent can never drift apart. ## What the transaction does not protect The local commit protects against **process death**, not against losing the app's storage: - an uninstall that removes the app's private files takes unsent entries with it; - clearing the app's data, or a lost or reset phone, does the same; - a device backup, where one exists, is not a route to the app's server. So the outbox should drain as soon as coverage returns, and the app should show how much is still waiting - a technician who can see "12 changes waiting" does not reinstall the app to fix a glitch with a day's work unsent.

  • Why not mark changed records as dirty and work out what to send when the network returns?
    A dirty flag works when only a record's latest state matters. It loses what the outbox keeps: the order of operations across records, intents such as a create followed by a delete, and a stable idempotency key per change. For a job with creates, children and a final close, those are exactly what the server needs, so an explicit queue is the safer design.
  • Does committing locally make the technician's change safe from everything?
    No. It survives a kill, a crash, a reboot and a flat battery. It does not survive anything that deletes the app's storage - an uninstall that removes private files, clearing the app's data, a lost or reset phone. That is why the queue should drain at the first chance and the app should show how many changes are still unsent.

  • The OS always gives the app time to flush its queue before the process is killed.
  • Keeping pending writes in memory is fine because sync runs right after the edit.
  • Writing the record and the queue entry in two commits is fine; the gap is too short to matter.
  • A change can be shown as saved as soon as its request has been sent.
  • Once a change is committed locally, it is safe even if the app is uninstalled.
  • The outbox database can live in the cache folder because the OS never clears it.
open as a page

In an offline field-inspection mobile app, what does each outbox entry hold, and in what order must the entries replay?

level: middleimportance: must knowfreq 52%

basics

~20 s

Each entry holds the operation, the target record's device-minted id, the payload, its dependencies, a stored idempotency key and delivery state. Edits to one record replay in creation order and a child never precedes its parent; independent jobs drain separately.

open as a page

In a field-inspection app's offline outbox, when is a queued write's idempotency key created, and how does that limit merging later edits into it?

level: middleimportance: should knowfreq 48%

basics

~20 s

The key is minted once with the entry and stored in it, so every replay, even after a relaunch, sends the same key. Since a key stands for one payload, later edits may be merged only into an entry never attempted.

open as a page

In an offline field-inspection mobile app, what app-wide sync status should technicians see, and what must happen to unsent outbox entries at sign-out?

level: middleimportance: should knowfreq 42%

basics

~20 s

Show counts derived from the outbox itself: changes waiting, sending, needing attention, and when it last synced. At sign-out, warn about unsent entries, try to send them, never send them under another account, and discard only on explicit confirmation.

open as a page

In an offline field-inspection mobile app, the server rejects a queued finding with 422 on every attempt and later entries depend on it; how should the outbox keep draining, and what does the technician see?

level: seniorimportance: should knowfreq 46%

basics

~20 s

Classify the response: temporary failures retry later with backoff, but a 422 will fail again unchanged. Park that entry with its error, hold everything that depends on it, keep draining unrelated entries, and show the technician a fix-or-discard choice.

open as a page