Guides · integration testing
How to test API retries, backoff and recovery
An inventory lookup hits a rate limit. The client has to decide whether another attempt fits its deadline. Follow the request timing to see when the client should wait, retry or stop.
Watch the client wait or stop
Run the same read with two server delays. Advance the client’s test clock to see exactly when another request becomes eligible.
INVENTORY DETAILS
Trail water bottle
Slate · 24 oz
SKU DEMO-7- Warehouse
- North · Aisle 4
- Requested
- 1 unit
- Available
- Not confirmed
Send a read-only request to check current stock.
0 ms / 2,500 msRequest trace
0 requestsSend the inventory read to see the HTTP response and client scheduling decision.
This sample advances an in-page client clock and uses preset HTTP responses from the worked specification. It sends no network requests.
First decide whether to send another request
A status code alone is not a retry policy. Write down the operation, the failure classes your API contract treats as recoverable, the attempt limit and the total deadline.
Can this operation be repeated under the contract?
No or uncertain: stop or reconcile the result. Yes: classify the failure. A timeout after a write does not tell you whether that write happened.
Does this failure allow a retry?
Terminal: return the error or required corrective action. Retryable: check the remaining attempts, deadline and cancellation state.
When is the next attempt eligible?
Apply the client’s backoff and usable server delay. Define the fallback for an absent or malformed delay. If jitter is part of the policy, test its permitted interval rather than one exact timestamp.
Can the retry fit within the total deadline?
No: stop rather than retry sooner. Yes: wait, then recheck the bounds before dispatch. Cap each attempt timeout by the remaining operation time.
HTTP idempotency concerns the intended server effect of repetition; responses can still differ. Automatic retries of non-idempotent requests need known idempotent semantics or evidence the original request was not applied. RFC 9110 §9.2.2
If the API provides an idempotency mechanism, include its scope, lifetime and repeated-payload rules in the test. Check the server-side effect with a controlled fixture or safe test system. Two client log lines cannot establish that only one charge or order was created.
Test Retry-After as an input
Retry-After can contain an HTTP date or a nonnegative delay in seconds. A 429 response may include it; the header is optional. RFC 9110 §10.2.3 · RFC 6585 §4
Cover delay-seconds, a future date, a past date, a missing field and a malformed value. Give your harness a controlled wall clock for date parsing and a monotonic clock for the operation deadline. Specify how your client combines the parsed delay with backoff.
For a valid delay that extends past your deadline, assert a stopped operation. Do not shorten the server’s requested wait merely to squeeze in another attempt. Also count retries made by an SDK: nested retry loops can send more requests than the outer policy suggests.
Assert the stop as carefully as the recovery
Count outgoing requests, observe scheduled waits and check the caller’s final state. Choose the expected outcome for each case before running it.
| Case | Arrange | Assert |
|---|---|---|
| Recovery | A retryable failure, followed by the intended success | No early attempt; the correct result reaches the caller; no further retry remains scheduled |
| Exhaustion | Retryable failures continue through the configured bound | Count the initial request as an attempt; no request beyond the limit; one terminal failure with a useful reason |
| No unsafe duplicate action | A write is applied, but its response is lost | Follow the API’s recovery contract; observe the actual side-effect count, not just client request logs |
| Permanent error | A failure the API contract classifies as terminal | No automatic retry; the caller receives the documented error or required corrective action |
| Timeout or deadline | An attempt stalls, or the next permitted attempt falls outside the deadline | The operation ends within its total budget; no late retry starts; late completions cannot overwrite the terminal result |
A read recovers after one rate-limit response
An exact recovery case needs an ordered response fixture and control of the client scheduler. Keep the eligibility time, attempt bound and caller result in the same assertion. The full retry-behavior exercise contains the authored inputs, event timeline and failure variants.
Choose the smallest useful test environment
A stubbed transport and fake timers can cover exact response sequences and scheduler branches. An HTTP dependency environment adds real routing and response handling. Follow the HTTP setup and observation procedure when that boundary matters.
A stochastic chaos preset does not establish “attempt 1 fails, attempt 2 succeeds.” Advancing simulated API time does not advance app timers, SDK backoff or worker sleeps. Control the client scheduler separately and assert against the HTTP faults actually observed.
Inspect a saved API recovery sequence
The rate-limit recovery walkthrough contains two saved synthetic runs. Its API allows two requests, returns a 429, then accepts a request after an explicit API-time advance.
The example makes the clock boundary visible: advancing API time leaves the failed refresh on screen until a separate retry. Use it to inspect the dependency sequence; apply your own assertions to establish how your client behaves.
Common questions
- Should every 5xx response be retried?
- Choose retryable failures from the API contract and the operation’s semantics. A server error does not establish that repeating a write is safe.
- Do three retries mean three requests?
- Define the vocabulary in the test. This guide counts the initial request as an attempt, so a limit of three attempts allows at most two retries.
- What should I keep from a failed test?
- Keep the selected policy, API condition, request timeline, terminal reason and application assertion. Those observations distinguish an early retry, missing coverage and a recovery bug.