# Resilience4j — Lightweight Fault Tolerance Library for Java > Resilience4j is a lightweight fault tolerance library for Java applications, providing circuit breaker, rate limiter, retry, bulkhead, and time limiter patterns with a functional programming model. ## Install Save as a script file and run: # Resilience4j — Lightweight Fault Tolerance Library for Java ## Quick Use ```java // Add dependency: io.github.resilience4j:resilience4j-circuitbreaker:2.2.0 CircuitBreakerConfig config = CircuitBreakerConfig.custom() .failureRateThreshold(50) .waitDurationInOpenState(Duration.ofSeconds(10)) .build(); CircuitBreaker breaker = CircuitBreaker.of("myService", config); Supplier decorated = CircuitBreaker .decorateSupplier(breaker, () -> callRemoteService()); Try.ofSupplier(decorated).recover(throwable -> "fallback"); ``` ## Introduction Resilience4j is a fault tolerance library designed for Java 8+ and functional programming. It is the recommended replacement for Netflix Hystrix, which entered maintenance mode. Resilience4j provides lightweight, composable decorators for circuit breaking, rate limiting, retrying, bulkheading, and time limiting, all built on top of Java functional interfaces. ## What Resilience4j Does - Implements the circuit breaker pattern to stop cascading failures when a downstream service is unhealthy - Provides rate limiting to control the number of calls to a service within a time window - Adds automatic retry with configurable backoff strategies for transient failures - Offers bulkhead isolation to limit concurrent calls and prevent resource exhaustion - Includes time limiter to cancel operations that exceed a configured duration ## Architecture Overview Resilience4j is organized as a set of independent modules, each implementing a single resilience pattern. Decorators wrap Java functional interfaces (Supplier, Function, Runnable) and can be composed together using the Decorators builder. Each module maintains its own state through an in-memory ring buffer or sliding window. Events from each component are published to an event stream that can be consumed for monitoring. The library integrates with Micrometer for metrics export and provides Spring Boot auto-configuration starters. ## Self-Hosting & Configuration - Add individual modules as Maven or Gradle dependencies — use only what you need - Configure through application.yml when using the Spring Boot starter for declarative setup - Define circuit breaker instances with failure rate thresholds, sliding window sizes, and wait durations - Use the `@CircuitBreaker`, `@RateLimit`, and `@Retry` annotations for Spring Boot declarative usage - Export metrics to Prometheus, Grafana, or any Micrometer-supported backend for observability ## Key Features - Composable decorators let you stack circuit breaker, retry, and rate limiter on a single call chain - Sliding window circuit breaker supports both count-based and time-based evaluation strategies - Spring Boot auto-configuration with YAML-based instance management and actuator health indicators - Built-in event consumers for logging, metrics, and custom reactions to state transitions - No external dependencies beyond Vavr for the core modules ## Comparison with Similar Tools - **Netflix Hystrix** — Thread-pool-based circuit breaker now in maintenance mode; Resilience4j is the recommended successor with lower overhead - **Sentinel (Alibaba)** — Flow control and circuit breaking with a dashboard, but heavier and designed primarily for the Alibaba ecosystem - **Polly (.NET)** — Equivalent fault tolerance library for .NET applications with a similar pattern-based approach - **Failsafe (Java)** — Lightweight alternative with a simpler API but fewer patterns and less ecosystem integration ## FAQ **Q: How is Resilience4j different from Hystrix?** A: Resilience4j uses functional composition instead of thread pools, has lower memory footprint, requires no external dependencies like Archaius, and supports Java 8+ lambdas natively. **Q: Can I use multiple patterns together?** A: Yes. Use the Decorators utility class to compose circuit breaker, retry, rate limiter, and bulkhead on a single supplier or function in a defined order. **Q: Does Resilience4j work with reactive frameworks?** A: Yes. Dedicated modules provide operators for Project Reactor (resilience4j-reactor) and RxJava (resilience4j-rxjava2/rxjava3). **Q: How do I monitor circuit breaker state?** A: Resilience4j integrates with Micrometer to export metrics. In Spring Boot, circuit breaker state is also exposed through the actuator health endpoint. ## Sources - https://github.com/resilience4j/resilience4j - https://resilience4j.readme.io/ --- Source: https://tokrepo.com/en/workflows/asset-f252f5ed Author: Script Depot