Practice
1. What is the primary purpose of using a circuit breaker like Resilience4j in a Spring Boot application?
easy
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 DQuick Check:
Circuit breaker purpose = prevent repeated failures [OK]
Hint: Circuit breaker stops repeated failing calls [OK]
Common Mistakes:
- Confusing circuit breaker with scaling
- Thinking it speeds up database queries
- Assuming it encrypts data
2. Which annotation is used in Spring Boot to apply a Resilience4j circuit breaker on a method?
easy
Solution
Step 1: Recall Resilience4j integration
Resilience4j provides the @CircuitBreaker annotation to mark methods for circuit breaking.Step 2: Differentiate from other annotations
@Retryable is for retries, @Cacheable for caching, and @Transactional for DB transactions.Final Answer:
@CircuitBreaker -> Option BQuick Check:
Resilience4j circuit breaker annotation = @CircuitBreaker [OK]
Hint: Use @CircuitBreaker to enable circuit breaking [OK]
Common Mistakes:
- Confusing with @Retryable for retries
- Using caching or transaction annotations incorrectly
- Misspelling annotation names
3. Given this Spring Boot method with Resilience4j circuit breaker:
@CircuitBreaker(name = "serviceA", fallbackMethod = "fallback")
public String callService() {
// calls external service
throw new RuntimeException("Service down");
}
public String fallback(Throwable t) {
return "Fallback response";
}
What will be the output when callService() is invoked?medium
Solution
Step 1: Analyze method behavior with circuit breaker
The method throws an exception, but the circuit breaker calls the fallback method.Step 2: Understand fallback method output
The fallback method returns the string "Fallback response" which is returned to the caller.Final Answer:
"Fallback response" -> Option AQuick Check:
Fallback method output = "Fallback response" [OK]
Hint: Exception triggers fallback method output [OK]
Common Mistakes:
- Expecting exception to propagate without fallback
- Assuming null is returned on failure
- Thinking method returns normally without fallback
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
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 CQuick 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
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 AQuick Check:
Circuit breaker + fallback = stability + graceful degradation [OK]
Hint: Combine circuit breaker with fallback for best resilience [OK]
Common Mistakes:
- Relying only on retries without circuit breaker
- Not providing fallback causing failures
- Increasing threads without handling failures
