Backend & Node.js · APIs, data and failure

What is idempotency and how do you implement it in an API?

Mediumidempotencyretriesapi designUpdated Oct 2026
Quiz this track

Short answer

An operation is idempotent when repeating it has the same effect as doing it once. GET, PUT and DELETE are defined as idempotent; POST is not, so create endpoints use an idempotency key to deduplicate retries.

Networks are unreliable. A client may send a request, lose the response and retry. If the server processed the first attempt, a naive retry can charge a card twice or create two orders. Idempotency makes the retry safe by recording the key and replaying the original result.

js
const key = req.get("Idempotency-Key");
const fingerprint = hashBody(req.body);
const existing = await idempotency.find(key);

if (existing) {
  if (existing.fingerprint !== fingerprint) {
    return res.status(422).json({ error: "key_reused_with_different_body" });
  }
  return res.status(existing.status).json(existing.body);
}

const result = await createOrder(req.body);
await idempotency.save(key, fingerprint, result);
res.status(201).json(result);
The key and the request fingerprint must match. Reusing a key with a different payload is a client error.

Implementation requirements

  • The key is scoped to a client or tenant, not global
  • The record and the business change should commit atomically or be recoverable
  • Store the response so retries return the same status and body
  • Handle concurrent attempts with a uniqueness constraint or lock, not a read-then-write race
  • Expire keys after a window longer than the client retry policy
  • Do not treat implicit retries in a proxy or queue as safe without this protection

PUT is naturally idempotent when it replaces the full resource with a known representation. PATCH may or may not be, depending on whether it applies a delta or sets a value. DELETE is idempotent by intent, though the response code can differ on a second call.

Idempotency is about effect, not about response identity. A second DELETE may return 404, but the system state is the same as after the first call.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1Which HTTP methods are idempotent and why?
  2. 2What happens when a key is reused with a different body?
  3. 3How do you handle two concurrent retries with the same key?
  4. 4Why is a database transaction important for idempotency?

Say it in the interview

Idempotency means repeating an operation does not change the result beyond the first execution. GET, PUT and DELETE are idempotent by definition; POST needs an idempotency key for safe retries. I store the key with a request fingerprint and the original response, and I enforce uniqueness so concurrent retries cannot both execute. Without that, a lost response can turn into a duplicate charge or order.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Backend & Node.js mock interview and get scored feedback on depth, clarity and confidence.