Implementing Idempotency-Key in POST APIs

Safe POST retries using payload hashing and database uniqueness under ADP-012.

Idempotency state transitions

Enforce deterministic outcomes across first attempts, concurrent races, and replays.

Request stateHTTP statusServer actionHeader
First request (inserted row)201 CreatedExecute handler and save serialized responseLocation
Completed duplicate, same SHA-256Cached 201 CreatedReturn stored JSON without side effectsIdempotent-Replayed: true
Concurrent duplicate in progress409 ConflictAbort duplicate and signal client backoffRetry-After
Same key reused with different body422 Unprocessable ContentBlock accidental parameter mutationapplication/problem+json

Atomic insert guard in PostgreSQL

INSERT INTO idempotency_records (tenant_id, idempotency_key, request_hash, state, expires_at)
VALUES ($1, $2, $3, 'in_progress', NOW() + INTERVAL '24 hours')
ON CONFLICT (tenant_id, idempotency_key) DO NOTHING;

Check POST operations in OpenAPI

Rule ADP-012 verifies every mutation route declares an Idempotency-Key header parameter in the OpenAPI Validator.