# Failsafe — Fault Tolerance and Resilience Patterns for the JVM > Failsafe is a lightweight Java library that provides retry policies, circuit breakers, rate limiters, timeouts, and fallbacks to make applications resilient against transient failures. ## Install Save as a script file and run: # Failsafe — Fault Tolerance and Resilience Patterns for the JVM ## Quick Use ```xml dev.failsafe failsafe 3.4.0 ``` ```java RetryPolicy retryPolicy = RetryPolicy.builder() .handle(ConnectException.class) .withDelay(Duration.ofSeconds(1)) .withMaxRetries(3) .build(); String result = Failsafe.with(retryPolicy) .get(() -> httpClient.send(request)); ``` ## Introduction Failsafe provides composable resilience policies for Java applications. Instead of scattering try-catch-retry logic throughout a codebase, developers declare policies like retries, circuit breakers, and timeouts as first-class objects and compose them into execution pipelines. The library has zero external dependencies and works with any Java 8+ project. ## What Failsafe Does - Retries failed operations with configurable delays, backoff strategies, and jitter - Implements circuit breakers that trip after a failure threshold and reset after a cool-down period - Applies rate limiting, timeouts, and bulkhead isolation to protect downstream services - Composes multiple policies into a single execution pipeline with deterministic ordering - Supports synchronous and asynchronous execution including CompletableFuture integration ## Architecture Overview Failsafe models each resilience pattern as a Policy object. Policies are composed into an outer-to-inner chain: when you call `Failsafe.with(retry, circuitBreaker, timeout).get(supplier)`, the retry wraps the circuit breaker which wraps the timeout. Each policy intercepts the execution lifecycle — before, after, success, and failure — to decide whether to permit, retry, or short-circuit the call. Event listeners on each policy enable logging and metrics without coupling to a specific observability stack. ## Self-Hosting & Configuration - Add the `failsafe` Maven/Gradle dependency — no additional runtime services required - Build policies programmatically using fluent builder APIs (no XML or annotation configuration) - Compose policies via `Failsafe.with(policy1, policy2, ...)` in the order you want them applied - Register event listeners on policies for logging, metrics, or alerting - Use `FailsafeExecutor.getAsync()` for non-blocking execution on a custom executor or virtual threads ## Key Features - Zero dependencies — a single 100 KB JAR with no transitive pull - Composable policies that stack in a clear, predictable order - Built-in support for exponential backoff, jitter, and delay functions - Async-first design with CompletableFuture and virtual thread compatibility - Strongly typed — policies carry result and failure type parameters ## Comparison with Similar Tools - **Resilience4j** — Similar feature set with annotation-driven Spring integration; Failsafe favors programmatic composition with zero deps - **Spring Retry** — Tied to the Spring ecosystem; Failsafe works in any Java project - **Hystrix** — Netflix library now in maintenance mode; Failsafe is actively maintained with a lighter API - **Polly (.NET)** — .NET equivalent; Failsafe fills the same role for JVM applications - **MicroProfile Fault Tolerance** — Spec-based with CDI annotations; Failsafe offers a standalone programmatic approach ## FAQ **Q: Does Failsafe require Spring or Jakarta EE?** A: No. Failsafe is a standalone library with zero framework dependencies. It works in plain Java, Spring, Quarkus, or any JVM environment. **Q: How do I compose a retry with a circuit breaker?** A: Pass both policies to `Failsafe.with(retryPolicy, circuitBreaker)`. The retry wraps the circuit breaker, so retries happen around circuit-breaker-guarded calls. **Q: Does Failsafe support asynchronous execution?** A: Yes. Use `Failsafe.with(...).getAsync(supplier)` to return a CompletableFuture. You can also supply a custom ScheduledExecutorService. **Q: How does Failsafe handle rate limiting?** A: The RateLimiter policy supports smooth and bursty rate limiting. Calls that exceed the rate either block, fail fast, or wait for a configured duration. ## Sources - https://github.com/failsafe-lib/failsafe - https://failsafe.dev --- Source: https://tokrepo.com/en/workflows/asset-d6fd97df Author: Script Depot