Part IV β The Reach Β· Lesson 9.3
Offline Writes & the Sync Queue
In one lineWrite locally first, queue every change, and replay the queue so the server applies each change exactly once.
Where you've seen itWhatsApp on a train β the clock icon means the message is sitting in the outbox; the first tick means the server has it.
Diagram
THE OUTBOX RECORD β everything a replay needs, nothing more
{
opId: "c1a7β¦", made on the client = idempotency key
url: "/api/notes/42", what it touches
method: "PATCH", create / update / delete
patch: { title: β¦ }, the change, not the whole object
baseVer: 7, version the edit started from (9.4)
attempts: 2, drives backoff and "give up"
createdAt: 1696β¦ keeps replay in order
}How it works
- Write the record and its outbox entry in one IndexedDB transaction (6.5) β the UI updates instantly and the change can't be lost between the two.
- Create the
opIdon the client, before the first attempt, and send it as an idempotency key; a retry after a lost response is then harmless. - Replay oldest first β an update must never overtake the create it depends on.
- Push, then pull everything since the saved cursor, so your own edits come back already acknowledged instead of as surprises.
- Retry with backoff; park, don't drop. After repeated failures or a 4xx, mark the op "needs attention" where the user can see it.
async function flush() {
for (const op of await outbox.oldestFirst()) {
const res = await fetch(op.url, {
method: op.method,
headers: { 'Content-Type': 'application/json', 'Idempotency-Key': op.opId },
body: JSON.stringify({ patch: op.patch, baseVer: op.baseVer }),
});
if (res.status === 409) return resolveConflict(op, await res.json()); // 9.4
if (!res.ok) return outbox.backoff(op); // stop here β order matters
await outbox.ack(op, await res.json());
}
await pullChangesSince(await cursor.get());
}Trade-offs
| Choice | Cost |
|---|---|
| Send patches, not whole objects | The server must apply field-level changes |
| Strict ordering | One stuck op blocks everything behind it |
| Idempotency keys | The server stores keys for a retention window |
| Optimistic UI from the outbox | You must show "pending" and "failed" honestly |
Collapse before you send: five offline edits to the same note can become one patch β fewer requests, fewer chances to conflict.
Interview angle
"The user taps Send twice on a flaky connection. How do you avoid a duplicate message?"
- The client creates the message ID once, before the first attempt, and every retry reuses it as the idempotency key.
- The server records keys it has already applied and returns the stored result for a repeat instead of applying it again.
- The UI renders from the local outbox by that same ID, so the screen shows one message too β clock, then tick.
Recap
- Local write and outbox entry in one transaction; the UI never waits for the network.
- Client-made IDs as idempotency keys make every retry safe.
- Push oldest-first, then pull from a cursor; park failures where the user can see them.