Skip to content
Library/Core Concepts
API design

Idempotency

1 min read

Ensure repeated requests produce the same result — critical for payment and write-heavy APIs.

Ensure repeated requests produce the same result. Use idempotency keys for any write operation involving money or state changes.

How It Works

An idempotent operation produces the same result regardless of how many times it is executed. POST is not naturally idempotent. The standard pattern: client generates a unique idempotency key (UUID), sends it in a header, server stores the key with the response. On retry, server returns the stored response. Store keys in Redis with a TTL (24-48 hours).

Real-World Example

Stripe requires an Idempotency-Key header on all POST requests. When a charge request arrives, Stripe checks Redis for the key. If found, it returns the cached result. This prevents double-charging when a client retries due to network timeout.

Test Yourself

A payment API sometimes gets retried due to network timeouts. How do you prevent double-charging?

Get notified when we launch

One email when the full practice product is live. No spam.