Web apps & software

What is idempotency?

Idempotency means an operation can be repeated any number of times and the outcome is the same as doing it once. In APIs it is what stops a retried request from charging a card twice or creating two identical bookings.

Why retries make it necessary

Networks fail at awkward moments. A phone sends "create booking", the server saves it, and then the connection drops before the reply arrives. The app does not know whether it worked, so it retries. Without idempotency, the customer now has two bookings and possibly two charges.

How it is usually done

  • Idempotency keys: the client generates a unique ID for each logical action and sends it in a header. The server stores the key with the result; if the same key arrives again, it returns the stored result instead of acting again. Stripe's API popularized this approach.
  • Natural idempotency: "set status to cancelled" is safe to repeat; "add 10 to balance" is not, so design write operations as states rather than increments where possible.
  • Unique constraints: a database rule that rejects a second row with the same external reference.

HTTP methods

By definition GET, PUT and DELETE should be idempotent, while POST is not. That is a contract the developer must honour, not something the protocol enforces. Webhook receivers, payment flows and message-queue consumers are the places to check first, because they see duplicates most often.

Questions, answered

Long enough to cover realistic retry windows; many APIs keep them for about a day.

No. Any action with side effects, such as sending an SMS or creating an account, benefits.

By the Stegnor team · Updated