Architecture & BFF

Idempotency in APIs

Direct Definition

Idempotency means that performing the same API operation multiple times has the same effect as performing it once. Idempotent endpoints let clients and gateways safely retry requests after timeouts without creating duplicate orders, charges, or records.

Technical Architecture & Deep-Dive

Networks fail unpredictably. A mobile client may send a payment request, lose connectivity before receiving the response, and retry. Without idempotency, the customer could be charged twice.

HTTP defines GET, PUT, and DELETE as idempotent by convention, while POST is not. APIs like Stripe accept an `Idempotency-Key` header so the server stores the first result and returns it for any duplicate request with the same key. Gateways and webhook handlers should apply the same principle when retrying upstream calls.

Key Architectural Benefits

Makes automatic retries safe after network timeouts.
Prevents duplicate charges, orders, and database rows.
Simplifies webhook processing when providers redeliver events.

Idempotent Payment Request

typescript
await fetch('https://api.stripe.com/v1/payment_intents', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${env.STRIPE_SECRET_KEY}`,
    'Idempotency-Key': orderId,
  },
  body: new URLSearchParams({ amount: '2000', currency: 'usd' }),
});
REALCOMPOSER EDGE WORKSPACE

Implement Idempotency in APIs at the Edge

Deploy lightweight API aggregation, edge caching, and secret injection in minutes without Docker containers or complex servers.

START BUILDING FREE

Frequently Asked Questions

Q:Is POST ever idempotent?

Not by default, but APIs can make POST idempotent by requiring a client-generated idempotency key and deduplicating requests that reuse it.

Related Architecture Concepts

Ready-to-Use Edge Recipes