resilience4j

Resilience4j is a fault tolerance library for Java™

Resilience4j is a lightweight fault tolerance library inspired by Netflix Hystrix, but designed for functional programming. Lightweight, because the library only uses Vavr, which does not have any other external library dependencies. Netflix Hystrix, in contrast, has a compile dependency to Archaius which has many more external library dependencies such as Guava and Apache Commons Configuration.

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 = () -> backendService.doSomething(param1, param2);

Supplier<String> decoratedSupplier = Decorators.ofSupplier(supplier)
  .withRetry(Retry.ofDefaults("name"))
  .withCircuitBreaker(CircuitBreaker.ofDefaults("name"))
  .withBulkhead(Bulkhead.ofDefaults("name"));  

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

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

Get Started

Examples

Examples of resilience4j-rxjava2

Decorate Flowable with a CircuitBreaker

The following example shows how to decorate an Observable by using the custom RxJava operator. All other reactive types like Observable, Flowable, Single, Maybe and Completable are also supported.

The CircuitBreakerOperator checks if a downstream subscriber/observer can acquire a permission to subscribe to an upstream Publisher. If the CircuitBreaker is OPEN, the CircuitBreakerOperator emits a CallNotPermittedException to the downstream subscriber.

CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("name");
Flowable.fromCallable(backendService::doSomething)
    .compose(CircuitBreakerOperator.of(circuitBreaker))

Decorate Flowable with a RateLimiter

The following example shows how to decorate a Flowable by using the custom RxJava2 operator.
All other reactive types like Flowable, Single, Maybe and Completable are also supported.

The RateLimiterOperator checks if a downstream subscriber/observer can acquire a permission to subscribe to an upstream Publisher. If the rate limit is exceeded, the RateLimiterOperator emits a RequestNotPermitted to the downstream subscriber.

RateLimiter rateLimiter = RateLimiter.ofDefaults("name");
Flowable.fromCallable(backendService::doSomething)
    .compose(RateLimiterOperator.of(rateLimiter))

Decorate Flowable with a Bulkhead

The following example shows how to decorate a Flowable by using the custom RxJava2 operator.
All other reactive types like Flowable, Single, Maybe and Completable are also supported.

The BulkheadOperator checks if a downstream subscriber/observer can acquire a permission to subscribe to an upstream Publisher. If the Bulhead is full, the BulkheadOperator emits a BulkheadFullException to the downstream subscriber.

Bulkhead bulkhead = Bulkhead.ofDefaults("name");
Flowable.fromCallable(backendService::doSomething)
  .compose(BulkheadOperator.of(bulkhead));

Decorate Flowable with Retry

Retry retry = Retry.ofDefaults("backendName");
Flowable.fromCallable(backendService::doSomething)
    .compose(RetryOperator.of(retry))

Examples


Examples of resilience4j-rxjava2

Suggested Edits are limited on API Reference Pages

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