Rara’s order is retried after its reply is lost on the road: how many times does the gudang book it, with and without an Idempotency-Key? Then a double click…
Why it matters
Networks do fail, and most often after the server has finished its work: only the reply gets lost. With no key, an automatic retry or a panicked double click makes a double order and a double charge. That is why payment APIs (Stripe, and an IETF draft for HTTP) use an Idempotency-Key on every POST that changes something.
The key idea
The network can fail AFTER the server has done its work
Common mistakes
An Idempotency-Key is set, yet every try still makes a new order The key is made anew on every try (crypto.randomUUID() inside the loop), so the gudang sees a new request every time. Make the key once per order, before the first try, then send exactly that key on every try.
Thinking a timeout means it failed, so it is safe to resend A timeout only means the reply did not arrive; the server may already have booked it. POST is not idempotent: resending it makes a new order. Send an Idempotency-Key so the server decides whether this is a retry.
The second order vanishes, replaced by the first one’s reply Two different orders used the same key. A server that does not compare bodies takes the second for a retry and answers the first reply again: the second order is never booked. The IETF draft has the server compare fingerprints and refuse with 422; on the code’s side: one key for one order.
Clicking twice while the first order is still being booked With the same key, the server answers the second click with 409 Conflict while the first is not done; with no key, two orders. Make one key per order, not per click, and disable the button until the answer comes.
Try it in this playground
Send with no key, one reply lost Two orders in the book; Rara pays twice.
Set an Idempotency-Key, one per order The second try gets the first reply again; still one order.
Make a new key on every try The gudang sees a new order every time.
Click twice with the same key The second click gets 409 Conflict.