Resilience4j is a fault tolerance library for Java™

Resilience4j is a lightweight fault tolerance library inspired by Netflix Hystrix, but 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.

Supplier<String> supplier = () -> service.sayHelloWorld(param1);

String result = Decorators.ofSupplier(supplier)
  .withFallback(asList(CallNotPermittedException.class, BulkheadFullException.class),  
      throwable -> "Hello from fallback")

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

Get Started


Examples of resilience4j-circuitbreaker

Create a CircuitBreakerRegistry

Create a CircuitBreakerRegistry with a custom CircuitBreakerConfig.

// Create a custom configuration for a CircuitBreaker
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
    .recordExceptions(IOException.class, TimeoutException.class)
    .ignoreExceptions(BusinessException.class, OtherBusinessException.class)

// Create a CircuitBreakerRegistry with a custom global configuration
CircuitBreakerRegistry circuitBreakerRegistry =

Create a CircuitBreaker

Get a CircuitBreaker from the CircuitBreakerRegistry with the global default configuration

CircuitBreaker circuitBreaker = circuitBreakerRegistry

Decorate a functional interface

Decorate your call to BackendService.doSomething() with a CircuitBreaker and execute the decorated supplier and recover from any exception.

Supplier<String> decoratedSupplier = CircuitBreaker
    .decorateSupplier(circuitBreaker, backendService::doSomething);

String result = Try.ofSupplier(decoratedSupplier)
    .recover(throwable -> "Hello from Recovery").get();

Execute a decorated functional interface

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

String result = circuitBreaker

Recover from an exception

If you want to recover from an exception after the CircuitBreaker recorded it as a failure, you can chain the method Try.recover(). The recovery method is only invoked, if Try.of() returns a Failure<Throwable> Monad.

// Given
CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName");

// When I decorate my function and invoke the decorated function
CheckedFunction0<String> checkedSupplier =
  CircuitBreaker.decorateCheckedSupplier(circuitBreaker, () -> {
    throw new RuntimeException("BAM!");
Try<String> result = Try.of(checkedSupplier)
        .recover(throwable -> "Hello Recovery");

// Then the function should be a success, 
// because the exception could be recovered
// and the result must match the result of the recovery function.
assertThat(result.get()).isEqualTo("Hello Recovery");

If you want to recover from an exception before the CircuitBreaker records it as a failure, you can do the following:

Supplier<String> supplier = () -> {
            throw new RuntimeException("BAM!");

Supplier<String> supplierWithRecovery = SupplierUtils
  .recover(supplier, (exception) -> "Hello Recovery");

String result = circuitBreaker.executeSupplier(supplierWithRecovery);

assertThat(result).isEqualTo("Hello Recovery");

SupplierUtils and CallableUtils contain other methods like andThen which can take can be used to chain functions. For example to check the status code of a HTTP response, so that exceptions can be thrown.

Supplier<String> supplierWithResultAndExceptionHandler = SupplierUtils
  .andThen(supplier, (result, exception) -> "Hello Recovery");

Supplier<HttpResponse> supplier = () -> httpClient.doRemoteCall();
Supplier<HttpResponse> supplierWithResultHandling = SupplierUtils.andThen(supplier, result -> {
    if (result.getStatusCode() == 400) {
       throw new ClientException();
    } else if (result.getStatusCode() == 500) {
       throw new ServerException();
    return result;
HttpResponse httpResponse = circuitBreaker

Reset CircuitBreaker

The Circuit Breaker supports resetting to its original state, losing all the metrics and effectively resetting its Sliding Window.

CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName");

Transition to states manually

CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName");
// circuitBreaker.onFailure(...) won't trigger a state change
circuitBreaker.transitionToClosedState(); // will transition to CLOSED state and re-enable normal behaviour, keeping metrics
// circuitBreaker.onSuccess(...) won't trigger a state change
circuitBreaker.reset(); //  will transition to CLOSED state and re-enable normal behaviour, losing metrics

Override the RegistryStore

You can override the im-memory RegistryStore by a custom implementation. For example, if you want to use a Cache which removes unused instances after a certain period of time.

CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.custom()
  .withRegistryStore(new CacheCircuitBreakerRegistryStore())


Examples of resilience4j-circuitbreaker

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.