Java Tutorial 0/145 lessons ~6 min read Lesson 74

    Resilience4j

    resilience4j resilience4j brings circuit breakers, retries, rate limiters and bulkheads to spring boot — preventing cascading failures when downstream services are slow

    Course progress0%
    Focus
    11 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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:

    ts
    @Service
    public 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.yml
    resilience4j.circuitbreaker.instances.payment.slidingWindowSize=10
    resilience4j.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

    1. Understand the core idea behind Resilience4j.
    2. Walk through the runnable example and tweak it in the playground.
    3. Apply the pattern in a small Spring Boot or CLI exercise of your own.
    4. Re-read the common mistakes and interview Q&A to lock the concept in.

    Interactive workflow diagram

    1Resilience4j — typical flow
    1 / 4

    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.
    Describe what problem it solves, the JDK APIs involved, and one production trade-off.
    Q2When would you avoid Resilience4j?
    Mention performance, complexity, or readability cases where a simpler approach wins.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.