GuidesGitHubLog In


Resilience4j is a lightweight fault tolerance library designed for functional programming. Resilience4j provides higher-order functions (decorators) to enhance any functional interface, lambda expression or method reference with a Circuit Breaker, Rate Limiter, Retry or Bulkhead. You can stack more than one decorator on any functional interface, lambda expression or method reference. The advantage is that you have the choice to select the decorators you need and nothing else.

NOTE: Resilience4j 2 requires Java 17.

Sneak preview

The following example shows how to decorate a lambda expression with a CircuitBreaker and Retry in order to retry the call at most 3 times when an exception occurs.
You can configure the wait interval between retries and also configure a custom backoff algorithm.
The example uses Vavr’s Try monad to recover from an exception and invoke another lambda expression as a fallback, when all retries have failed.

// Create a CircuitBreaker with default configuration
CircuitBreaker circuitBreaker = CircuitBreaker

// Create a Retry with default configuration
// 3 retry attempts and a fixed time interval between retries of 500ms
Retry retry = Retry

// Create a Bulkhead with default configuration
Bulkhead bulkhead = Bulkhead

Supplier<String> supplier = () -> backendService
  .doSomething(param1, param2)

// Decorate your call to backendService.doSomething() 
// with a Bulkhead, CircuitBreaker and Retry
// **note: you will need the resilience4j-all dependency for this
Supplier<String> decoratedSupplier = Decorators.ofSupplier(supplier)

// When you don't want to decorate your lambda expression,
// but just execute it and protect the call by a CircuitBreaker.
String result = circuitBreaker

// You can also run the supplier asynchronously in a ThreadPoolBulkhead
 ThreadPoolBulkhead threadPoolBulkhead = ThreadPoolBulkhead

// The Scheduler is needed to schedule a timeout 
// on a non-blocking CompletableFuture
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);
TimeLimiter timeLimiter = TimeLimiter.of(Duration.ofSeconds(1));

CompletableFuture<String> future = Decorators.ofSupplier(supplier)
    .withTimeLimiter(timeLimiter, scheduledExecutorService)
                  throwable -> "Hello from Recovery")


With Resilience4j you don't have to go all-in, you can pick what you need.

Resilience4j provides several core modules and add-on modules:

All core modules and the Decorators class

  • resilience4j-all

Core modules

  • resilience4j-circuitbreaker: Circuit breaking
  • resilience4j-ratelimiter: Rate limiting
  • resilience4j-bulkhead: Bulkheading
  • resilience4j-retry: Automatic retrying (sync and async)
  • resilience4j-cache: Result caching
  • resilience4j-timelimiter: Timeout handling

Add-on modules

  • resilience4j-feign: Feign adapter
  • resilience4j-consumer: Circular Buffer Event consumer
  • resilience4j-kotlin: Kotlin coroutines support
  • resilience4j-vavr: Vavr support

Frameworks modules

  • resilience4j-spring-boot3: Spring Boot 3 Starter
  • resilience4j-spring-boot2: Spring Boot 2 Starter
  • resilience4j-micronaut: Micronaut Starter

Reactive modules

  • resilience4j-rxjava2: Custom RxJava2 operators
  • resilience4j-rxjava3: Custom RxJava3 operators
  • resilience4j-reactor: Custom Spring Reactor operators

Metrics modules

  • resilience4j-micrometer: Micrometer Metrics exporter
  • resilience4j-metrics: Dropwizard Metrics exporter

3rd party modules