Idempotency

Designing an operation so doing it more than once has the same effect as doing it once.

What is it?

Networks are unreliable. A request can time out even though the server processed it, leaving the client unsure whether to retry. If a retry could make the operation happen twice (charging a customer twice, sending a duplicate email), that's a real problem.

An operation is idempotent if doing it many times has the same effect as doing it once. That makes it safe to retry.

Explain like I'm 10

Pressing an elevator call button is idempotent: pressing it five times doesn't summon five elevators. Shouting 'add one more pizza to my order' five times is not; each shout is a new instruction, so you get five pizzas.

Examples

A non-idempotent request made safely idempotent

// Not idempotent: retrying this could charge the customer twice
// POST /payments  { amount: 50, userId: 123 }

// Made idempotent with a client-generated key
// POST /payments
// { amount: 50, userId: 123, idempotencyKey: "req-8f3a2b" }

// Server logic:
function handlePayment(request, alreadyProcessed, previousResult, processPayment) {
  if (alreadyProcessed(request.idempotencyKey)) {
    return previousResult(request.idempotencyKey); // don't redo the charge
  }
  return processPayment(request); // otherwise process normally, and remember this key's result
}

How it works

The client creates a unique key per logical operation, not per network attempt (retries reuse the same key), and sends it with the request.

The server remembers which keys it has processed and their results. If a key arrives again, it returns the stored result instead of repeating the effect.

Why does it exist?

If the response was lost, the client can't tell whether the request succeeded. Retrying risks doing it twice; not retrying risks leaving a real failure unresolved. Idempotency removes that dilemma by making retries harmless.

When to use it

Make an operation idempotent when duplicating it would cause real harm (payments, sending a notification, creating an order), especially if a client might retry after an unclear failure.

When not to use it

Some operations are already safe to repeat (checking a status, reading data, setting a value to a fixed state), so no extra key or tracking is needed. Save the idempotency-key machinery for operations that aren't naturally idempotent, like "charge" or "send".

Common mistakes

  • Assuming GET requests need idempotency protection. Reads are already idempotent, since reading twice changes nothing.

  • Generating a fresh idempotency key for every retry instead of reusing one key per logical operation, which defeats the purpose.

  • Never expiring stored idempotency keys, so storage grows without limit.

Practice exercises

  1. Easy:

    Explain in your own words why retrying a payment request without idempotency protection is risky.

  2. Medium:

    Give one example of an operation that's naturally idempotent and one that isn't, and explain the difference.

  3. Hard:

    Explain why the client, not the server, must generate the idempotency key once per logical attempt.

Interview questions

What does it mean for an operation to be idempotent?

Doing it many times has the same effect as doing it once, so it's safe to retry.

Why is idempotency especially important for payment APIs?

A client unsure whether a payment went through needs a safe way to retry without charging the customer twice.

Is a GET request typically idempotent?

Yes. Reading the same data repeatedly changes nothing, so GET is naturally idempotent with no extra design work.