In an offline field-inspection mobile app, what does each outbox entry hold, and in what order must the entries replay?
answer
- intent, not a record snapshot
- the id is minted on the device
- key stored with the entry
- order within a job, not globally
- a local sequence, not the clock
basics
~20 sEach 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.
solid answer
~50 sAn entry records one intended change: a local sequence number, the operation (`create_inspection`, `add_finding`, `close_inspection`), the target record's id minted on the device, the fields it sets, the lane and entries it depends on, an idempotency key created with the entry, the base revision when the record already existed, and delivery state - attempts, next attempt time, last error. Because ids are client-generated UUIDs, a finding can name its inspection before the server has ever seen it, so nothing waits for a server id. Order is needed only where it matters: edits to the same record in the order they were made, and a parent before its children. A lane per inspection gives both, while separate inspections drain independently so one stuck job does not hold up the rest. The local sequence, not a timestamp, decides order.
code
json · 20 lines{
"entryId": "0199b8a2-61f0-7c2e-9a41-3d7e5b20c8f1",
"seq": 418,
"lane": "inspection:0199b8a1-0b3c-7f45-8e12-6a9d0c4b7e23",
"op": "add_finding",
"recordId": "0199b8a2-5e7a-7d10-b3c4-81f2a6d09e57",
"payload": {
"inspectionId": "0199b8a1-0b3c-7f45-8e12-6a9d0c4b7e23",
"severity": "major",
"text": "Corroded flange on pump 3 discharge line"
},
"dependsOn": ["0199b8a1-0b40-7a22-9c5e-2f81d3a6b094"],
"idempotencyKey": "5f0c2d9e-8b41-4a7f-9e3d-c16a72b84f05",
"baseRevision": null,
"state": "pending",
"firstSentAt": null,
"attempts": 0,
"nextAttemptAt": null,
"lastError": null
}go deeper
Recall that each queued change records what to do, to which record, and with which values, and that changes to one record go out in the order they were made.
Explain every field of an entry and why it is there, how client-generated ids let a child reference an unsent parent, and why a local sequence orders the queue.
Justify the lane boundary - per job, with explicit cross-lane dependencies - against a global queue, and show how one-in-flight-per-lane keeps replays from reordering writes.
Decide whether the backend should accept client-generated ids for every offline-writable type, weighing the API change against the queue complexity of id swapping.
## The situation At 08:10 a technician arrives at a site with no signal, creates an inspection, ticks forty checklist items, adds three findings, edits one of them, and closes the job at 11:30. Every action produced an **outbox entry**. When the van reaches coverage the queue drains. Two design decisions decide whether that drain is correct: what each entry carries, and the order entries go out in. ## What an entry holds An entry describes one intended change with everything needed to send it later - after a relaunch, hours on - without re-reading state that may have changed since: - **Entry id and a local sequence number.** The sequence comes from the local database at commit and fixes creation order on this device. - **Operation**: `create_inspection`, `set_item`, `add_finding`, `update_finding`, `close_inspection`. Recording the **intent**, rather than a copy of the whole record, keeps requests small and avoids overwriting fields that someone else changed on the server. - **Target record id**, minted on the device. - **Payload**: the fields the operation sets. - **Lane and dependencies**: the group whose order matters (here, the inspection) and any entries that must succeed first. - **Idempotency key**, created once with the entry and stored in it, so every replay is recognisably the same request. - **Base revision**, when the record already existed on the server, so the server can spot an edit made against an old copy. What happens on a stale base is the conflict policy's decision, not the queue's. - **Delivery state**: pending, in flight or parked; when it was first sent, if ever; attempt count; next attempt time; last error. ## Client-generated ids remove the waiting If the server assigns ids, a finding cannot name its inspection until the inspection's create has come back. The queue must then hold the finding, wait, and rewrite its reference afterwards. With **client-generated ids**, the phone mints a UUID when the inspection is created and every later entry refers to it. Nothing waits for a round trip, and no queued payload is ever rewritten. Swapping temporary ids for server ids - and what the screen shows meanwhile - is a separate problem that exists only when the server insists on assigning ids. RFC 9562 notes that UUIDs can be created without coordination, which is why they suit offline creation. It also notes that random **version 4** values have poor index locality in a B-tree, because successive values insert at random positions, while **version 7** values start with a timestamp and sort near each other. That helps the local and server indexes. It does **not** make the id an ordering tool: the timestamp comes from the device clock, the RFC makes no guarantee about how close that clock is to real time, and users can set it by hand. Order comes from the local sequence number. ## Ordering: global, per record, or per lane | Order | What it guarantees | What it costs | |---|---|---| | One global queue | Every entry goes out in creation order | One stuck entry holds up every job behind it | | Per record only | Edits to one record apply in order | A finding can arrive before its inspection exists | | Per lane (per inspection) plus explicit dependencies | Order within each job; jobs drain independently | A little more bookkeeping per entry | The real requirement is narrower than "everything in order": **edits to the same record must apply in the order they were made, and a child must not arrive before its parent.** A lane per inspection gives both, because the inspection's create, its checklist items and its findings sit in one ordered line. An entry that crosses lanes - a finding that also references a shared equipment record - lists that dependency explicitly. Separate inspections are independent, so a slow or failing job does not hold up the others. A single global queue is still a correct and simple choice for a small app; its cost is head-of-line blocking, which matters once one entry can fail for days. ## Draining in order 1. Pick the oldest pending entry in each lane whose dependencies have all succeeded. 2. Send it with its stored idempotency key. 3. On success, in one local transaction, apply what the server returned and delete the entry. 4. Only then move to the next entry in that lane. Lanes can drain in parallel, but within a lane one entry is in flight at a time. That is what keeps the order honest: if the second entry were sent while the first was still unanswered, a retry of the first could land after it on the server.
- Why not order the whole queue globally and keep it simple?A global queue is correct and easy to reason about, and for a small app it can be the right call. Its cost is head-of-line blocking: one entry the server keeps rejecting stops every job behind it. Lanes per inspection keep the order that matters, within a job, while letting other jobs through.
- Could the UUIDv7 ids themselves order the queue?No. A version 7 id starts with a timestamp from the device clock, which may be wrong or set by hand, and RFC 9562 makes no promise about how close that value is to real time. A sequence number assigned by the local database at commit gives a reliable creation order on the device.
- What changes if the server insists on assigning ids?Dependants cannot be sent until the parent's create returns its id. The queue holds them, then in one transaction records the mapping and rewrites the queued references before releasing them. It works, but it serialises every child behind a round trip and adds a rewrite step that client-generated ids avoid.
- A finding cannot be queued until the server has returned its inspection's id.
- Sending the whole record every time is harmless because the device's copy is newest.
- Order the queue by each entry's device-clock timestamp.
- Writes to unrelated inspections must go out in one global order to stay correct.
- A random UUIDv4 primary key costs a B-tree index nothing.