📖 Scenario: You are building a Spring Boot microservice that calls an external payment API. The payment API occasionally goes down, and you want to prevent cascading failures by adding a circuit breaker using Resilience4j.
🎯 Goal: Add Resilience4j circuit breaker to a Spring Boot service method with a fallback that returns a safe default response when the payment API is unavailable.
📋 What You'll Learn
Add the Resilience4j Spring Boot starter dependency
Configure circuit breaker properties in application.yml
Create a service method annotated with @CircuitBreaker
Implement a fallback method that returns a default response
💡 Why This Matters
🌍 Real World
Circuit breakers are essential in microservice architectures where services depend on external APIs that can fail. They prevent cascading failures and keep applications responsive.
💼 Career
Understanding resilience patterns like circuit breakers is critical for backend developers building production microservices in Spring Boot.
Progress0 / 4 steps
1
Add Resilience4j dependency
Add the Resilience4j Spring Boot starter dependency to your pom.xml file inside the <dependencies> section.
Spring Boot
Hint
The group ID is io.github.resilience4j and the artifact ID is resilience4j-spring-boot3.
2
Configure circuit breaker in application.yml
Add circuit breaker configuration in application.yml. Set the sliding window size to 10 calls, failure rate threshold to 50 percent, and wait duration in open state to 30 seconds.
Spring Boot
Hint
Use slidingWindowSize, failureRateThreshold, and waitDurationInOpenState under the named instance.
3
Create service method with @CircuitBreaker
Create a PaymentService class with a method processPayment annotated with @CircuitBreaker. Set the circuit breaker name to paymentService and the fallback method to paymentFallback. The method should use RestTemplate to call the payment API.
Spring Boot
Hint
Use @CircuitBreaker(name = "paymentService", fallbackMethod = "paymentFallback") above your method.
4
Implement fallback method
Add a fallback method called paymentFallback in the same class. It must have the same parameters as processPayment plus a Throwable parameter. Return a default response string indicating the payment is queued for retry.
Spring Boot
Hint
The fallback method must match the return type and parameters of the original method, with an additional Throwable parameter at the end.
Practice
(1/5)
1. What is the primary purpose of using a circuit breaker like Resilience4j in a Spring Boot application?
easy
A. To automatically scale the application horizontally
B. To increase the speed of database queries
C. To encrypt data sent between microservices
D. To prevent repeated calls to a failing service and allow recovery
Solution
Step 1: Understand circuit breaker role
A circuit breaker stops calls to a failing service temporarily to avoid cascading failures.
Step 2: Identify the main benefit
This helps the system recover and prevents wasting resources on repeated failing calls.
Final Answer:
To prevent repeated calls to a failing service and allow recovery -> Option D
4. Consider this incorrect Resilience4j usage in Spring Boot:
@CircuitBreaker(name = "serviceB", fallback = "fallbackMethod")
public String fetchData() {
// call external API
}
What is the main issue with this code?
medium
A. The method fetchData must return void
B. Circuit breaker name cannot be "serviceB"
C. The fallback method name should be specified with fallbackMethod, not fallback
D. The annotation @CircuitBreaker cannot be used on public methods
Solution
Step 1: Check annotation attribute names
The correct attribute for fallback method name is fallbackMethod, not fallback.
Step 2: Validate other conditions
Method return type and visibility are valid; circuit breaker name can be any string.
Final Answer:
The fallback method name should be specified with fallbackMethod, not fallback -> Option C
Quick Check:
Correct fallback attribute = fallbackMethod [OK]
Hint: Use fallbackMethod attribute for fallback name [OK]
Common Mistakes:
Using 'fallback' instead of 'fallbackMethod'
Thinking method must be void
Assuming circuit breaker name is restricted
5. You want to design a Spring Boot microservice that calls an unstable external payment API. Using Resilience4j circuit breaker, which approach best ensures system stability and graceful degradation?
hard
A. Apply @CircuitBreaker with a fallback method returning a cached payment status
B. Remove circuit breaker and rely on retries only
C. Use @CircuitBreaker without fallback to fail fast
D. Disable circuit breaker and increase thread pool size
Solution
Step 1: Understand system stability needs
Using a circuit breaker prevents repeated calls to the unstable API, avoiding overload.
Step 2: Importance of fallback for graceful degradation
A fallback method returning cached or default data keeps the system responsive during failures.
Step 3: Evaluate other options
Retries alone can worsen overload; no fallback causes failures; increasing threads doesn't solve API instability.
Final Answer:
Apply @CircuitBreaker with a fallback method returning a cached payment status -> Option A