Access & APIsUpdated 2026-11-20

Idempotency

How an idempotency key stops a retry from charging twice, where duplicates come from, and the open question in my POS fiscal design.

A receipt number stops you paying twice.

You hand over the money, the till jams, and you try again. If the shop knows your receipt number, it says "that one is already paid" instead of charging you a second time.

How it works

An operation is idempotent if repeating it gives the same intended result as running it once.

This matters most when the network lies. A client sends POST /payment. The connection times out. The client cannot know whether the payment went through, so it retries.

  • Without idempotency: payment 1 plus payment 2. A duplicate charge.
  • With an idempotency key: the client sends the same key on both attempts, for example Idempotency-Key: abc123. The backend sees the repeat and returns the original result instead of doing the work again.
Code
Attempt 1:  key abc123 → no record → charge → store {abc123 → result}
            (reply lost)
Attempt 2:  key abc123 → record found → return stored result, no charge

How to implement it

  1. Require a key from the client, or derive one deterministically from the request content.
  2. Store processed keys with their results for a bounded time window.
  3. On a repeated key, return the stored result and do not run the operation again.

The check and the write have to be atomic. If two copies of the same request arrive at once, a unique constraint on the key makes one of them lose. Checking first and inserting later is a race.

Where duplicates come from

Retries are exactly when duplicates happen. They come from:

  • client retries after a timeout
  • queue workers, which deliver at least once
  • webhooks, which senders resend until you answer 2xx
  • a user who double-clicks

That is why this idea sits next to queues, webhooks and every retry policy. Retrying without idempotency is how an outage becomes a billing incident.

Webhooks

For incoming webhooks the key is the event id. The pattern is to insert the event into an inbox table with a unique constraint on the provider and the event id. On conflict, you have already seen it. Return 2xx quickly and do the work in the background.

Calling someone else

For outgoing calls, send an idempotency key on every create request. If a provider has no idempotency support, the adapter has to query for the existing record before it posts again.

Where I used it

In my POS SaaS design, every fiscal retry carries an idempotency key. If the tax authority accepts an invoice and the reply is lost, the retry returns the original answer, so there is no second invoice. A second fiscal invoice for one real sale is a tax problem, not a bug report.

One part of this is still open, and I say so. If a provider has no idempotency support, the adapter has to query before it posts again. I have not settled that, because it depends on what a real provider does. POS is early-stage, so this is a design and not a production result.

Trade-offs

  • You need storage and a window. Keys and results must be kept, and you must choose how long. Too short and a late retry slips through. Too long and the table grows.
  • Same key, different body. Decide what happens when a key is reused with a different request. I would reject it with an error, because silently returning the old result hides a client bug.
  • It adds latency to the write path. The key lookup is a database round trip.
  • It does not fix partial failure. If step two of three fails, the key says "started", not "safe to skip". Design the steps so a rerun finishes the job.
  • Do not bother for reads. GET requests are naturally idempotent. Some writes can tolerate duplicates, for example a view counter that is allowed to be off a little.