FSD Frontend System Design

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.

  • Time16 min
  • DiagramOutbox record + push-then-pull sync cycle
  • Chapter9 Β· Offline Support

Diagram

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
  }
diagram

How it works

  1. 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.
  2. Create the opId on the client, before the first attempt, and send it as an idempotency key; a retry after a lost response is then harmless.
  3. Replay oldest first β€” an update must never overtake the create it depends on.
  4. Push, then pull everything since the saved cursor, so your own edits come back already acknowledged instead of as surprises.
  5. Retry with backoff; park, don't drop. After repeated failures or a 4xx, mark the op "needs attention" where the user can see it.
js
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

ChoiceCost
Send patches, not whole objectsThe server must apply field-level changes
Strict orderingOne stuck op blocks everything behind it
Idempotency keysThe server stores keys for a retention window
Optimistic UI from the outboxYou 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.