Scrivio Interview library
CodingIntermediate6 min read · updated 12 November 2026

Configure a Resilience4j retry in Spring Boot for a call to an inventory service: 3 attempts, exponential backoff starting at 200 ms, retry only on IOException and HTTP 503, and never on 4xx.

We configure a Resilience4j instance named inventory with max-attempts 3 (which includes the first call), wait-duration 200ms, enable-exponential-backoff true and a multiplier of 2, so the waits are 200 ms and then 400 ms. retry-exceptions lists the I/O exception our client actually throws and the 503 exception, and ignore-exceptions lists the 4xx exception type, then we put @Retry(name = "inventory") on a public method of a Spring bean. Because @Retry works through a proxy, calls from inside the same class are not retried.

Why this question is asked

Configuring a retry is a few lines of YAML, but each line hides a decision that interviewers like to probe: does 3 mean three retries or three calls, which exceptions does the client really throw, and why does the annotation sometimes do nothing at all. Getting these wrong gives either a retry that never fires or one that retries a 400 three times. In this answer, we will recap how Resilience4j retry works, write the configuration line by line, check which exceptions Spring's HTTP clients throw, apply the annotation, and look at the new retry support in Spring Framework 7.

1A Quick Recap of Resilience4j Retry

Resilience4j is a lightweight fault tolerance library. Its Retry module wraps a call and, when the call throws an exception that the configuration says is retryable, waits and calls it again, up to a limit. In Spring Boot we add the resilience4j-spring-boot3 starter (check your version for the Boot 4 module name) and the AOP starter, and then each named instance is configured under resilience4j.retry.instances.

Two terms matter. An attempt is one call, including the first one, so max-attempts: 3 means the first call plus up to two retries. The wait duration is the pause before each retry; with exponential backoff it is multiplied after every attempt.

2The Configuration, Line by Line

Here is the full configuration for the inventory instance in application.yml.

Java
resilience4j:
retry:
instances:
inventory:
max-attempts: 3 # 1 call + 2 retries
wait-duration: 200ms # first wait
enable-exponential-backoff: true
exponential-backoff-multiplier: 2 # waits: 200 ms, then 400 ms
exponential-max-wait-duration: 2s # ceiling for longer policies
enable-randomized-wait: true # jitter; see the note below
randomized-wait-factor: 0.5
retry-exceptions:
- org.springframework.web.client.ResourceAccessException
- java.io.IOException
- org.springframework.web.client.HttpServerErrorException$ServiceUnavailable
ignore-exceptions:
- org.springframework.web.client.HttpClientErrorException
Step throughOne call through the inventory retry
  1. Attempt 1call inventory
  2. Exception thrown?
    YesCheck its typeNoReturn the result
  3. 4xx (HttpClientErrorException)?
    YesThrow at once, no retryNoCheck the retry list
  4. I/O error or 503?
    YesWait 200 ms, then 400 msNoThrow at once
  5. Attempts left (max 3)?
    YesCall againNoThrow the last exception

5 steps in order. Play them, or step through them one at a time.

With three attempts there are only two waits: 200 ms before the second call and 400 ms before the third. If the third call also fails, the exception is thrown to our code. Randomised wait adds jitter around each value, so many instances of our service do not retry in lockstep. Older Resilience4j versions refused to combine exponential backoff with randomised wait, while recent 2.x versions combine them into exponential random backoff, so check your version.

When retry-exceptions is set, only those exceptions (and their subclasses) are retried, and every other exception fails at once. ignore-exceptions makes the rule explicit for 4xx: an ignored exception is never retried, even if it happened to match another rule.

3Which Exception Does Our Client Really Throw?

This is the step that most often breaks a retry. Resilience4j matches exception classes, not their causes. Spring's RestClient and RestTemplate do not let a raw IOException escape. They wrap it in ResourceAccessException, which is a runtime exception. So if we list only java.io.IOException, a connection reset arrives as ResourceAccessException and is never retried. That is why the configuration lists ResourceAccessException as well; IOException stays in the list for clients that throw it directly.

For HTTP status codes, Spring throws HttpClientErrorException for 4xx and HttpServerErrorException for 5xx, and each has nested subclasses per status, such as HttpServerErrorException.ServiceUnavailable for 503. In YAML a nested class is written with a dollar sign. Listing only ServiceUnavailable means a 500 Internal Server Error is not retried, which is usually what we want, because a 500 is often a bug, not a passing condition.

Java
RestClient client = builder.baseUrl("http://inventory").build();
client.get().uri("/stock/{sku}", sku).retrieve().body(Stock.class);
// connection refused -> ResourceAccessException (cause: java.net.ConnectException)
// 503 -> HttpServerErrorException.ServiceUnavailable
// 404 -> HttpClientErrorException.NotFound

If we use WebClient instead, the types differ: I/O errors come as WebClientRequestException and status errors as WebClientResponseException, so the lists must name those. When matching by class is not enough, Resilience4j also accepts a retry-exception-predicate, a class implementing Predicate<Throwable>, where we can inspect the cause or the status code ourselves.

4Applying @Retry and Why Self-Invocation Is Not Covered

We apply the instance by name on a method of a Spring bean.

Java
@Service
class InventoryClient {
private final RestClient client;
InventoryClient(RestClient.Builder builder) { this.client = builder.baseUrl("http://inventory").build(); }
@Retry(name = "inventory")
public Stock stock(String sku) {
return client.get().uri("/stock/{sku}", sku).retrieve().body(Stock.class);
}
public List<Stock> stockFor(List<String> skus) {
return skus.stream().map(this::stock).toList(); // this.stock(): no retry, the proxy is skipped
}
}

The annotation works through an aspect. Spring creates a proxy, a wrapper object around our bean, and the retry logic runs only when a call enters through that proxy. A call from another bean goes through the proxy and is retried. A call to this.stock() from inside the same class goes straight to the real object, so it is not retried, and private methods are never intercepted. If we need retries for internal calls, we move the method into a separate bean or use the Retry object programmatically.

5Spring Framework 7's Built-In Retry

Spring Framework 7 adds core retry support, so simple cases no longer need Resilience4j. We enable it with @EnableResilientMethods on a configuration class and annotate methods with @Retryable. Its attributes count retries, not attempts, so our policy is maxRetries = 2. Package names and attribute names are new, so check your version.

Java
@Retryable(includes = {ResourceAccessException.class, HttpServerErrorException.ServiceUnavailable.class},
maxRetries = 2, delay = 200, multiplier = 2, jitter = 50) // 3 attempts: 200 ms, then 400 ms
public Stock stock(String sku) { ... }

Resilience4j remains the better choice when we also want circuit breakers, bulkheads and per-instance metrics with consistent names, while the framework's version is attractive for a single retry with no extra dependency. Before Framework 7, the separate Spring Retry project offered a different @Retryable with similar ideas.

6Follow-up questions

Does max-attempts: 3 mean three retries?

No. It is the total number of calls, including the first one. So max-attempts: 3 gives one call and up to two retries, with two waits. In Spring Framework 7's @Retryable, the attribute is maxRetries, which counts only the retries, so the same policy is maxRetries = 2.

How do you test that the retry works?

With WireMock or a MockRestServiceServer, we make the inventory endpoint return 503 twice and 200 the third time, call the bean through the Spring context, and assert that the result arrived and that three requests were made. A second test returns 404 and asserts only one request. Resilience4j also publishes retry events and Micrometer counters that we can check.

What happens if the inventory call is not idempotent?

Then we should not retry it on timeouts unless the request carries an idempotency key. Retrying a stock reservation blindly could reserve stock twice. For read-only stock lookups, as in this example, retries are safe.

7Conclusion

In this answer, we configured a Resilience4j retry for the inventory service: three attempts in total, a 200 ms first wait doubling to 400 ms, optional jitter, retries only for I/O errors and 503, and 4xx ignored. We saw that Spring's clients wrap IOException in ResourceAccessException, so the retry list must name the exceptions the client really throws. @Retry works through a proxy, so self-invocation is not covered, and Spring Framework 7 offers a built-in @Retryable for simpler needs.

What to say in the interviewAbout 80 seconds aloud

Open with

I would configure a named instance, inventory, under resilience4j.retry.instances. max-attempts is 3, which counts the first call, so it means two retries. wait-duration is 200ms with enable-exponential-backoff true and a multiplier of 2, so the waits are 200 and then 400 milliseconds, and I would add randomised wait for jitter if the version supports combining them.

Then, if they want more

For the exceptions, retry-exceptions lists the I/O error and HttpServerErrorException dollar ServiceUnavailable for 503, and ignore-exceptions lists HttpClientErrorException so 4xx is never retried. One trap here: RestClient and RestTemplate wrap IOException in ResourceAccessException, and Resilience4j matches the class, not the cause, so I list ResourceAccessException, otherwise connection failures are never retried. With WebClient the type is WebClientRequestException.

Then I put @Retry with name inventory on a public method of a Spring bean. Because it is applied by an aspect through a proxy, a call from another method in the same class, or to a private method, is not retried; I would move the call into its own bean if needed.

I would test it with WireMock returning 503 twice and then 200, and a 404 case that must be called only once. And I would mention that Spring Framework 7 has its own @Retryable with @EnableResilientMethods, where maxRetries counts only retries.

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 "Configure a Resilience4j retry in Spring Boot for a call to an inventory service: 3...": https://scrivio-interview-library.netlify.app/spring-boot/resilience/17bc64cc93/