📌 Introduction
In microservice or API aggregation systems, service calls can fail because of network jitter, unstable third-party services, and similar issues. To improve system resilience and stability, Resilience4j provides a complete set of fault-tolerance components covering retries, circuit breaking, rate limiting, timeout control, and resource isolation.
This article covers Resilience4j's core module principles, annotation usage, and how to integrate it elegantly in a Spring Boot project. It also uses diagrams to help explain how the modules work together.
📦 1. Add Dependencies
Using Spring Boot + Maven to manage the project, add the following dependency:
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot3</artifactId>
</dependency>
🧩 2. Resilience4j Core Modules
| Module name | Purpose | Use case |
|---|---|---|
Retry | Automatic retry on failure | Network jitter, transient failures |
CircuitBreaker | Circuit breaker | Fault isolation, prevent cascading failures |
RateLimiter | Rate limiter | Limit call frequency |
Bulkhead | Thread/semaphore isolation | Prevent a single service from taking down the system |
TimeLimiter | Timeout limit | Control service call response time |
🔄 How the Modules Work Together
The diagram above shows a call chain in which the modules can run in sequence. All modules are optional.
🧾 3. Annotation Usage in Detail
After integrating Resilience4j into Spring Boot, you can use the following annotations:
🔁 @Retry
YAML configuration:
resilience4j.retry:
instances:
imageRetry:
max-attempts: 3
wait-duration: 1s
⚡ @CircuitBreaker
@CircuitBreaker(name = "imageCB", fallbackMethod = "fallback")
public String generateImage(String prompt) {
return callImageApi(prompt);
}
resilience4j.circuitbreaker:
instances:
imageCB:
failure-rate-threshold: 50
sliding-window-size: 5
wait-duration-in-open-state: 10s
🚦 @RateLimiter
@RateLimiter(name = "imageRL", fallbackMethod = "fallback")
public String generateImage(String prompt) {
return callImageApi(prompt);
}
resilience4j.ratelimiter:
instances:
imageRL:
limit-for-period: 5
limit-refresh-period: 1s
⛓️ @Bulkhead
@Bulkhead(name = "imageBH", fallbackMethod = "fallback")
public String generateImage(String prompt) {
return callImageApi(prompt);
}
resilience4j.bulkhead:
instances:
imageBH:
max-concurrent-calls: 3
⏱️ @TimeLimiter (for async methods)
resilience4j.timelimiter:
instances:
imageTL:
timeout-duration: 2s
🔗 Recommended Annotation Order
All modules share the single name img and are configured together through application.yml.
❗ Notes on Annotations and Fallback
❓ If @Retry triggers fallback, does execution still go through @CircuitBreaker?
❌ No!
Once
@Retry'sfallbackMethodreturns a result successfully, no further exception is thrown, soCircuitBreakernever sees one.
✅ Recommended approach:
Use @CircuitBreaker to manage fallback, and let @Retry handle retries:
🧠 Advanced Usage: Multi-Level Fallback Chain (A → B → C)
Annotations can't implement multi-level fallback, but you can chain the calls programmatically:
Combined with functional interfaces such as CircuitBreaker.decorateSupplier(...), you can build a complete fault-tolerance chain.
📈 Micrometer Metrics Integration
After integrating Resilience4j into Spring Boot, Actuator + Prometheus support is enabled by default, and you can observe the following metrics:
resilience4j_circuitbreaker_calls_totalresilience4j_retry_calls_totalresilience4j_rate_limiter_wait_duration_seconds
✅ Summary
| Feature | Annotation | YAML Config | Fallback Support |
|---|---|---|---|
| Retry | ✅ | ✅ | ✅ |
| CircuitBreaker | ✅ | ✅ | ✅ |
| RateLimiter | ✅ | ✅ | ✅ |
| Bulkhead | ✅ | ✅ | ✅ |
| TimeLimiter | ✅ | ✅ | ✅ (async) |
- In Spring Boot projects, combining annotations + YAML is the quickest way to get up and running
- For complex fault-tolerance chains, use programmatic composition
- It's clearer to let CircuitBreaker manage fallback strategies
📚 Further Reading
- Official documentation: https://resilience4j.readme.io/
- GitHub project: https://github.com/resilience4j/resilience4j
- Micrometer metrics integration: https://micrometer.io