The Developer's Guide to Safe Retries The Developer's Guide to Safe Retries

Idempotent API

Ensures identical requests produce the same result, enabling safe retries.

Overview

Idempotent Flow Idempotent Flow

Usage

Client Side

Include Idempotency-Key header with unique key per logical operation:

Key Format

Key rules:
  • Max 256 characters
  • Must be URL-safe
  • Client generates and manages

How It Works

1. Middleware Intercepts

2. Response Caching

Cached data:
  • HTTP status code
  • Response body
  • Headers (except sensitive ones)

3. TTL Expiration

After TTL, key is purged — safe for new operations.

Supported Operations

Error Handling

Missing Key (400)

Invalid Key Format (400)

Conflict (409)

If request is in progress with same key:

Configuration

When to Use

Use Cases

  • ✅ User registration
  • ✅ Payment processing
  • ✅ Order creation
  • ✅ Any state-changing operation

Don’t Use For

  • GET requests (already idempotent)
  • ❌ Search queries
  • ❌ Real-time data fetching

Testing

Best Practices

  1. Generate keys client-side — Never rely on server for key generation
  2. Use ULID for keys — Sortable + unique
  3. Set reasonable TTL — 24h is usually sufficient
  4. Include key in error responses — Helps with debugging
  5. Don’t use sequential keys — Use UUID/ULID to prevent collisions