Exercise — Circuit Breaker: add fallback behavior when a dependency is down.

Topic: microservices, Resilience. Level: Intermediate.

Answer

Wrap the call in a breaker and provide a fallback (cached value, default, or degraded response) invoked when the breaker is open or the call fails. In Spring, `@CircuitBreaker` with a `fallbackMethod` keeps it declarative. ```java @Service class RecommendationService { @CircuitBreaker(name = "recs", fallbackMethod = "popularFallback") public List<Item> personalized(String userId) { return recsClient.forUser(userId); // remote call } // Same signature + Throwable; runs when circuit open or call fails List<Item> popularFallback(String userId, Throwable t) { return cache.popularItems(); // graceful degradation } } ```