Pattern: Retries, idempotency keys and busy answers
Note for AI agents: This page is baseline guidance, not an exact prescription. Read it, tell your owner what applies to their project, and get their approval before you change anything. Your project's own rules take precedence. If something important for this project is missing here, suggest it to your owner.
For agents
- Offer as a suggestion. Values are examples.
- Core ideas: every write carries a unique request key so a repeat does nothing new; clients retry with growing, randomised waits and a cap; the server says “busy” with 429 or 503 and a retry hint, never 500.
- Related: rate limits, server layers, file upload.
1. Problem
Networks drop responses. A phone that did not get an answer cannot know whether the server did the work. Naive retries create duplicate orders or messages; naive servers answer overload with 500, and naive clients retry in lockstep and make the overload worse.
2. Options
| Topic | Option | Upside | Downside |
|---|---|---|---|
| Duplicates | Hope retries are rare | No work | Duplicate charges, posts |
| Duplicates | Idempotency key per write (recommended) | Repeats are safe | Server stores keys for a while |
| Retry timing | Immediate retry | Fast when lucky | Retry storms |
| Retry timing | Exponential back-off with jitter (recommended) | Spreads load; AWS documents it | Slower in the worst case |
| Busy answer | 500 | None | Looks like a bug; clients cannot tell what to do |
| Busy answer | 429 or 503 with Retry-After (recommended) |
Clients know to wait | Needs limit and capacity logic |
3. Recommended design
- Idempotency key on every write. The client creates a unique key (a random id) per intended action and sends it in a header such as
Idempotency-Key(an IETF HTTPAPI draft defines this header). The server stores key and result for a period (example: 24 hours); a repeated key returns the stored result and does nothing else. Keys that arrive with a different request body are rejected. Natural idempotency (HTTPPUT,DELETE) helps, butPOSTneeds a key (MDN on idempotent methods; RFC 9110). - Client retries. Retry only idempotent or keyed requests, and only on network errors, 429 and 503. Wait with exponential back-off plus random jitter, cap the wait (example: 30 seconds) and the attempts (example: 5). AWS’s article on exponential back-off and jitter explains why the randomness matters. The AWS Well-Architected guidance on limiting retries adds the cap.
- Obey the server. If
Retry-Afteris present, wait at least that long (MDN, RFC 9110). - Busy is not broken. When over capacity answer 429 (the client is too fast) or 503 (the service is overloaded) with
Retry-After, never 500. Google’s SRE material on cascading failures describes why limiting retries and shedding load protect the backend. - Server-side repeat safety also needs database guards: a unique constraint or conditional write, so even a missing key cannot create duplicates (server layers).
- Show progress honestly: the app shows “retrying” rather than failing at once, and offers a manual retry after the cap.
- Uploads: completion messages are idempotent too (file upload).
4. Scenarios and edge cases
- Response lost after the server succeeded: the retry with the same key returns the stored result.
- Two taps on “send” in a second: the key makes the second one a no-op.
- Server overloaded: requests receive 503 with
Retry-After; clients spread out thanks to jitter. - User changes the body but reuses a key: rejected with a clear error.
- Key store unavailable: fail closed for payments and similar actions; for harmless writes decide explicitly.
5. Common mistakes
- Retrying non-idempotent requests blindly.
- Fixed retry intervals without jitter.
- Retrying forever.
- Answering overload with 500.
- Ignoring
Retry-After. - Generating a new key on each retry.
6. How to audit
- What: repeating a write with the same key creates one result.
- How to check: send the same request with the same key twice.
- Evidence: one new row; identical responses.
- What: the client back-off has jitter and a cap.
- How to check: read the retry code; simulate failures and log waits.
- Evidence: varying wait times that grow and stop growing.
- What: busy answers are 429 or 503 with a hint.
- How to check: exceed capacity in a test.
- Evidence: status and
Retry-Afterheader.
- What: key reuse with another body is rejected.
- How to check: send a different body with an old key.
- Evidence: the error response.
Sources
- The Idempotency-Key HTTP Header Field (Internet-Draft) — IETF
- Well-Architected: make mutating operations idempotent — AWS
- REL05-BP03 Control and limit retry calls — AWS
- Exponential Backoff and Jitter — AWS Architecture Blog
- Retry-After header — MDN Web Docs
- RFC 9110: HTTP Semantics — IETF
- Addressing Cascading Failures (Site Reliability Engineering book) — Google SRE
- Idempotent (glossary) — MDN Web Docs