State & Architecture

Outbox Queues, Idempotency Keys & Causal Ordering

Prevents duplicate charges and out-of-order server overwrites on flaky networks by persisting mutations in an IndexedDB FIFO outbox with UUID idempotency keys.

About this page
READ
2 min
Interactive Simulator

Outbox Queue & Idempotency Key Retry

Trace how an IndexedDB outbox persists a mutation key, survives a dropped response, and deduplicates on retry.

IDB Outbox [1]
Network Worker
API Server
Step 1 / 3: 1. Enqueue Intent with Fixed UUID

User submits order; client generates Idempotency-Key once, writes to IndexedDB, and updates UI optimistically.

await idb.put('outbox', { seq: 1, key: '9b1d-...', body });
Internal State / Memory
outboxLength
1
idempotencyKey
9b1d-4c2a
attempt
0
Outbox Queue & Idempotency Key Retry

Under the Hood#

An outbox separates intent capture from network delivery. When a user triggers a mutation, the client writes an immutable command record into IndexedDB and applies an Optimistic UI patch synchronously.

  1. Deterministic key: Generate crypto.randomUUID() once at enqueue time and store it on the outbox record so every retry sends the identical Idempotency-Key header.
  2. Causal FIFO drain: Dispatch head-of-line mutations sequentially (or partitioned per entity ID) so UPDATE never overtakes CREATE on the wire.
  3. Terminal vs transient errors: Retry 5xx or network timeouts with exponential backoff; dequeue and roll back on 4xx validation rejections.
TS
interface OutboxItem { id: string; key: string; payload: unknown }

async function drainOutbox(queue: OutboxItem[]) {
  for (const item of queue) {
    const res = await fetch('/api/orders', {
      method: 'POST',
      headers: { 'Idempotency-Key': item.key, 'Content-Type': 'application/json' },
      body: JSON.stringify(item.payload),
    });
    if (res.status >= 500) break; // Keep head-of-line order; retry later
    await removeFromIdbOutbox(item.id);
  }
}

Problem Genealogy#

  • 2014–2018 (Fire-and-Forget Retries): Naive fetch retry wrappers regenerated request payloads on retry without keys, creating duplicate invoices when ACKs dropped on 3G networks.
  • 2019+ (Stripe & IETF Idempotency-Key): Standardized client-generated UUID keys cached server-side for 24 hours to replay original responses safely.
  • 2022+ (Local-First Outboxes): Combined IndexedDB persistence with causal queues so mutations survive full browser restarts.

Production Pitfalls#

  • Duplicate records after mobile tunnel drop: Generating crypto.randomUUID() inside the retry loop instead of at enqueue time gives each attempt a fresh key, defeating server deduplication.
  • Head-of-line blocking across unrelated entities: Sending all mutations through one global FIFO queue stalls a user's profile edit behind a failing 10 MB attachment upload.

Where this connects

Prerequisites

State & Architecture

Optimistic UI & Rollback Safety

Queued mutations apply optimistic UI updates immediately and reconcile or roll back when the outbox drains.

Seen in Architecture Autopsies