Lesson 5 of 9 · 29m
Mutation Outbox, Idempotency, and Conflict Resolution
Queue user intent durably, make retries safe with idempotency keys, and choose conflict semantics that match the product rather than the database.
Every offline write is a promise: the user did something now, and the system will reconcile it later. The easiest way to break that promise is blind replay. Networks retry, users double-tap, browsers suspend tabs mid-flight, and servers process requests before responses are lost. If mutations are not idempotent, sync eventually duplicates money, messages, inventory, or work.
The outbox is not a request log
Store intent at the domain level:
{
"mutationId": "mut_01JDG8GFWT8D6T3KPHSKZQ7A68",
"entity": "note",
"entityId": "01JDFZQ8N8M7M6N6QJ0D0F6S3H",
"op": "update",
"payload": {"title": "Launch checklist"},
"baseRevision": 14,
"state": "pending"
}mutationId is created once and reused on every retry. It is not a per-request ID because retries of the same intent must collapse server-side. baseRevision captures what the user saw when editing; it is the starting point for conflict detection.
The server must enforce idempotency
A request carries the key in Idempotency-Key. If the key was already accepted, return the original result; if it is in progress, return conflict or retry guidance; if it is new, process once. A relational implementation often uses a unique key:
CREATE TABLE mutation_results (
client_id uuid NOT NULL,
mutation_id text NOT NULL,
entity_id text NOT NULL,
response jsonb NOT NULL,
PRIMARY KEY (client_id, mutation_id)
);Client-side idempotency keys without server enforcement are decorative.
Sync order is dependency order
Do not flush the outbox randomly. Create operations often establish objects later mutations depend on: create note A, update note A, attach file B, then share note A. Process per-entity queues in order and parallelize independent entities. Deletes are sensitive: updating after a delete may be pointless, while recreating after a delete might be product-level resurrection.
Conflict semantics are product semantics
last write wins is acceptable for low-stakes fields. Field-level merge can work for independent metadata. Manual resolution is required for destructive operations. A small merge might be:
function mergeNote(local, server, base) {
return {
...server,
title: local.title !== base.title ? local.title : server.title,
body: local.body !== base.body ? local.body : server.body,
revision: Math.max(local.revision, server.revision) + 1,
};
}This is not collaborative editing. If realtime multi-user editing is a requirement, use CRDTs or operational transformation rather than inventing half a merge algorithm in a sync queue.
Retry and user state
Use exponential backoff with jitter for transport failures, for example min(30s, 500ms * 2 ** attempts) + jitter. Do not backoff validation errors, authorization errors, or structural conflicts. Show honest state: synced, waiting, blocked, or needing conflict resolution. Only server acknowledgement clears intent.
Field notes
- Generate mutation IDs before first transmission. A new key per retry defeats the design.
- A server response can be lost after commit. Assume at-least-once delivery.
- Clocks are unreliable across devices; use server revisions for ordering decisions.
- Attachments need separate idempotent upload semantics, often with chunk checkpoints.
- Test by killing the browser after the request is sent but before the response returns.
Was this lesson helpful?