Scripts2026年7月19日·1 分钟阅读

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.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Failsafe Overview
直接安装命令
npx -y tokrepo@latest install d6fd97df-83b1-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产