A retried request is a normal part of how networks behave: a response gets lost, the client retries, the payment provider sends a notification again. If the API is not ready for that, every retry becomes a duplicate: a second order, a second charge, a second email to the customer. Idempotency is the property that makes a retry safe.

Idempotency in plain terms.

An operation is idempotent if repeating it gives the same result as the first call. You can read an order as many times as you like and nothing changes. Charging a card twice is a different result.

Under the HTTP specification (RFC 9110), GET, PUT and DELETE are idempotent, while POST and PATCH are not: every POST may create a new record. Orders and payments are usually created with POST.

An important caveat

A method’s idempotency is a promise made by the specification, not a guarantee. Whether a particular handler keeps it depends on the code: a PUT that appends to a payment log on every call is not idempotent.

Where retries come from.

Duplicates rarely come from a bug on a single line. More often a retry is the normal behaviour of one of the parties:

01 / SIGNAL

Client timeout

The server completed the operation but the response never arrived. The client sees a network error and retries, so the operation runs twice.

02 / SIGNAL

Repeated notification

A payment provider or another service resends a webhook until it gets a successful response. A slow handler receives the same event several times.

03 / SIGNAL

Double action

A user clicked “Pay” twice, refreshed the page after submitting a form, or opened it in two tabs.

  • Automatic retries in HTTP clients, SDKs, and proxies: easy to enable and forget that they retry POST requests too.
  • Message queues that guarantee “at least once” delivery: the same message can arrive twice.

The key point

A network error does not tell you whether the operation happened. Retrying without idempotency is guesswork.

The idempotency key.

The client creates a unique key for one logical operation and sends it in a header, usually Idempotency-Key. On the first request the server performs the operation and stores the result under that key; on a retry with the same key it returns the stored response without doing anything again. Stripe’s API works this way, and YooKassa uses the same mechanism with an Idempotence-Key header. The Idempotency-Key header is specified in the IETF draft draft-ietf-httpapi-idempotency-key-header.

01 /

One key per operation, not per attempt

The key is created before the first attempt and reused for every retry. A new key per attempt protects nothing.

02 /

Stored together with the operation

The key and the result are written in the same transaction as the operation itself. Written separately, a failure between the two writes reopens the door to a duplicate.

03 /

Concurrent retries

While the first request with a key is still running, a retry with the same key must not start the operation again: it is rejected or asked to retry later.

04 /

Payload check and retention

The same key with a different request body is a client error, not a retry. Keys are kept for as long as clients may retry.

Unique constraints are the last line of defence.

Even with idempotency keys, protect the data at the database level. A unique constraint on an external identifier — the provider’s payment ID, or the order number plus operation type — prevents a duplicate row whatever happens above it.

  • “Look up, then insert” does not help. Two concurrent requests both find nothing and both insert. Only a constraint in the database itself protects you.
  • Insert with conflict handling. In PostgreSQL, INSERT … ON CONFLICT DO NOTHING, in MySQL, INSERT … ON DUPLICATE KEY UPDATE. A retry does not fail; it simply detects that the operation already happened.
  • States instead of flags. A payment moves through allowed transitions: created → paid → refunded. A second “paid” for a paid payment changes nothing.

Incoming notifications: process each once.

A webhook is a notification an external service sends to your API: a payment succeeded, an order was delivered, a status changed. Almost every service resends a webhook when it does not get a successful response, so the handler must expect repeats.

  • Verify the notification’s signature or source: without it anyone can send “payment succeeded”.
  • Store the event ID and skip events you have already processed.
  • Respond quickly and do the work in the background through a queue. A slow handler looks like a failed delivery and gets retried.
  • Do not rely on ordering: notifications arrive out of sequence. Take the current status from the service’s API.
  • If a notification never arrives, the status is polled on a schedule. Webhooks speed things up but do not replace reconciliation.

Queues: “at least once” is the norm.

Most message brokers guarantee at-least-once delivery. In practice, “exactly once” comes not from broker settings but from an idempotent consumer: it remembers which messages it has processed and skips repeats.

The opposite problem is losing an event. If a service changes data and publishes an event as two separate steps, a failure between them causes a mismatch. The transactional outbox pattern writes the event in the same transaction as the data change, and a separate process delivers it to the queue.

What to bring to the first conversation.

You do not need a diagnosis. Describe where duplicates appear — payments, orders, emails, records in a business system — how often, and after what: timeouts, deployments, outages of an external service.

  • Which external services are involved, and do they support idempotency keys?
  • Who retries requests, and how: the client, an SDK, a proxy, the service itself?
  • Is there a log of requests and notifications in which a duplicate can be traced?