📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

How do you make a POST endpoint safe to retry when the operation is not naturally idempotent, such as taking a payment?

Junior Theory Beginner

🎯 Key points

  • Client-generated unique key per logical operation, reused on every retry of it
  • Keys scoped per client/API key to prevent cross-client replay
  • Store key + request hash + response atomically with the side effect (unique constraint)
  • Replay stored response on repeat; same key, different body = 422; in-flight duplicate = 409
  • Expire keys after a documented window via TTL or cleanup job
  • Log and monitor key reuse and mismatches

⏱ 30-second answer

Have the client send a unique Idempotency-Key header per logical operation (one checkout attempt, not one HTTP attempt), and scope keys per client or API key so one caller can't replay another's. The server stores the key with a hash of the request and the response, atomically with the side effect (same transaction, or a unique constraint on the key). A repeat with the same key and body returns the stored response; the same key with a different body returns 422; a concurrent duplicate gets 409 until the first completes. Keys expire after a documented window such as 24 hours, enforced with a TTL (Redis) or a cleanup job (SQL). Log key reuse and mismatches so replay attempts and buggy clients show up in monitoring.

🔍 What the interviewer is checking

Checks whether they know idempotency keys and how to store and replay responses

↩ Usually asked after

More in HTTP Fundamentals

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me