CONCEPT SHEET · Concepts

Idempotency

One-liner: An operation is idempotent if doing it N times has the same effect as once; it is what makes retries and at-least-once delivery safe.

ArticleConcepts© Arthur Bernier Jr531 words1 code block

Say this#

How it works#

TEXT
client --POST /payments, Idempotency-Key: 7f3a--> serverserver: lookup key in Redis/DB   hit, in-progress  -> 409 or wait   hit, done         -> return stored response (same status/body)   miss              -> INSERT key (unique) ; do the work ; store response ; return
  1. Client generates a UUID per intent (same key on retries of the same action; new key for a new action).
  2. Server atomically claims the key (INSERT ... ON CONFLICT DO NOTHING or SETNX key value EX 86400).
  3. Executes the side effect once; persists result keyed by the idempotency key (ideally in the same transaction).
  4. Replays return the stored result. Keys expire after 24 h (Stripe) or a reasonable window.

Consumer-side (queues): processed_events(event_id PK) inserted in the same DB transaction as the business write; duplicate event → unique violation → ack and skip.

Trade-offs#

ChoiceProCon
Idempotency keysSafe client retries, exactly-once semantics for POSTStorage for keys/responses; key scoping (per user)
Dedupe in RedisFast, TTL built inNot transactional with the business write; risk if Redis loses data
Dedupe in DB (unique index)Atomic with the writeDB load; table growth needs TTL/partition pruning
Natural idempotency (upsert, CAS)No extra stateNot every operation can be expressed this way
Broker "exactly-once" (Kafka transactions)Handles Kafka→Kafka pipelinesDoesn't cover external side effects (email, payment)

Numbers#

  • Stripe idempotency keys are honored for 24 hours.
  • Dedupe window should exceed the maximum retry horizon (broker redelivery + client backoff), typically hours to a day.
  • SQS FIFO dedupe window: 5 minutes based on MessageDeduplicationId.

In the interview#

See also