Now inviting automation leaders: publish your playbook as a skill, under your own name. Publish yours →
Automation Skills
All skills/Api idempotency retry/Key-lifecycle-walkthrough
Reference · Api idempotency retry

Key-lifecycle-walkthrough

Part of Api idempotency retry. Install the skill and your agent reads this page when it needs the depth.

Idempotency key lifecycle - worked walkthrough

A complete pass through one key's life, from first claim to reaping, using the default-rung mechanism (DB unique constraint) and the patterns from the Postgres reference implementation (Brandur Leach, brandur.org/idempotency-keys). No vendor publishes their production internals, so treat this as an independent, Stripe-inspired reference design, not Stripe's disclosed code.

Storage schema

Relational-flavored for concreteness; every column maps to a concept any store needs.

CREATE TABLE idempotency_keys (
  id              BIGSERIAL PRIMARY KEY,
  account_id      BIGINT NOT NULL,          -- per-caller scope, never global
  idempotency_key TEXT NOT NULL,            -- client-minted, e.g. up to 255 chars (Stripe's cap)
  request_fingerprint TEXT NOT NULL,        -- payload checksum for the mismatch guard
  locked_at       TIMESTAMPTZ,              -- non-NULL = in-flight; drives lock recovery
  recovery_point  TEXT NOT NULL DEFAULT 'started',  -- resumable position for multi-step requests
  response_status INT,                      -- stored outcome: both success AND failure
  response_body   JSONB,                    -- replayed verbatim on completed retry
  created_at      TIMESTAMPTZ NOT NULL DEFAULT now(),
  UNIQUE (account_id, idempotency_key)      -- the atomic claim: the DB picks one winner
);

The atomic claim

Never SELECT then INSERT - under concurrent retries both callers see "absent" and both proceed (TOCTOU). Insert first and let the constraint arbitrate:

INSERT INTO idempotency_keys (account_id, idempotency_key, request_fingerprint, locked_at)
VALUES ($1, $2, $3, now())
ON CONFLICT (account_id, idempotency_key) DO NOTHING
RETURNING id;

Branching on an existing key - the three states

State: complete (response_status set) - first compare request_fingerprint.

HTTP/1.1 409 Conflict
{ "code": "invalid_idempotent_request",
  "message": "This idempotency key was already used with a different request payload. Use a new key, or resend the original payload." }

(Resend's documented code name; fix advice belongs in the message - see samber/developer-platform-skills@api-error-design for message rules.)

State: in-flight (locked_at recent, no stored response) - the original is still running. Default handling, per the field convention:

HTTP/1.1 409 Conflict
{ "code": "concurrent_idempotent_requests",
  "message": "A request with this idempotency key is currently in progress. Wait and retry." }

Never let the second caller through because the first "seems stuck" - the alternatives (block for the bounded result; 202 + status URL for long-running effects) also keep exactly one executor. The expired IETF draft made this deliberate: in-flight conflict and completed replay are different responses, not fallbacks.

State: in-flight but lock expired (locked_at older than the lock timeout) - the original crashed or was abandoned. This caller may take over the lock (an atomic conditional UPDATE ... SET locked_at = now() WHERE locked_at < $threshold) and resume from recovery_point - not from the beginning.

Multi-step requests: recovery points around foreign state mutations

A request that calls an external, non-rollbackable system mid-flight (a card charge, an outbound email) cannot be one transaction. Decompose into atomic phases separated by each foreign call:

Phase 1 (txn): claim key, validate, create local order row     → recovery_point = 'order_created'
  -- foreign state mutation: charge the card --
Phase 2 (txn): record charge id                                 → recovery_point = 'charge_recorded'
  -- foreign state mutation: send confirmation email --
Phase 3 (txn): store final response                             → recovery_point = 'finished'

Retention: two horizons, then reaping

The two background jobs

Recovery is these active processes, not passive expiry - expiry alone leaves half-finished requests half-finished forever.

Client-side pairing

The key only works if the client reuses it across retries of one intent. For critical calls, write the intent to a durable queue with a pending status before the first attempt, so the retry state - key included - survives an application restart.

Derivation, in the recommended order:

The anti-pattern that defeats everything: Date.now() or a fresh crypto.randomUUID() per attempt.

Publish your skill

Copy this prompt into Claude Code, Codex or any agent. It walks you through your first skill in about 20 minutes, with you as the author.

Click to copy.