spring-boot-resilience4j
Install this skill
npx skills add giuseppe-trisciuoglio/developer-kitWorks across Claude Code, Cursor, Codex, Copilot & Antigravity
Resilience4j for Spring Boot provides a functional, modular approach to increasing system stability within Java applications. It integrates directly into the Spring ecosystem using Aspect-Oriented Programming (AOP), allowing developers to decorate service methods with annotations that handle distributed system failures. By decoupling fault tolerance logic from business code, this library manages circuit breakers, request retries, rate limiting, and bulkhead isolation via centralized YAML configuration. This approach prevents systemic failure when downstream dependencies encounter downtime or latency spikes. It is an alternative to Hystrix, focusing on lightweight components that operate without requiring auxiliary infrastructure, making it highly effective for reactive and blocking service architectures seeking to maintain predictable behavior under fluctuating traffic loads or intermittent network instability.
When to Use This Skill
- β’Preventing cascading failures when calling third-party REST APIs
- β’Protecting database connections from becoming saturated during traffic bursts
- β’Mitigating intermittent connectivity issues with external message brokers
- β’Enforcing API consumption quotas for specific service endpoints
How to Invoke This Skill
Example prompts that trigger this skill in Claude Code, Cursor, or Antigravity:
- βHow do I prevent my Spring service from crashing when the API is down?
- βShow me how to retry a failed RestTemplate call automatically.
- βLimit the number of requests per second to my payment endpoint.
- βHow to configure a circuit breaker with Spring Boot 3?
- βAdd fault tolerance to my Spring Boot microservice.
Pro Tips
- π‘Combine multiple Resilience4j patterns (e.g., Circuit Breaker + Retry + Time Limiter) for comprehensive fault tolerance on critical operations and external calls.
- π‘Leverage Spring Boot Actuator endpoints (e.g., `/actuator/resilience4j`) for real-time monitoring of Resilience4j metrics, allowing you to observe pattern states and performance.
- π‘Tune the configuration parameters for each pattern (e.g., `failureRateThreshold`, `waitDurationInOpenState` for Circuit Breaker) based on the specific characteristics and expected latency of the external dependency or operation.
What this skill does
- β’Stateful circuit breaker management with health monitoring
- β’Configurable automated retry policies with exponential backoff
- β’Request rate limiting to throttle excessive traffic
- β’Resource-level bulkhead isolation for thread pool protection
- β’Annotation-driven fallback methods for graceful degradation
When not to use it
- βWhen you require a centralized service mesh for fault tolerance across non-JVM languages
- βWhen simple, manual try-catch blocks are sufficient for low-complexity local operations
- βWhen the infrastructure layer already handles request throttling and circuit breaking
Example workflow
- Include the resilience4j-spring-boot3 and starter-aop dependencies in your build file.
- Define circuit breaker or retry thresholds in application.yml.
- Annotate target service methods with @CircuitBreaker or @Retry.
- Implement a fallback method within the same class to handle failures.
- Verify health indicators using the Spring Boot Actuator endpoint.
Prerequisites
- βSpring Boot 3.x project
- βSpring Boot Actuator for monitoring
- βBasic understanding of AOP
Pitfalls & limitations
- !AOP annotations require the target method to be public and called from an external bean.
- !Over-configuring wait durations can lead to thread exhaustion if timeouts are not synchronized.
- !Fallback methods must reside in the same class to be easily detected by the AOP proxy.
FAQ
How it compares
Unlike manual try-catch blocks or custom interceptors, Resilience4j provides standardized, production-ready patterns with built-in metrics and externalized configuration that evolve with your system's needs.
π Full skill instructions β original source: giuseppe-trisciuoglio/developer-kit
## When to Use
To implement resilience patterns in Spring Boot applications, use this skill when:
- Preventing cascading failures from external service unavailability with circuit breaker pattern
- Retrying transient failures with exponential backoff
- Rate limiting to protect services from overload or downstream service capacity constraints
- Isolating resources with bulkhead pattern to prevent thread pool exhaustion
- Adding timeout controls to async operations with time limiter
- Combining multiple patterns for comprehensive fault tolerance
Resilience4j is a lightweight, composable library for adding fault tolerance without requiring external infrastructure. It provides annotation-based patterns that integrate seamlessly with Spring Boot's AOP and Actuator.
## Instructions
### 1. Setup and Dependencies
Add Resilience4j dependencies to your project. For Maven, add to
pom.xml:<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot3</artifactId>
<version>2.2.0</version> // Use latest stable version
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>For Gradle, add to
build.gradle:implementation "io.github.resilience4j:resilience4j-spring-boot3:2.2.0"
implementation "org.springframework.boot:spring-boot-starter-aop"
implementation "org.springframework.boot:spring-boot-starter-actuator"Enable AOP annotation processing with
@EnableAspectJAutoProxy (auto-configured by Spring Boot).### 2. Circuit Breaker Pattern
Apply
@CircuitBreaker annotation to methods calling external services:@Service
public class PaymentService {
private final RestTemplate restTemplate;
public PaymentService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@CircuitBreaker(name = "paymentService", fallbackMethod = "paymentFallback")
public PaymentResponse processPayment(PaymentRequest request) {
return restTemplate.postForObject("http://payment-api/process",
request, PaymentResponse.class);
}
private PaymentResponse paymentFallback(PaymentRequest request, Exception ex) {
return PaymentResponse.builder()
.status("PENDING")
.message("Service temporarily unavailable")
.build();
}
}Configure in
application.yml:resilience4j:
circuitbreaker:
configs:
default:
registerHealthIndicator: true
slidingWindowSize: 10
minimumNumberOfCalls: 5
failureRateThreshold: 50
waitDurationInOpenState: 10s
instances:
paymentService:
baseConfig: defaultSee @references/configuration-reference.md for complete circuit breaker configuration options.
### 3. Retry Pattern
Apply
@Retry annotation for transient failure recovery:@Service
public class ProductService {
private final RestTemplate restTemplate;
public ProductService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@Retry(name = "productService", fallbackMethod = "getProductFallback")
public Product getProduct(Long productId) {
return restTemplate.getForObject(
"http://product-api/products/" + productId,
Product.class);
}
private Product getProductFallback(Long productId, Exception ex) {
return Product.builder()
.id(productId)
.name("Unavailable")
.available(false)
.build();
}
}Configure retry in
application.yml:resilience4j:
retry:
configs:
default:
maxAttempts: 3
waitDuration: 500ms
enableExponentialBackoff: true
exponentialBackoffMultiplier: 2
instances:
productService:
baseConfig: default
maxAttempts: 5See @references/configuration-reference.md for retry exception configuration.
### 4. Rate Limiter Pattern
Apply
@RateLimiter to control request rates:@Service
public class NotificationService {
private final EmailClient emailClient;
public NotificationService(EmailClient emailClient) {
this.emailClient = emailClient;
}
@RateLimiter(name = "notificationService",
fallbackMethod = "rateLimitFallback")
public void sendEmail(EmailRequest request) {
emailClient.send(request);
}
private void rateLimitFallback(EmailRequest request, Exception ex) {
throw new RateLimitExceededException(
"Too many requests. Please try again later.");
}
}Configure in
application.yml:resilience4j:
ratelimiter:
configs:
default:
registerHealthIndicator: true
limitForPeriod: 10
limitRefreshPeriod: 1s
timeoutDuration: 500ms
instances:
notificationService:
baseConfig: default
limitForPeriod: 5### 5. Bulkhead Pattern
Apply
@Bulkhead to isolate resources. Use type = SEMAPHORE for synchronous methods:@Service
public class ReportService {
private final ReportGenerator reportGenerator;
public ReportService(ReportGenerator reportGenerator) {
this.reportGenerator = reportGenerator;
}
@Bulkhead(name = "reportService", type = Bulkhead.Type.SEMAPHORE)
public Report generateReport(ReportRequest request) {
return reportGenerator.generate(request);
}
}Use
type = THREADPOOL for async/CompletableFuture methods:@Service
public class AnalyticsService {
@Bulkhead(name = "analyticsService", type = Bulkhead.Type.THREADPOOL)
public CompletableFuture<AnalyticsResult> runAnalytics(
AnalyticsRequest request) {
return CompletableFuture.supplyAsync(() ->
analyticsEngine.analyze(request));
}
}Configure in
application.yml:resilience4j:
bulkhead:
configs:
default:
maxConcurrentCalls: 10
maxWaitDuration: 100ms
instances:
reportService:
baseConfig: default
maxConcurrentCalls: 5
thread-pool-bulkhead:
instances:
analyticsService:
maxThreadPoolSize: 8### 6. Time Limiter Pattern
Apply
@TimeLimiter to async methods to enforce timeout boundaries:@Service
public class SearchService {
@TimeLimiter(name = "searchService", fallbackMethod = "searchFallback")
public CompletableFuture<SearchResults> search(SearchQuery query) {
return CompletableFuture.supplyAsync(() ->
searchEngine.executeSearch(query));
}
private CompletableFuture<SearchResults> searchFallback(
SearchQuery query, Exception ex) {
return CompletableFuture.completedFuture(
SearchResults.empty("Search timed out"));
}
}Configure in
application.yml:resilience4j:
timelimiter:
configs:
default:
timeoutDuration: 2s
cancelRunningFuture: true
instances:
searchService:
baseConfig: default
timeoutDuration: 3s### 7. Combining Multiple Patterns
Stack multiple patterns on a single method for comprehensive fault tolerance:
@Service
public class OrderService {
@CircuitBreaker(name = "orderService")
@Retry(name = "orderService")
@RateLimiter(name = "orderService")
@Bulkhead(name = "orderService")
public Order createOrder(OrderRequest request) {
return orderClient.createOrder(request);
}
}Execution order: Retry β CircuitBreaker β RateLimiter β Bulkhead β Method
All patterns should reference the same named configuration instance for consistency.
### 8. Exception Handling and Monitoring
Create a global exception handler using
@RestControllerAdvice:@RestControllerAdvice
public class ResilienceExceptionHandler {
@ExceptionHandler(CallNotPermittedException.class)
@ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
public ErrorResponse handleCircuitOpen(CallNotPermittedException ex) {
return new ErrorResponse("SERVICE_UNAVAILABLE",
"Service currently unavailable");
}
@ExceptionHandler(RequestNotPermitted.class)
@ResponseStatus(HttpStatus.TOO_MANY_REQUESTS)
public ErrorResponse handleRateLimited(RequestNotPermitted ex) {
return new ErrorResponse("TOO_MANY_REQUESTS",
"Rate limit exceeded");
}
@ExceptionHandler(BulkheadFullException.class)
@ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
public ErrorResponse handleBulkheadFull(BulkheadFullException ex) {
return new ErrorResponse("CAPACITY_EXCEEDED",
"Service at capacity");
}
}Enable Actuator endpoints for monitoring resilience patterns in
application.yml:management:
endpoints:
web:
exposure:
include: health,metrics,circuitbreakers,retries,ratelimiters
endpoint:
health:
show-details: always
health:
circuitbreakers:
enabled: true
ratelimiters:
enabled: trueAccess monitoring endpoints:
-
GET /actuator/health - Overall health including resilience patterns-
GET /actuator/circuitbreakers - Circuit breaker states-
GET /actuator/metrics - Custom resilience metrics## Best Practices
- **Always provide fallback methods**: Ensure graceful degradation with meaningful responses rather than exceptions
- **Use exponential backoff for retries**: Prevent overwhelming recovering services with aggressive backoff (
exponentialBackoffMultiplier: 2)- **Choose appropriate failure thresholds**: Set
failureRateThreshold between 50-70% depending on acceptable error rates- **Use constructor injection exclusively**: Never use field injection for Resilience4j dependencies
- **Enable health indicators**: Set
registerHealthIndicator: true for all patterns to integrate with Spring Boot health- **Separate failure vs. client errors**: Retry only transient errors (network timeouts, 5xx); skip 4xx and business exceptions
- **Size bulkheads based on load**: Calculate thread pool and semaphore sizes from expected concurrent load and latency
- **Monitor and adjust**: Continuously review metrics and adjust timeouts/thresholds based on production behavior
- **Document fallback behavior**: Make fallback logic clear and predictable to users and maintainers
## Common Mistakes
Refer to
references/testing-patterns.md for:- Testing circuit breaker state transitions
- Simulating transient failures with WireMock
- Validating fallback method signatures
- Avoiding common misconfiguration errors
Refer to
references/configuration-reference.md for:- Complete property reference for all patterns
- Configuration validation rules
- Exception handling configuration
## References and Examples
- [Complete property reference and configuration patterns](references/configuration-reference.md)
- [Unit and integration testing strategies](references/testing-patterns.md)
- [Real-world e-commerce service example using all patterns](references/examples.md)
- [Resilience4j Documentation](https://resilience4j.readme.io/)
- [Spring Boot Actuator Skill](/skills/spring-boot-actuator/SKILL.md) - Monitoring resilience patterns with Actuator
How to Use This Skill Unit
Option A: Project-Specific (Recommended)
- Click "Download" above
- In your project, create the directory:
.agent/skills/spring-boot-resilience4j/ - Save the file as
SKILL.md - The agent will automatically discover the skill based on its description.
Option B: Global Installation (All Agents)
Save the file to these locations to make it available across all projects:
- Claude Code:
~/.claude/skills/giuseppe-trisciuoglio/developer-kit/spring-boot-resilience4j/SKILL.md - Cursor:
~/.cursor/skills/giuseppe-trisciuoglio/developer-kit/spring-boot-resilience4j/SKILL.md - Antigravity:
~/.gemini/antigravity/skills/giuseppe-trisciuoglio/developer-kit/spring-boot-resilience4j/SKILL.md
π Install with CLI:npx skills add giuseppe-trisciuoglio/developer-kit