Idempotencyhard3-5 years

How would you design an Idempotency-Key mechanism for a POST /charges endpoint so a retried request can't double-charge a customer?

The client generates a unique key per logical operation (typically a UUID) and sends it on every attempt via an Idempotency-Key header. The server stores the key together with the response it produced; a request that arrives with a key already marked complete gets the stored response replayed, not a fresh computation — same chargeId, no second charge. A key reused with a different body is a client bug, not a retry, and should get a 409/422, not a silent replay, because replaying then would hide the mistake instead of catching it.

The lesson behind it →
More on Idempotency