resilience4cata

Resilience annotations for plain Java 21+, with no Spring, CDI, AspectJ or agents. Annotate a method with @Retry, @CircuitBreaker, @Timeout… and an annotation processor generates plain Java at compile time. No proxies, no reflection: a few nanoseconds per call and no allocation on the sync success path.
- Policies: retry, hedge, circuit breaker, rate limiter, adaptive limit, timeout, bulkhead, fallback.
- Call styles: sync,
CompletionStage, Reactor Mono/Flux; on interfaces or on annotated classes.
- Configuration: named configs, overrides from properties, environment or system properties.
- Also: events, snapshots, Micrometer/OpenTelemetry metrics, a programmatic
Pipeline, a testkit with a manual clock, Spring Boot, Micronaut and Quarkus integrations.
Status: 1.2.0 on Maven Central, stable since 1.0.0: within 1.x the API, the generated code and the configuration stay compatible (compatibility). Docs: resilience4cata.dev (English, Spanish, Portuguese and Chinese).
Install
The library plus the annotation processor (required; only on the processor path).
Gradle:
dependencies {
implementation 'dev.resilience4cata:resilience4cata-all:1.2.0'
annotationProcessor 'dev.resilience4cata:resilience4cata-processor:1.2.0'
}
Maven:
<dependency>
<groupId>dev.resilience4cata</groupId>
<artifactId>resilience4cata-all</artifactId>
<version>1.2.0</version>
</dependency>
<annotationProcessorPaths>
<path>
<groupId>dev.resilience4cata</groupId>
<artifactId>resilience4cata-processor</artifactId>
<version>1.2.0</version>
</path>
</annotationProcessorPaths>
Kotlin DSL, the BOM, JPMS and Lombok/MapStruct: installation. Frameworks: Spring Boot, Micronaut, Reactor.
Example
public interface PriceClient {
@Retry(maxAttempts = 3, delay = "100ms")
@CircuitBreaker(name = "prices")
@Timeout("2s")
@Fallback(method = "unknownPrice")
BigDecimal price(String sku) throws IOException;
default BigDecimal unknownPrice(String sku, Exception failure) {
return BigDecimal.ZERO;
}
}
ResilienceRegistry registry = ResilienceRegistry.builder().build();
PriceClient prices = Resilient.of(PriceClient.class, new HttpPriceClient(), registry);
BigDecimal price = prices.price("sku-1"); // retried, timed out after 2 s, guarded by the "prices" breaker
The processor generates PriceClient$Resilient; a wrong attribute or fallback signature fails the compilation. Annotated classes work too (Resilient.create(...)), and then self-calls are protected as well. Full walkthrough: quickstart.
JMH against Resilience4j 2.4.0 with equivalent configuration, AWS c7g.xlarge (Graviton3), Corretto 21, average time and allocation per call:
| Scenario |
Threads |
resilience4cata |
Resilience4j |
| Retry, first attempt succeeds |
1 |
1.4 ns, 0 B |
22.9 ns, 0 B |
| Circuit breaker, all calls succeed |
1 |
1.9 ns, 0 B |
89.9 ns, 0 B |
| Circuit breaker, all calls succeed |
4 |
1.9 ns, 0 B |
850 ns, 2 B |
| Circuit breaker, 1 in 10 fails |
4 |
278 ns, 0 B |
940 ns, 2 B |
| Sync timeout |
1 |
23.6 ns, 0 B |
8908 ns, 183 B |
| Bulkhead with free slots |
1 |
17.3 ns, 0 B |
20.1 ns, 0 B |
Speed is measured on ARM only so far (Graviton3, Apple M1); x86 and tail latencies are not measured yet. Method, fairness adjustments and all results: benchmarks. CI fails a pull request if a sync success or rejection path starts allocating, and runs the concurrency tests (jcstress) on both x86 and ARM after every merge (why both).
Documentation
Build
./gradlew build (JDK 17+, compiles with a Java 21 toolchain). ./gradlew publishToMavenLocal installs the artifacts locally. ./gradlew :site:serve previews the docs site.
License
Apache License 2.0.
评论