Idempotency-Key Design
When to Use This Skill
Any POST or PATCH that has a side effect the caller must not repeat: charge a card, create an order, send a notification, initiate a transfer. If the client retries on a network timeout, the operation must not execute twice.
1. The Contract
POST /orders
Idempotency-Key: 550e8400-e29b-41d4-a716-446655440000
Content-Type: application/json
{ "items": [...], "total": 99.95 }
- The header name is
Idempotency-Key (title-case, per the IETF draft — [verify-at-build] on draft status).
- Value: a client-generated UUID v4 (or ULID). The server treats it as opaque.
- The server stores the key + response for the dedup window; re-submitting the same key returns the stored response without re-executing.
2. Server-Side Design Checklist
| Step |
Detail |
| Parse and validate |
If header absent on a non-idempotent-by-design endpoint → 422 or proceed without dedup (document which). If header present but malformed → 400. |
| Lock the key |
Atomically: check-and-set the key in a store (Redis SETNX, DB upsert). If key already locked and processing → 409 in-progress. |
| Execute or replay |
Key absent → execute normally, store (key, status, response_body, expires_at). Key present + complete → return stored response with original status. |
| Dedup window |
Minimum 24 hours, typically 7 days. Beyond the window, treat as a new request. Document the TTL. |
| Scope |
Key is scoped to the authenticated caller + endpoint. The same key from a different user is a different request. |
3. Storage Schema (minimal)
CREATE TABLE idempotency_keys (
key TEXT NOT NULL,
user_id UUID NOT NULL,
endpoint TEXT NOT NULL,
status SMALLINT NOT NULL,
response JSONB NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
expires_at TIMESTAMPTZ NOT NULL,
PRIMARY KEY (key, user_id, endpoint)
);
4. Response Behaviour Table
| Scenario |
Status returned |
Notes |
| First request, succeeds |
201 or 200 |
Store response |
| Retry with same key, completed |
Same as first (201/200) |
Return stored body |
| Retry with same key, still processing |
409 in-progress |
Retry-After header |
| Key expired, resubmitted |
Treat as new request |
Document TTL |
| Wrong user submitting same key |
Normal processing (different scope) |
Keys are user-scoped |
| Key reused for different payload |
422 idempotency-key-reuse |
Detect via payload hash if desired |
5. OpenAPI Declaration
/orders:
post:
operationId: createOrder
parameters:
- name: Idempotency-Key
in: header
required: true
schema:
type: string
format: uuid
description: |
Client-generated UUID v4. Safe to retry within 7 days.
Re-submitting the same key returns the stored response.
responses:
"201":
description: Order created
"409":
description: Key in progress or payload mismatch
content:
application/problem+json:
schema:
$ref: "#/components/schemas/Problem"
6. Client Retry Algorithm
1. Generate key = UUID v4; store locally.
2. POST with Idempotency-Key header.
3. On network error or 5xx: wait (exponential backoff + jitter), retry with SAME key.
4. On 409 in-progress: wait Retry-After seconds, retry with SAME key.
5. On 2xx: success.
6. On 4xx (not 409): do NOT retry — fix the request.
Pitfalls
- Using a server-generated key instead of a client-generated one — the client can't retry if the first response never arrived
- Setting a dedup window under 24 hours — not sufficient for mobile clients on spotty networks
- Scoping the key globally (not per-user) — one user can accidentally collide with another
- Not storing the full response body — on replay the server must return identical status + body, not regenerate
- Making
GET idempotent with this mechanism — GET is already safe; only unsafe methods need it
See Also
1---2name: idempotency-key-design3description: "Playbook for designing safe-to-retry POST and PATCH operations using Idempotency-Key — covers key format, dedup window, stored-response replay, conflict handling, and OpenAPI declaration."4metadata:5 source: https://skillmd.com/skills/mcorbett51090/idempotency-key-design6---78# Idempotency-Key Design910## When to Use This Skill1112Any `POST` or `PATCH` that has a side effect the caller must not repeat: charge a card, create an order, send a notification, initiate a transfer. If the client retries on a network timeout, the operation must not execute twice.1314## 1. The Contract1516```17POST /orders18Idempotency-Key: 550e8400-e29b-41d4-a716-44665544000019Content-Type: application/json2021{ "items": [...], "total": 99.95 }22```2324- The header name is `Idempotency-Key` (title-case, per the IETF draft — `[verify-at-build]` on draft status).25- Value: a **client-generated** UUID v4 (or ULID). The server treats it as opaque.26- The server stores the key + response for the dedup window; re-submitting the same key returns the stored response without re-executing.2728## 2. Server-Side Design Checklist2930| Step | Detail |31|---|---|32| Parse and validate | If header absent on a non-idempotent-by-design endpoint → 422 or proceed without dedup (document which). If header present but malformed → 400. |33| Lock the key | Atomically: check-and-set the key in a store (Redis SETNX, DB upsert). If key already locked and processing → 409 `in-progress`. |34| Execute or replay | Key absent → execute normally, store `(key, status, response_body, expires_at)`. Key present + complete → return stored response with original status. |35| Dedup window | Minimum 24 hours, typically 7 days. Beyond the window, treat as a new request. Document the TTL. |36| Scope | Key is scoped to the **authenticated caller** + **endpoint**. The same key from a different user is a different request. |3738## 3. Storage Schema (minimal)3940```sql41CREATE TABLE idempotency_keys (42 key TEXT NOT NULL,43 user_id UUID NOT NULL,44 endpoint TEXT NOT NULL,45 status SMALLINT NOT NULL,46 response JSONB NOT NULL,47 created_at TIMESTAMPTZ NOT NULL DEFAULT now(),48 expires_at TIMESTAMPTZ NOT NULL,49 PRIMARY KEY (key, user_id, endpoint)50);51```5253## 4. Response Behaviour Table5455| Scenario | Status returned | Notes |56|---|---|---|57| First request, succeeds | 201 or 200 | Store response |58| Retry with same key, completed | Same as first (201/200) | Return stored body |59| Retry with same key, still processing | 409 `in-progress` | `Retry-After` header |60| Key expired, resubmitted | Treat as new request | Document TTL |61| Wrong user submitting same key | Normal processing (different scope) | Keys are user-scoped |62| Key reused for different payload | 422 `idempotency-key-reuse` | Detect via payload hash if desired |6364## 5. OpenAPI Declaration6566```yaml67/orders:68 post:69 operationId: createOrder70 parameters:71 - name: Idempotency-Key72 in: header73 required: true74 schema:75 type: string76 format: uuid77 description: |78 Client-generated UUID v4. Safe to retry within 7 days.79 Re-submitting the same key returns the stored response.80 responses:81 "201":82 description: Order created83 "409":84 description: Key in progress or payload mismatch85 content:86 application/problem+json:87 schema:88 $ref: "#/components/schemas/Problem"89```9091## 6. Client Retry Algorithm9293```941. Generate key = UUID v4; store locally.952. POST with Idempotency-Key header.963. On network error or 5xx: wait (exponential backoff + jitter), retry with SAME key.974. On 409 in-progress: wait Retry-After seconds, retry with SAME key.985. On 2xx: success.996. On 4xx (not 409): do NOT retry — fix the request.100```101102## Pitfalls103104- Using a server-generated key instead of a client-generated one — the client can't retry if the first response never arrived105- Setting a dedup window under 24 hours — not sufficient for mobile clients on spotty networks106- Scoping the key globally (not per-user) — one user can accidentally collide with another107- Not storing the full response body — on replay the server must return identical status + body, not regenerate108- Making `GET` idempotent with this mechanism — `GET` is already safe; only unsafe methods need it109110## See Also111112- [`../../agents/api-implementation-engineer.md`](../../agents/api-implementation-engineer.md) — HTTP semantics and unsafe-retry patterns113- [`../../agents/api-security-engineer.md`](../../agents/api-security-engineer.md) — resource-consumption limits (API4)114- [`../../CLAUDE.md`](../../CLAUDE.md) — house opinion: unsafe retries need an Idempotency-Key