The duplicate order problem
A buyer taps pay, the network drops the response, and the client retries. Without protection you create two orders and two charges for one intent.
Idempotency keys
- The client generates a unique idempotency key for the checkout attempt and sends it with every retry.
- The server stores the key with the result of the first successful creation.
- A retry with the same key returns the stored result instead of creating again.
Storage and expiry
- Keep an idempotency table mapping key to order id and response.
- Use a unique constraint on the key so a concurrent duplicate fails fast.
- Expire keys after the attempt window since the buyer will not retry forever.
Key idea
Attach an idempotency key to each order attempt and store the first result under a unique key, so any retry returns the same order instead of creating a duplicate.