Guide

How to set a request-wide API retry budget

The PainHunt Team· Published · 5 min read

TL;DR: Set one deadline for the caller's operation, assign retry ownership and admit another attempt only when its wait and useful execution time fit. An attempt count alone does not bound total elapsed time, and a timeout alone does not make a write safe to repeat.

Define the operation the caller is waiting for

Start the budget when the application accepts the operation, not every time a helper function begins. Include connection setup, request transmission, response reading, required result processing and any time reserved to send the final answer.

Choose these values from the product's actual contract. An interactive lookup, an asynchronous export and a payment operation do not share a universal timeout. The one-second example below is deliberately synthetic; it is not a recommended service-level target.

Use a monotonic clock for elapsed-time decisions in a running process. A wall clock serves timestamps and HTTP-date interpretation, but changing that clock should not silently grant a longer local execution budget. Check how the runtime and client implement deadlines rather than assuming that a property named timeout covers every phase.

Put retries under one owner

List every layer that can repeat work: UI, route handler, service client, SDK, proxy and job runner. For each, record its retryable conditions, attempt count, wait policy and timeout scope.

In a toy call tree, an outer loop allowing three attempts and an inner client allowing three attempts per call can produce nine downstream attempts. That is simple composition, not a measurement of any provider. It shows why reducing only the outer count may leave unexpected load.

Amazon's Builders' Library discussion explains how retries can amplify load during failures and why backoff and jitter matter. For your implementation, select one layer to coordinate the operation's retry budget. Explicitly account for any retries that remain inside the client.

Do not remove useful transport recovery blindly. Verify the installed SDK's behavior and the operation's semantics, then configure the layers to cooperate with the same deadline.

Work through a fake-clock schedule

We executed a local scheduler fixture with a total budget of 1,000 milliseconds, three allowed attempts, a fixed 200-millisecond duration per attempt and waits of 100 then 200 milliseconds. Every attempt in this fixture fails in a retryable way. No real API or sleeping process is involved.

No. Start End Next step
1 0 ms 200 ms Wait 100 ms
2 300 ms 500 ms Wait 200 ms
3 700 ms 900 ms Stop at the attempt limit

The fixture completed three attempts in 900 milliseconds. It admitted an attempt only if the planned wait plus its 200-millisecond allowance fit the remaining total budget.

A second case supplied a 2,000-millisecond required wait after the first failure. Only one attempt ran: at 200 milliseconds, the wait and another useful attempt could not fit. The scheduler reported that the next attempt did not fit; it did not compress the wait to force another request.

These deterministic results test accounting. They do not model network variance, event-loop stalls, jitter or a remote server's completion. In a real client, cap each attempt by the remaining budget and reserve enough time for the final response. Recheck the deadline after waits and operations; elapsed work can differ from the plan.

Treat Retry-After as input to admission

RFC 9110's Retry-After section defines a delay expressed as either a non-negative number of seconds or an HTTP date. Parse the documented format carefully and convert units explicitly. An absolute date also requires a policy for clock differences.

For a retryable response under your contract, compare the permitted next-attempt time with the operation's remaining deadline. If the required wait does not fit, end the current attempt sequence. Returning an unavailable status and scheduling durable work are different product behaviors; do not pretend the latter happened unless a task was actually accepted.

Handle invalid or missing headers under a stated fallback policy. Do not infer that every response with this header, or every status code, grants permission to replay a consequential operation.

Preserve uncertainty about remote writes

An interrupted response leaves two questions: did the local attempt finish, and did the remote operation commit? They can have different answers.

Before retrying a write, use the provider's documented idempotency scope, retention and lookup behavior. A newly generated idempotency key for every attempt may create separate operations. A retained key is also insufficient if it is reused outside the API's supported contract or paired with conflicting parameters.

When the result remains unknown, retain a reference for reconciliation and present the appropriate pending or unavailable state. The LLM tool-call idempotency article discusses that particular integration scenario; the deadline arithmetic here applies to other callers too.

Record attempts without mistaking elapsed time for CPU

For controlled tests, capture an operation reference, attempt number, phase, planned wait, actual elapsed time, time remaining and stop reason. Avoid request bodies, authorization headers and raw connection strings in ordinary logs.

Wall-clock latency includes waiting. It does not measure active CPU time or the amount billed by a hosting platform. Use the platform's matching metric and billing window before claiming a retry change saved a particular CPU percentage.

Include late completion and cancellation tests. Ending a promise wait does not prove that a downstream query stopped. Coordinate client limits with the server's execution controls, and ensure a fallback does not start unlimited overlapping work. For an optional dependency, test its fallback contract alongside the shared request deadline.

Keep the fake-clock schedule as a regression fixture, then validate the adapter in a bounded test environment. A useful result names exactly why retries stopped: success, non-retryable failure, attempt limit, insufficient time or unresolved write outcome.

Frequently asked questions

Is an attempt timeout a total request timeout?

No. Several attempts, connection setup, backoff and result handling can exceed one attempt's timeout. Define an overall deadline and charge each phase against the remaining time, including retries inside an SDK.

What if Retry-After exceeds the remaining deadline?

Do not shorten the requested wait merely to fit another attempt into the same request. Stop that request's retries and return or defer under the product contract. A durable deferred task needs its own lifetime and deduplication rules.

Does a timeout make a write safe to repeat?

No. The server may have completed the operation before its response was lost. Use the API's documented idempotency and reconciliation behavior. If the outcome cannot be established safely, retain an unknown outcome instead of blindly replaying the write.

Research the problem behind your idea

Explore problem summaries and source links to compare workflows and decide what to investigate next.

Open the Pain Point Browser

Related articles