Video in progress
Why does the retry never fire in this Resilience4j configuration?
@Retry(name = "pricing")
@CircuitBreaker(name = "pricing", fallbackMethod = "cachedPrice")
public Price getPrice(String sku) {
return pricingClient.price(sku);
}
private Price cachedPrice(String sku, Throwable t) {
return priceCache.get(sku);
}
In the default order, the Retry aspect wraps the CircuitBreaker. Since the CircuitBreaker has a fallback, the error is turned into a normal value inside, so Retry sees a successful response and doesn't retry.
📖 Answer
Resilience4j in Spring Boot applies the aspects in a set order. By default Retry is outermost, then CircuitBreaker inside it, then RateLimiter, TimeLimiter and Bulkhead:
Retry( CircuitBreaker( RateLimiter( TimeLimiter( Bulkhead( method ) ) ) ) )
The order in which the annotations are written in the code doesn't matter; the aspect order configuration decides.
Now trace the flow: pricingClient.price() fails with a timeout → the CircuitBreaker aspect records the error and calls the fallbackMethod → the cached price comes back → as far as the Retry aspect is concerned, the method finished successfully. Retry never fires, while you expect to see "3 attempts" in the log.
Ways to fix it:
- Move the fallback to the outermost layer,
@Retry. Then the retries go first, and the cache comes last.
@Retry(name = "pricing", fallbackMethod = "cachedPrice")
@CircuitBreaker(name = "pricing")
public Price getPrice(String sku) { ... }
- Add
CallNotPermittedExceptionto the Retry'signore-exceptions. If the breaker is open, retrying makes no sense; these are wasted attempts that never even reach the network.
It's easy to catch such mistakes with a test: in WireMock make the first two responses 503 and the third 200, and check that the service returned the real price.
Sign up to mark studied questions and save your progress.