MohitDecodes
Back to blogsBackend

Rate Limiting & Idempotency Keys: Making Write APIs Safe

October 8, 2026 · 6 min read

Mobile networks drop responses all the time. If a client retries a `POST /orders` request because it timed out after the server already processed it, a naive backend creates two orders.

1. Idempotency keys for safe retries

Have the client generate a UUID (`Idempotency-Key`) per checkout attempt. The server stores the response keyed by `(user_id, idempotency_key)` — if the exact same key arrives again, return the saved result instead of charging twice.

2. Rate limiting to protect shared resources

  • Apply sliding-window or token-bucket limits per IP on public auth routes (`/login`, `/otp`).
  • Return `429 Too Many Requests` with a `Retry-After` header so legitimate clients know when to back off.
  • Keep limits in Redis when running multiple backend instances so counters are shared.