Resilience4j
resilience4j resilience4j brings circuit breakers, retries, rate limiters and bulkheads to spring boot — preventing cascading failures when downstream services are slow
Introduction
Resilience4j brings circuit breakers, retries, rate limiters and bulkheads to Spring Boot — preventing cascading failures when downstream services are slow or down.
Informative example
Circuit breaker + retry on a service call:
@Servicepublic class PaymentClient {@CircuitBreaker(name = "payment", fallbackMethod = "fallback")@Retry(name = "payment")@TimeLimiter(name = "payment")public CompletableFuture<Receipt> charge(Order order) {return CompletableFuture.supplyAsync(() -> rest.post("/charge", order));}private CompletableFuture<Receipt> fallback(Order o, Throwable t) {return CompletableFuture.completedFuture(Receipt.pending(o.id()));}}# application.ymlresilience4j.circuitbreaker.instances.payment.slidingWindowSize=10resilience4j.circuitbreaker.instances.payment.failureRateThreshold=50
Best practices
- Always provide a fallback — open circuit without fallback = errors.
- Tune thresholds from production metrics, not guesses.
- Combine circuit breaker + retry + timeout — not just one.
Purpose of this lesson
Master Resilience4j so you can apply it confidently in production Java code, technical interviews, and code reviews.
Step-by-step explanation
- Understand the core idea behind Resilience4j.
- Walk through the runnable example and tweak it in the playground.
- Apply the pattern in a small Spring Boot or CLI exercise of your own.
- Re-read the common mistakes and interview Q&A to lock the concept in.
Interactive workflow diagram
Identify use case
Recognize when resilience4j is the right tool for the problem.
Debugging tips
- Circuit stuck open? Check failureRateThreshold and waitDurationInOpenState.
- Fallback not called? Method signature must match: (args..., Throwable).
Optimization strategies
- Profile before optimizing — JFR (Java Flight Recorder) and async-profiler reveal real hotspots.
- Prefer immutable data and stream pipelines over hand-rolled loops when readability matters.
- Reach for the right JDK collection (ArrayList vs LinkedList vs ArrayDeque) before writing custom data structures.
Enterprise example
Teams at Netflix, Uber and Goldman Sachs apply Resilience4j daily — usually wrapped behind Spring Boot services with observability hooks (Micrometer + OpenTelemetry).
Interview questions & answers
Q1Explain Resilience4j in one minute.
Q2When would you avoid Resilience4j?
Summary
In this lesson you learned Resilience4j — the concept, syntax, a runnable example, and the production pitfalls to avoid. Apply it in the playground before moving on.