# What is an idempotency key?

> An idempotency key is a unique id that the client chooses and sends with a request, so a retry of the same operation can be recognized and not applied twice. The server stores the key together with the result, and answers a repeated key with the stored result instead of acting again.

- Author: Zeeshan Adil (https://www.internalswithzee.com/about)
- Published: 2026-11-12
- Source: https://www.internalswithzee.com/concepts/idempotency-key

Also called: idempotency token, request id.

## The problem it solves

A request times out. The client does not know whether the server never got it, did the work and lost the reply, or is still working. Giving up risks losing the action. Sending it again risks doing it twice, which for a payment or a text message is a real harm. The client cannot tell the cases apart, so it needs a way to retry that is safe in all of them.

## How it works

The client generates a unique key when it first decides to do the operation, and reuses the same key on every retry of that operation.

```
POST /charges   Idempotency-Key: 7f3a...   -> server applies it, stores key + result
POST /charges   Idempotency-Key: 7f3a...   -> server finds the key, returns the stored result
```

Rules that make it work:

- **Generate once per operation, not once per attempt.** A new key on each retry defeats the point.
- **Store the key atomically with the effect.** Use a unique constraint in the same transaction as the write. Checking first and writing second leaves a gap that two concurrent retries can both slip through.
- **Keep keys long enough.** A key only protects retries made while it is remembered. Providers keep them for a limited time.

## What it does not guarantee

It makes retries safe, not the first attempt succeed. It does not help if the server ignores the key. It also says nothing about two different operations: reusing a key for a different request should be rejected, not silently answered with the old result.

## Measured

These numbers come from my own runs. In the [sharding challenge](/challenges/distributed-systems/where-does-each-comment-live), 100 writes whose reply was lost after the commit were retried. With the same request id on the retry, 100 rows were stored and 0 were duplicates. With a retry that started over without one, 200 rows were stored and 100 were duplicates.

In the [one message per action challenge](/challenges/distributed-systems/one-message-per-action), a local run against a fake provider that injected timeouts and duplicate webhooks sent 2,000 actions through 200 random SIGKILLs and a database restart. All 2,000 had exactly one effect in the provider's own log. 60 needed a second attempt, and each one reused the original key after a lookup.

## Related

- Challenge to run: https://www.internalswithzee.com/challenges/distributed-systems/one-message-per-action
- Article: https://www.internalswithzee.com/blog/resilient-jobs-unreliable-apis
- Article: https://www.internalswithzee.com/blog/message-queue-patterns
- Related concept: https://www.internalswithzee.com/concepts/transactional-outbox
- Related concept: https://www.internalswithzee.com/concepts/at-least-once-delivery

## Sources

- [Stripe API docs: Idempotent requests](https://docs.stripe.com/api/idempotent_requests)

