Skip to content

How to make an operation idempotent

Use natural keys, idempotency tokens, upserts or state-based writes; never rely on "the client will not retry".

Making an operation idempotent

  1. Use a natural key

    PUT /users/42 is naturally idempotent. POST /users is not; generate the ID client-side or use an Idempotency-Key header.

  2. Use idempotency keys

    Stripe's approach: client sends Idempotency-Key: <uuid>. Server stores the response; a repeated key returns the cached response.

  3. Prefer upserts

    INSERT ... ON CONFLICT DO UPDATE (Postgres), MERGE (SQL standard), PUT in REST.

  4. Design the schema for it

    Unique constraints, version columns, state machines. Schema enforces idempotency that code alone cannot.