Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

When is it safe to retry a failed HTTP call, and what retry policy would you use?

It is safe to retry an HTTP call when repeating it cannot cause a different outcome, which means idempotent operations such as GET, PUT and DELETE, or non-idempotent ones like POST protected by an idempotency key the server uses to detect duplicates. We retry only transient failures, such as connection errors, 503, 429 (respecting Retry-After) and some 502 or 504 responses, never 400-class validation errors. The policy I would use is a small number of attempts, typically three in total, with exponential backoff and jitter, all fitting inside the caller's deadline; Spring Framework 7 has built-in @Retryable support, and Spring Retry and Resilience4j are alternatives.

Why this question is asked

Retries are the most natural reaction to a failed call, and they do fix many short glitches, a dropped connection or a pod restarting during a deployment. They are also behind some of the worst incidents: double charges, duplicate orders and retry storms that turn a small slowdown into an outage. Interviewers ask this question because a good answer needs two separate judgements, whether repeating the operation is safe and whether the failure is likely to go away, plus a policy that does not make things worse. In this answer, we will explain idempotency, decide which failures are worth retrying, build a backoff policy with jitter, fit it inside a deadline, and see the options for implementing it in Spring.

1A Quick Recap of Idempotency

An operation is idempotent when doing it twice has the same effect on the server as doing it once. GET reads and changes nothing. PUT /users/7 with a full body sets the user to that state, so a second identical PUT leaves the same state. DELETE /users/7 removes the user, and deleting again leaves it removed (the second call may return 404, but the state is the same). POST /payments, on the other hand, usually creates a new payment each time, so two POSTs mean two charges. PATCH may or may not be idempotent depending on what it does: setting a field is, incrementing a counter is not.

The reason this matters is that a failed call does not tell us whether the server acted. If the connection drops after the request was sent, the server may have charged the card and the response was lost. Retrying a non-idempotent call in that situation can do the work twice.

2Making Non-Idempotent Calls Safe With an Idempotency Key

We can make a POST safe to retry by sending a unique idempotency key with it, usually a header such as Idempotency-Key, generated once by the client for this logical operation and reused on every retry. The server stores the key with the result. If the same key arrives again, it returns the stored result instead of acting again. Many payment providers support this, and there is an IETF draft standardising the header.

Java
String key = UUID.randomUUID().toString(); // one key per logical payment, kept across retries
PaymentResult r = payments.post().uri("/payments")
.header("Idempotency-Key", key)
.body(new PaymentRequest(orderId, amount))
.retrieve()
.body(PaymentResult.class);
// first attempt: connection reset after sending -> retry with the SAME key
// second attempt: 200 {"id":"pay_81","status":"captured"}, one charge only

Notice that the key is created before the first attempt and reused, not regenerated per attempt. A new key on each retry would make the server treat each one as a new payment, which is exactly what we wanted to avoid.

3Which Failures Are Worth Retrying

Even for a safe operation, a retry only helps if the failure is transient, meaning it is likely to be gone a moment later. A connection refused or reset often is: a pod restarting, a load balancer rotating. 503 Service Unavailable says the server is temporarily unable to serve. 429 Too Many Requests says we are being rate-limited; we can retry, but only after the time the server gives in its Retry-After header, otherwise we just get rate-limited again. 502 and 504 from a gateway can be transient, but a 504 means the upstream may have done the work, so it needs the same idempotency care as a timeout.

Errors in the 400 range other than 429, such as 400 Bad Request, 401, 403, 404 and 422, mean the request itself is wrong or not allowed. Sending the same request again will fail the same way, so retrying only adds load and delay.

Retry or not?
FailureRetry?Why
Connection refused or resetyesoften a restart or a rotating instance
503 Service Unavailableyes, with backoffserver says it is temporarily unavailable
429 Too Many Requestsyes, after Retry-Afterwe are rate-limited; waiting less fails again
502 / 504 from a gatewaysometimestransient, but the work may have happened
Read timeoutonly if idempotentthe server may have acted
400, 401, 403, 404, 422nothe same request fails the same way

4A Policy: Few Attempts, Exponential Backoff, Jitter

Retrying immediately rarely works, because whatever caused the failure is usually still there a millisecond later. Exponential backoff waits longer before each attempt, for example 200 ms, then 400 ms, then 800 ms, giving the dependency time to recover. Jitter adds randomness to each wait. Without it, a thousand clients that failed at the same moment would all retry at the same moment, hitting the recovering service with synchronised waves. With jitter, their retries spread out over time.

We also keep the number of attempts small, usually three in total, meaning one initial call and two retries. If a dependency fails three times in a row over a second or two, it is not a glitch, and more retries only add load to a service that is struggling.

Java
RetryConfig config = RetryConfig.custom() // Resilience4j
.maxAttempts(3) // 1 call + 2 retries
.intervalFunction(IntervalFunction.ofExponentialRandomBackoff(
Duration.ofMillis(200), 2.0, 0.5)) // ~200 ms, ~400 ms, +/- 50% jitter
.retryOnException(e -> e instanceof ResourceAccessException // connection problems
|| e instanceof HttpServerErrorException.ServiceUnavailable) // 503
.build();
Retry retry = Retry.of("inventory", config);
Stock s = retry.executeSupplier(() -> inventory.get().uri("/stock/{sku}", sku).retrieve().body(Stock.class));
// attempt 1: 503; wait 170 ms; attempt 2: 503; wait 430 ms; attempt 3: 200 -> Stock[available=4]

5Fitting Retries Inside the Deadline

Every attempt and every wait comes out of the time our caller is willing to wait. If the caller gives up after 2 seconds, three attempts with a 1 second timeout each, plus backoff, can never finish in time, and the last attempts are pure waste. So we size per-attempt timeouts and backoff together: with a 2 second budget, three attempts of 400 ms plus waits of about 200 and 400 ms add up to 1.8 seconds. When the remaining budget is too small for another attempt, we stop and return the error.

6Implementing Retries in Spring

Spring Framework 7 adds core retry support in the org.springframework.resilience package: a @Retryable annotation for methods, enabled with @EnableResilientMethods, and a programmatic RetryTemplate in the core retry package, with options for the number of retries, delay, multiplier, jitter and which exceptions to include. The annotation attribute names are new, so check your version. On earlier versions, the Spring Retry project offers its own @Retryable and RetryTemplate, and Resilience4j, shown above, adds retries along with circuit breakers, bulkheads and rate limiters, with Spring Boot integration and Micrometer metrics. For WebClient, Reactor's own retryWhen with Retry.backoff covers the reactive case.

7Follow-up questions

Should we retry on a read timeout?

Only if the operation is idempotent or carries an idempotency key. A read timeout means we stopped waiting, not that the server did nothing; it may have completed the work and been slow to answer. For a GET, retrying is fine. For a POST without a key, we should check the outcome instead, for example by querying the payment status.

Where should retries happen in a chain of services?

At one layer only, usually the one closest to the failing dependency or at the edge. If every layer retries three times, the attempts multiply, 3 x 3 x 3 = 27 for three layers, which turns a slowdown into an overload. Retry budgets and circuit breakers further limit the damage.

Is it safe to retry PUT?

Usually yes, when PUT replaces the resource with the full state in the body, because repeating it leaves the same state. If a service implements PUT with side effects, such as sending an email each time, it is not truly idempotent, and we should treat it like a POST.

8Conclusion

In this answer, we saw that a retry is safe only when repeating the call cannot change the outcome, which holds for idempotent operations and for POSTs protected by an idempotency key reused across attempts. We retry only transient failures such as connection errors, 503, 429 after Retry-After and some gateway errors, never 400-class validation errors. The policy is a few attempts with exponential backoff and jitter, all within the caller's deadline, implemented with Spring Framework 7's @Retryable, Spring Retry, Resilience4j or Reactor's retryWhen.

What to say in the interviewAbout 80 seconds aloud

Open with

I ask two questions before retrying. First, is it safe to repeat? A failed call does not tell me whether the server acted, so I only retry idempotent operations like GET, PUT or DELETE, or a POST that carries an idempotency key, which I generate once per logical operation and reuse on every attempt so the server can detect the duplicate.

Then, if they want more

Second, is the failure transient? Connection refused or reset, 503 and 429 are worth retrying, 429 only after the Retry-After time, and some 502 or 504 from gateways, though a 504 may mean the work happened. A 400, 401, 403, 404 or 422 will fail the same way again, so I never retry those.

For the policy I keep attempts small, usually three in total, with exponential backoff like 200 then 400 milliseconds and jitter, so clients that failed together do not retry in synchronised waves. And everything has to fit in the caller's deadline, so per-attempt timeouts are sized with the retries in mind.

I also retry at only one layer of a call chain, otherwise attempts multiply. In Spring, Framework 7 has @Retryable in org.springframework.resilience, I would check the version; otherwise Spring Retry or Resilience4j, and Reactor's retryWhen for WebClient.

About this answer

Last updated
12 November 2026
Versions it names
Spring Framework 7
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "When is it safe to retry a failed HTTP call, and what retry policy would you use?": https://scrivio-interview-library.netlify.app/spring-boot/http-clients/4c1f905e23/