Resilience4j is a fault tolerance library designed for Java8 and functional programming
Resilience4j is a fault tolerance library designed for Java8 and functional programming
= Fault tolerance library designed for functional programming :author: Robert Winkler and Bohdan Storozhuk :icons: :toc: macro :numbered: 1 ifdef::env-github[] :tip-caption: :bulb: :note-caption: :information_source: :important-caption: :heavy_exclamation_mark: :caution-caption: :fire: :warning-caption: :warning: endif::[]
image:https://github.com/resilience4j/resilience4j/actions/workflows/gradle-build.yml/badge.svg["Build Status"] image:https://img.shields.io/nexus/r/io.github.resilience4j/resilience4j-circuitbreaker?server=https%3A%2F%2Foss.sonatype.org["Release"] image:https://img.shields.io/nexus/s/io.github.resilience4j/resilience4j-circuitbreaker?server=https%3A%2F%2Foss.sonatype.org["Snapshot"] image:http://img.shields.io/badge/license-ASF2-blue.svg["Apache License 2", link="http://www.apache.org/licenses/LICENSE-2.0.txt"]
image:https://sonarcloud.io/api/project_badges/measure?project=resilience4j_resilience4j&metric=coverage["Coverage", link="https://sonarcloud.io/dashboard?id=resilience4j_resilience4j"] image:https://sonarcloud.io/api/project_badges/measure?project=resilience4j_resilience4j&metric=sqale_rating["Maintainability", link="https://sonarcloud.io/dashboard?id=resilience4j_resilience4j"] image:https://sonarcloud.io/api/project_badges/measure?project=resilience4j_resilience4j&metric=reliability_rating["Reliability", link="https://sonarcloud.io/dashboard?id=resilience4j_resilience4j"] image:https://sonarcloud.io/api/project_badges/measure?project=resilience4j_resilience4j&metric=security_rating["Security", link="https://sonarcloud.io/dashboard?id=resilience4j_resilience4j"] image:https://sonarcloud.io/api/project_badges/measure?project=resilience4j_resilience4j&metric=vulnerabilities["Vulnerabilities", link="https://sonarcloud.io/dashboard?id=resilience4j_resilience4j"] image:https://sonarcloud.io/api/project_badges/measure?project=resilience4j_resilience4j&metric=bugs["Bugs", link="https://sonarcloud.io/dashboard?id=resilience4j_resilience4j"]
toc::[]
== Introduction
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.
Resilience4j 3 requires Java 21.
// Create a CircuitBreaker with default configuration CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("backendService");
// Create a Retry with default configuration // 3 retry attempts and a fixed time interval between retries of 500ms Retry retry = Retry.ofDefaults("backendService");
// Create a Bulkhead with default configuration Bulkhead bulkhead = Bulkhead.ofDefaults("backendService");
Supplier 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 decoratedSupplier = Decorators.ofSupplier(supplier) .withCircuitBreaker(circuitBreaker) .withBulkhead(bulkhead) .withRetry(retry) .decorate();
// Execute the decorated supplier and recover from any exception String result = Try.ofSupplier(decoratedSupplier) .recover(throwable -> "Hello from Recovery").get();
// When you don't want to decorate your lambda expression, // but just execute it and protect the call by a CircuitBreaker. String result = circuitBreaker .executeSupplier(backendService::doSomething);
// You can also run the supplier asynchronously in a ThreadPoolBulkhead ThreadPoolBulkhead threadPoolBulkhead = ThreadPoolBulkhead .ofDefaults("backendService");
// The Scheduler is needed to schedule a timeout on a non-blocking CompletableFuture ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(3); TimeLimiter timeLimiter = TimeLimiter.of(Duration.ofSeconds(1));
CompletableFuture future = Decorators.ofSupplier(supplier) .withThreadPoolBulkhead(threadPoolBulkhead) .withTimeLimiter(timeLimiter, scheduler) .withCircuitBreaker(circuitBreaker) .withFallback(asList(TimeoutException.class, CallNotPermittedException.class, BulkheadFullException.class), throwable -> "Hello from Recovery") .get().toCompletableFuture();
NOTE: With Resilience4j you don’t have to go all-in, you can https://mvnrepository.com/artifact/io.github.resilience4j[*pick what you need*].
== Documentation
Setup and usage is described in our https://resilience4j.readme.io/docs[User Guide].
https://github.com/resilience4j-docs-ja/resilience4j-docs-ja[有志による日本語訳(非公式) Japanese translation by volunteers(Unofficial)]
https://github.com/lmhmhl/Resilience4j-Guides-Chinese[这是Resilience4j的非官方中文文档 Chinese translation by volunteers(Unofficial)]
== Overview
Resilience4j provides several core modules:
There are also add-on modules for metrics, Feign, Kotlin, Spring, Ratpack, Vertx, RxJava2 and more.
NOTE: Find out full list of modules in our https://resilience4j.readme.io/docs#section-modularization[User Guide].
TIP: For core modules package or +Decorators+ builder see https://mvnrepository.com/artifact/io.github.resilience4j/resilience4j-all[resilience4j-all].
== Best Practices
=== Instance Management: When to Share vs. Not Share Instances
One of the most important concepts for newcomers to understand is when to create separate instances versus when to share instances across different remote services or backends.
==== Why Unique Instances Matter
Creating separate instances for each backend service is critical for:
==== Instance-Aware vs. Non-Instance-Aware Patterns
Different resilience patterns have different requirements for instance separation:
===== Instance-Aware Patterns (MUST NOT Share)
These patterns maintain state that is specific to a particular service and MUST have separate instances:
// CORRECT: Separate CircuitBreaker for each service CircuitBreaker paymentServiceCB = CircuitBreaker.ofDefaults("paymentService"); CircuitBreaker inventoryServiceCB = CircuitBreaker.ofDefaults("inventoryService"); CircuitBreaker notificationServiceCB = CircuitBreaker.ofDefaults("notificationService");
// CORRECT: Separate Bulkhead for each service Bulkhead paymentBulkhead = Bulkhead.ofDefaults("paymentService"); Bulkhead inventoryBulkhead = Bulkhead.ofDefaults("inventoryService");
===== Non-Instance-Aware Patterns (CAN Share, But Better Not To)
These patterns create fresh context for each execution and don't maintain service-specific state:
// TECHNICALLY OK: Retry doesn't maintain state between calls Retry sharedRetry = Retry.ofDefaults("shared");
// BETTER: Unique instances provide better metrics and monitoring Retry paymentRetry = Retry.ofDefaults("paymentService"); Retry inventoryRetry = Retry.ofDefaults("inventoryService");
==== Recommended Approach: Always Use Unique Instances
Even for patterns that can be shared, creating unique instances is the recommended approach because:
===== Complete Example: Multiple Services
// You have 3 backend services to protect public class ServiceOrchestrator {
// Payment Service protection
private final CircuitBreaker paymentCB = CircuitBreaker.ofDefaults("paymentService");
private final Retry paymentRetry = Retry.ofDefaults("paymentService");
private final Bulkhead paymentBulkhead = Bulkhead.ofDefaults("paymentService");
// Inventory Service protection
private final CircuitBreaker inventoryCB = CircuitBreaker.ofDefaults("inventoryService");
private final Retry inventoryRetry = Retry.ofDefaults("inventoryService");
private final Bulkhead inventoryBulkhead = Bulkhead.ofDefaults("inventoryService");
// Notification Service protection
private final CircuitBreaker notificationCB = CircuitBreaker.ofDefaults("notificationService");
private final Retry notificationRetry = Retry.ofDefaults("notificationService");
public Order processOrder(OrderRequest request) {
// Each service call is protected by its own set of resilience instances
// Call payment service
Supplier<PaymentResult> paymentCall = () -> paymentService.charge(request);
PaymentResult payment = Decorators.ofSupplier(paymentCall)
.withCircuitBreaker(paymentCB)
.withRetry(paymentRetry)
.withBulkhead(paymentBulkhead)
.decorate()
.get();
// Call inventory service
Supplier<InventoryResult> inventoryCall = () -> inventoryService.reserve(request);
InventoryResult inventory = Decorators.ofSupplier(inventoryCall)
.withCircuitBreaker(inventoryCB)
.withRetry(inventoryRetry)
.withBulkhead(inventoryBulkhead)
.decorate()
.get();
// Call notification service (demonstrates circuit isolation: the notification circuit breaker remains independent of the payment circuit breaker, even if payment opens its circuit in other calls)
Runnable notificationCall = () -> notificationService.send(request);
Decorators.ofRunnable(notificationCall)
.withCircuitBreaker(notificationCB)
.withRetry(notificationRetry)
.decorate()CircuitBreaker: stale automatic state transitions after reset() and concurrent transitions, and HALF_OPEN timer self-interrupt
Retry code does not fire RetryOnRetryEvent when rety caused by predicate
Spring Boot 4: TimerRegistry is not refresh-scoped while the other five registries are
Documentation: CircuitBreakerConfig.Builder#maxWaitDurationInHalfOpenState javadoc still says HALF_OPEN waits for minimumNumberOfCalls (confirmed wrong in #1091 and #1869, never fixed)
SemaphoreBulkhead: expose the depth of the permission queue and allow bounding it
Kotlin: acquirePermissionSuspend() should await Bulkhead#acquirePermissionAsync() instead of blocking a Dispatchers.IO thread
RxJava 2/3 Bulkhead operators block the subscribing thread when maxWaitDuration > 0
circuitBreakers-info no longer show up under "/actuator/health" using Spring Boot 4.0.0-M2
InMemoryRegistryStore.putIfAbsent loses registrations when concurrent initialization fails
Adaptive capacity management (RateLimiter + BulkHead) decorator