Scrivio Interview library
CodingIntermediate6 min read · updated 12 November 2026

Using Resilience4j without annotations, decorate a Supplier<Price> that calls a pricing service with a circuit breaker and retry, and fall back to a cached price. Show the order you would choose.

We take the CircuitBreaker and Retry instances from CircuitBreakerRegistry and RetryRegistry, which the Boot starter auto-configures, and build Decorators.ofSupplier(supplier).withCircuitBreaker(cb).withRetry(retry).withFallback(List.of(CallNotPermittedException.class, ResourceAccessException.class), e -> cache.get(id)).get(). Each with call wraps everything before it, so Retry wraps the circuit breaker and every attempt is recorded by it, and the fallback is outermost and runs only for the listed exceptions. Doing this in code makes the order explicit and avoids proxy limitations such as self-invocation.

Why this question is asked

Annotations are convenient, but they hide two things: the order of the layers and whether the call goes through a proxy at all. The programmatic API puts both in plain sight, which is why many teams use it for their most important calls. Interviewers ask for it to see whether we can compose the layers deliberately and explain the result. In this answer, we will recap the pieces, build the decorated supplier step by step, read the order from the code, choose which exceptions reach the fallback, and see why this style avoids the proxy pitfalls.

1A Quick Recap of the Pieces

A Supplier<Price> is a function with no arguments that returns a Price, here a lambda that calls the pricing service. Resilience4j's core modules can wrap such a function: CircuitBreaker.decorateSupplier(cb, supplier) returns a new supplier that checks the breaker before calling the original and records the outcome afterwards, and Retry.decorateSupplier(retry, supplier) returns one that repeats the call on failure.

The Decorators class, from the resilience4j-all module, is a fluent builder that chains these wrappers in a readable way. The CircuitBreaker and Retry objects themselves come from registries. With the resilience4j-spring-boot3 starter, CircuitBreakerRegistry and RetryRegistry are Spring beans built from our YAML configuration, so an instance named pricing gets the settings under resilience4j.circuitbreaker.instances.pricing.

2Building the Decorated Call

Here is the whole service. The configuration for both instances lives in application.yml as usual.

Java
@Service
class PriceService {
private final PricingClient client;
private final PriceCache cache;
private final CircuitBreaker breaker;
private final Retry retry;
PriceService(PricingClient client, PriceCache cache,
CircuitBreakerRegistry breakers, RetryRegistry retries) {
this.client = client;
this.cache = cache;
this.breaker = breakers.circuitBreaker("pricing"); // settings from resilience4j.circuitbreaker.instances.pricing
this.retry = retries.retry("pricing"); // settings from resilience4j.retry.instances.pricing
}
Price price(String productId) {
Supplier<Price> call = () -> client.price(productId); // the real remote call
return Decorators.ofSupplier(call)
.withCircuitBreaker(breaker) // innermost wrapper
.withRetry(retry) // wraps the breaker
.withFallback(List.of(CallNotPermittedException.class, ResourceAccessException.class),
e -> cache.get(productId)) // outermost: only for these types
.get();
}
}
// pricing up: Price from the service
// timeout, then success: Price from the service (breaker recorded 1 failure, 1 success)
// breaker OPEN: cached Price, without calling pricing

Notice that the builder reads from the inside out. ofSupplier(call) is the core; withCircuitBreaker wraps it; withRetry wraps that; withFallback wraps everything. The final get() runs the whole chain once. If we want a reusable supplier instead, decorate() returns it.

3Reading the Order From the Code

Because each with call wraps the result of the previous ones, the order we chose is Fallback ( Retry ( CircuitBreaker ( call ) ) ). That gives three useful properties. Each retry attempt goes through the breaker, so the breaker records every attempt and opens promptly when pricing is failing. Once the breaker is open, the attempt fails with CallNotPermittedException without touching pricing. The fallback sees only the final outcome after all attempts, so it runs once.

Step throughA failing call through the decorated supplier
PriceServiceRetryCircuitBreakerPricing
get()
attempt 1
price(id)
timeout
failure recorded, rethrow
attempt 2 after backoff
OPEN: CallNotPermittedException
exception passed up (ignored by retry)
fallback: cache.get(id)

9 messages in order. Play them, or step through them one at a time.

The breaker opened after attempt 1 pushed it over its threshold; the fallback served the cached price.

One configuration detail completes the picture. The pricing retry should list CallNotPermittedException in ignore-exceptions, so it stops at once when the breaker opens instead of waiting through its remaining backoff. If we swapped the two calls, withRetry before withCircuitBreaker, the breaker would see one outcome per logical call after all retries, and would open more slowly.

4Choosing Which Exceptions Reach the Fallback

The fallback runs only for the exception types we list, and for their subclasses. Everything else propagates to the caller. This is deliberate: a cached price is a good answer when pricing is unreachable or the breaker is open, but not when our own code has a bug, such as an IllegalArgumentException from a bad product id. Hiding that behind a cached price would make the bug invisible.

The list must name what the supplier really throws. A Supplier cannot throw a checked IOException, so an I/O failure reaches us in an unchecked form. Spring's RestClient wraps it in ResourceAccessException, which is why that class is in the list rather than IOException. If our client throws UncheckedIOException or its own exception type instead, we list that. When the client really throws checked exceptions, Decorators.ofCheckedSupplier accepts a CheckedSupplier and a fallback can then name IOException directly.

What happens to each failure
FailureRetried?Result
ResourceAccessException (timeout, reset)yes, per retry configcached price if all attempts fail
CallNotPermittedException (breaker open)no, ignored by retrycached price at once
HttpClientErrorException (4xx)no, if ignored in configpropagates to the caller
IllegalArgumentException (our bug)depends on configpropagates: not in the fallback list

5Why the Programmatic Style Avoids Proxy Problems

With annotations, the protection exists only when the call passes through a Spring proxy, so a call from another method in the same class, a private method or a missing AOP starter silently removes it. With decorators, the protection is ordinary code inside the method. It runs whoever calls price(), including another method of the same class, and it works in classes that are not Spring beans at all.

The cost is a little more code, and we must reuse the CircuitBreaker and Retry instances from the registry rather than creating new ones per call, so that state and metrics accumulate in one place. Getting them in the constructor, as above, does exactly that.

6Follow-up questions

Why not create the CircuitBreaker with CircuitBreaker.ofDefaults() in the method?

Because each call would get a brand new breaker with an empty window, so it would never open, and its metrics would be lost. The breaker's value is its memory of recent calls, so it must be a long-lived instance from the registry.

How do you add a bulkhead or time limiter to this chain?

Add withBulkhead(bulkhead) close to the call, so it limits concurrent calls to pricing, usually before the circuit breaker in the chain. A time limiter needs an asynchronous call, so we would switch to Decorators.ofCompletionStage with a scheduler, or rely on the HTTP client's read timeout for this synchronous supplier.

Is the cached price always safe to return?

Only if it is recent enough and clearly a price we really charged. Many teams return the cached value with its age and refuse it beyond a limit, for example 15 minutes, rather than show an old price as current.

7Conclusion

In this answer, we decorated a Supplier<Price> in code with Resilience4j. We took the CircuitBreaker and Retry from the auto-configured registries, chained withCircuitBreaker, withRetry and withFallback so that retry wraps the breaker and every attempt is recorded, and listed only the exceptions that justify a cached price, naming the types the supplier really throws. The programmatic style makes the order explicit and works without proxies.

What to say in the interviewAbout 90 seconds aloud

Open with

I would inject the CircuitBreakerRegistry and RetryRegistry, which the Boot starter builds from the YAML, and get the pricing instances once in the constructor, so their state and metrics are shared.

Then, if they want more

Then in the method I wrap the call: Supplier of Price is a lambda calling the pricing client, and I build Decorators dot ofSupplier of that, with circuit breaker, then with retry, then with fallback, and call get. The order reads from the inside out, so it is fallback around retry around circuit breaker around the call.

I choose that because each retry attempt then goes through the breaker and is recorded, so the breaker opens promptly when pricing is failing, and once it is open, the attempt fails immediately with CallNotPermittedException without calling pricing. I configure the retry to ignore CallNotPermittedException, so it does not keep waiting.

The fallback gets an explicit list of exceptions, CallNotPermittedException and the I/O exception the client really throws, which for RestClient is ResourceAccessException rather than IOException, because a Supplier cannot throw checked exceptions. Anything else, like a bug in our code, propagates instead of being hidden behind a cached price.

The advantage over annotations is that there is no proxy involved, so self-invocation, private methods or a missing AOP starter cannot silently remove the protection, and the order is visible in code.

About this answer

Last updated
12 November 2026
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 "Using Resilience4j without annotations, decorate a Supplier<Price> that calls a pricing...": https://scrivio-interview-library.netlify.app/spring-boot/resilience/606a7f253c/