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 state | HTTP status | Server action | Header |
|---|---|---|---|
| First request (inserted row) | 201 Created | Execute handler and save serialized response | Location |
| Completed duplicate, same SHA-256 | Cached 201 Created | Return stored JSON without side effects | Idempotent-Replayed: true |
| Concurrent duplicate in progress | 409 Conflict | Abort duplicate and signal client backoff | Retry-After |
| Same key reused with different body | 422 Unprocessable Content | Block accidental parameter mutation | application/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.