A timeout is not a failure - it is an unknown. The request may have succeeded after your client gave up. If that request was “charge Rp 500.000”, retrying it naively charges twice, and no changelog entry will calm that customer down.
The contract
The client generates a UUID per intent (not per attempt) and sends it on every retry of that intent:
POST /payments
Idempotency-Key: 7f9c2b1e-...The server treats the key as a lock and a memory:
INSERT INTO idempotency (key, status) VALUES ($1, 'processing')
ON CONFLICT (key) DO NOTHING;- Insert succeeded: do the work, store the response body against the key.
- Conflict + stored response: return the stored response, do nothing.
- Conflict + still processing: return 409 and let the client retry later.
Details that bite
- Scope keys per endpoint + user, or one user’s UUID collision becomes another’s payment.
- Expire keys (24-48h) or the table becomes your biggest one.
- Same discipline on webhook ingestion: providers redeliver, so
event_idis your dedupe key.
Payments are civil engineering with money: assume every component fails, then design so failure is boring.