For years, retrying a flaky call in Spring meant adding spring-retry and spring-boot-starter-aop, then putting @EnableRetry on a config class. Some teams went further and pulled in Resilience4j just to retry one HTTP call.
Spring Framework 7, which Spring Boot 4 is built on, has retries in its core. The annotation is still called @Retryable, but it lives in a new package, takes different attributes, and needs no extra dependency. Next to it sits @ConcurrencyLimit, which caps how many threads can run a method at the same time.
This post sets both up on a small service and proves each behavior with a test. All five tests pass on Spring Boot 4.1.2. Two of them show mistakes that make the annotations silently do nothing.
The setup
The example is a service that reads exchange rates from a remote API through a RateClient:
public interface RateClient {
BigDecimal fetch(String currency);
}
The client throws RateUnavailableException when the remote API times out or returns a 503. Those errors are worth retrying. A bad currency code isn't.
The only dependency is spring-boot-starter, or any starter that includes it, like spring-boot-starter-webmvc.
Step 1: Turn it on
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.resilience.annotation.EnableResilientMethods;
@SpringBootApplication
@EnableResilientMethods
public class RatesApplication {
public static void main(String[] args) {
SpringApplication.run(RatesApplication.class, args);
}
}
Spring Boot doesn't do this for you. Without @EnableResilientMethods, the app still compiles and starts, and every @Retryable and @ConcurrencyLimit is ignored. I removed it from the demo to check, and the concurrency test hung forever because the second call was never rejected.
Step 2: Retry the flaky call
import java.math.BigDecimal;
import org.springframework.resilience.annotation.ConcurrencyLimit;
import org.springframework.resilience.annotation.Retryable;
import org.springframework.stereotype.Service;
@Service
public class RateService {
private final RateClient client;
public RateService(RateClient client) {
this.client = client;
}
@Retryable(includes = RateUnavailableException.class,
maxRetries = 3,
delayString = "${rates.retry.delay:500ms}",
multiplier = 2,
jitter = 100)
public BigDecimal rate(String currency) {
return client.fetch(currency);
}
@ConcurrencyLimit(value = 1, policy = ConcurrencyLimit.ThrottlePolicy.REJECT)
public BigDecimal refreshAll() {
return client.fetch("ALL");
}
}
Here's what each attribute on @Retryable does:
includes limits retries to the exceptions listed. Anything else fails right away. There's also excludes for the opposite.
maxRetries = 3 means up to 3 retries after the first call, so 4 calls in total.
delayString is the wait before the first retry. Using a property means tests can set it to 10ms instead of waiting seconds. Duration formats like 500ms and 2s work.
multiplier = 2 doubles the wait each time: 500ms, then 1s, then 2s.
jitter = 100 adds or removes up to 100ms at random, so a hundred instances don't all retry at the same moment and hit the API together.
If you need a cap on the growing delay, use maxDelay. The annotation also has timeout if all attempts together must finish within a set time.
Step 3: Prove it with tests
These tests use @MockitoBean to fake the client, so nothing goes over the network. The delay property is set to 10ms so the suite runs in about two seconds.
@SpringBootTest(properties = "rates.retry.delay=10ms")
class RateServiceTest {
@Autowired RateService service;
@MockitoBean RateClient client;
@Test
void retriesUntilTheCallWorks() {
when(client.fetch("EUR"))
.thenThrow(new RateUnavailableException("timeout"))
.thenThrow(new RateUnavailableException("timeout"))
.thenReturn(new BigDecimal("0.92"));
assertThat(service.rate("EUR")).isEqualByComparingTo("0.92");
verify(client, times(3)).fetch("EUR");
}
@Test
void givesUpAfterMaxRetries() {
when(client.fetch("EUR")).thenThrow(new RateUnavailableException("down"));
assertThatThrownBy(() -> service.rate("EUR"))
.isInstanceOf(RateUnavailableException.class)
.hasMessage("down");
verify(client, times(4)).fetch("EUR"); // 1 call + 3 retries
}
@Test
void doesNotRetryOtherErrors() {
when(client.fetch("XYZ")).thenThrow(new IllegalArgumentException("unknown currency"));
assertThatThrownBy(() -> service.rate("XYZ")).isInstanceOf(IllegalArgumentException.class);
verify(client, times(1)).fetch("XYZ");
}
}
The second test shows what the caller gets after the last retry: the original RateUnavailableException with its message. Your existing @ExceptionHandler methods keep working unchanged.
Step 4: Limit concurrent calls
refreshAll() from Step 2 reloads every rate, which is slow and expensive on the provider's side. Running two refreshes at once is a waste, so it has @ConcurrencyLimit(value = 1, policy = REJECT).
You can pick between two policies:
BLOCK (the default) makes extra callers wait until a slot frees up.
REJECT throws InvocationRejectedException right away.
The test holds the first call open with a latch, then makes a second call:
@Test
void rejectsASecondCallWhileOneIsRunning() throws Exception {
CountDownLatch started = new CountDownLatch(1);
CountDownLatch release = new CountDownLatch(1);
when(client.fetch(anyString())).thenAnswer(call -> {
started.countDown();
release.await();
return BigDecimal.ONE;
});
CompletableFuture<BigDecimal> first = CompletableFuture.supplyAsync(service::refreshAll);
started.await();
assertThatThrownBy(service::refreshAll).isInstanceOf(InvocationRejectedException.class);
release.countDown();
assertThat(first.get()).isEqualTo(BigDecimal.ONE);
}
With BLOCK, a limit is also a cheap way to protect a downstream service that falls over above a certain number of parallel requests. Set the limit to what it can handle and let the other threads queue.
The self-call trap
Both annotations work through a Spring proxy, so they only run when the call comes from outside the bean. This doesn't retry:
public class Caller {
public void outer() {
inner(); // same class: goes around the proxy
}
@Retryable(maxRetries = 3, delay = 10)
public void inner() {
client.fetch("EUR");
}
}
The demo has a test for this. With a client that always fails, calling outer() reaches the client exactly once. Put the retried method on a separate bean, like RateService here, and call it from outside.
When you need more control
When the retry decision depends on runtime data, use RetryTemplate from org.springframework.core.retry. It's the same machinery without the proxy, and it fits code that isn't a Spring bean.
Circuit breakers aren't part of Spring Framework 7. If a service is down for minutes, retries alone just add load to it, and that's where Resilience4j is still the usual choice.
Moving from spring-retry
If you already use spring-retry, the changes are small:
Remove the spring-retry dependency, and spring-boot-starter-aop if nothing else needs it.
Replace @EnableRetry with @EnableResilientMethods.
Change the import to org.springframework.resilience.annotation.Retryable.
Rewrite the attributes. maxAttempts = 4 becomes maxRetries = 3, since the new one counts retries only. backoff = @Backoff(delay = 500, multiplier = 2) becomes delay = 500, multiplier = 2 on the annotation itself. retryFor becomes includes.
Replace @recover methods with a try/catch at the caller. The new annotation has no recover hook, and as the test shows, the original exception reaches the caller.
Wrapping up
The whole retry setup is one annotation on the application class and one on the method. Check two things in every project: that @EnableResilientMethods is there, and that nothing calls a retried method from inside its own class. Both mistakes fail silently, and a test like the ones above catches them.
Retries are one of many things Boot 4 and Spring Framework 7 changed. I collected the rest in my Spring Boot 4 Cheat Sheet: 48 pages of code from one real app that compiles on Spring Boot 4.1, plus 55 interview answers, 22 common errors with their fixes, and the full source project with its tests.
It now comes with a free bonus, the Spring Boot Starter Kit (worth $29). It's one task-tracker app built three times, going from a plain REST API to JWT login, rate limiting, an outbox, metrics, tracing, Docker and Kubernetes. It has modules for Kafka, Redis, MongoDB, MySQL and Cassandra, plus a script that turns it into your own project. It runs on Spring Boot 4.1 and Java 25 with 220 passing tests, and you get it as a second download with the cheat sheet.
Both together are $9.99, with a 30-day refund if they don't help: Spring Boot 4 Cheat Sheet + Interview Guide.
What do you retry in production today, and with which library? Tell me in the comments.

Top comments (0)