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
Outbox Queue & Idempotency Key Retry
Trace how an IndexedDB outbox persists a mutation key, survives a dropped response, and deduplicates on retry.
User submits order; client generates Idempotency-Key once, writes to IndexedDB, and updates UI optimistically.
await idb.put('outbox', { seq: 1, key: '9b1d-...', body });- outboxLength
- 1
- idempotencyKey
- 9b1d-4c2a
- attempt
- 0
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.
- Deterministic key: Generate
crypto.randomUUID()once at enqueue time and store it on the outbox record so every retry sends the identicalIdempotency-Keyheader. - Causal FIFO drain: Dispatch head-of-line mutations sequentially (or partitioned per entity ID) so
UPDATEnever overtakesCREATEon the wire. - Terminal vs transient errors: Retry
5xxor network timeouts with exponential backoff; dequeue and roll back on4xxvalidation rejections.
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
fetchretry 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
Optimistic UI & Rollback Safety
Queued mutations apply optimistic UI updates immediately and reconcile or roll back when the outbox drains.
IndexedDB Transactions and Storage Quotas
Persisting the outbox inside IndexedDB readwrite transactions survives tab closes and offline periods.
Seen in Architecture Autopsies
Autopsy #01: Double Payment on a Disabled Button
Without an Idempotency-Key, the server cannot tell a second POST from a new purchase, so a retried request becomes a second charge.
Autopsy #08: Four Hours on a Train, Three Hours of Work Erased
Flaky cellular 4G retries replay outbox mutations without idempotency keys, duplicating or reordering operations.