Resilience: Timeouts, Retries and Circuit Breakers
In a distributed system other services will be slow or down. Timeouts, retries with backoff, circuit breakers, bulkheads and fallbacks keep one failing dependency from taking everything down. Resilience4j is the standard library for Spring Boot.
What you will learn
- Set timeouts on every remote call
- Retry safely with backoff and jitter
- Explain circuit breaker states
- Use bulkheads and fallbacks
Key concepts
Timeout
Every network call needs connect and read timeouts. Without them, threads wait forever and the caller's pool fills up: a cascading failure.
Retry
Retry only idempotent operations and transient errors, with exponential backoff and jitter, and a small maximum. Retries multiply load on a struggling service.
Circuit breaker
CLOSED: calls pass and failures are counted. OPEN: after the failure rate passes a threshold, calls fail fast for a while. HALF_OPEN: a few trial calls decide whether to close again.
Bulkhead
Limit concurrent calls per dependency (a semaphore or separate pool), so a slow recommendations service cannot use all threads needed for checkout.
Step by step
Resilience4j in Spring Boot
@Service
class RecommendationClient {
@CircuitBreaker(name = "recs", fallbackMethod = "popular")
@Retry(name = "recs")
@Bulkhead(name = "recs")
public List<Product> forUser(String userId) {
return restClient.get().uri("/recs/{id}", userId).retrieve().body(new ParameterizedTypeReference<>() {});
}
private List<Product> popular(String userId, Throwable t) {
return cache.popularProducts(); // degrade gracefully
}
}
resilience4j:
circuitbreaker.instances.recs: { failureRateThreshold: 50, slidingWindowSize: 20, waitDurationInOpenState: 30s }
retry.instances.recs: { maxAttempts: 3, waitDuration: 200ms, enableExponentialBackoff: true }Examples
Timeouts on RestClient
@Bean
RestClient recsClient(RestClient.Builder b) {
var factory = new JdkClientHttpRequestFactory();
factory.setReadTimeout(Duration.ofMillis(500));
return b.baseUrl("http://recs-service").requestFactory(factory).build();
}Practise it
Try these without looking back. If you can explain each one out loud, you are ready for this question.
- Simulate a slow dependency with Toxiproxy or WireMock delays and watch the circuit open.
- Explain why a retry inside a retry (client and gateway both retrying) is dangerous.
Interview questions
What interviewers ask next on this topic. Answer out loud first, then open the model answer.
What is a cascading failure?
One slow service makes its callers wait, their threads and connection pools fill up, so they become slow for their callers too, and the failure spreads. Timeouts, circuit breakers and bulkheads contain it.
What makes a good fallback?
Something useful and cheap: cached data, a default (popular products), a reduced feature, or queuing the work for later. For payments, the right fallback is often a clear error rather than a guess.
Summary
Always set timeouts, retry only idempotent calls with backoff and jitter, open a circuit when a dependency keeps failing, isolate dependencies with bulkheads, and degrade with sensible fallbacks.
Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus