▪ Fallback Pattern provides an alternative behavior or response
if a request fails or times out.
▪ If a service is unavailable, the client could use a cached
version of the data or display a default message to the user.
▪ It is used to provide an alternative course of action when a
service call fails or takes too long to complete.
▪ Define a fallback function that is called if the service call fails
or times out, and the function provides an alternative
response.
▪ In microservices, the fallback pattern help to improve the
overall reliability and stability of the system.
▪ If the payment processing service is unavailable or takes too
long to complete a request, the fallback pattern could be used
to provide an alternative response.
▪ I.e. displaying an error message or offering the user the option
to try again later.
xxxxxxxxxx
@Service
public class BookService {
private static final Logger LOG = LoggerFactory.getLogger(BookService.class);
private final WebClient webClient;
private final ReactiveCircuitBreaker readingListCircuitBreaker;
public BookService(ReactiveCircuitBreakerFactory circuitBreakerFactory) {
this.webClient = WebClient.builder().baseUrl("http://localhost:8090").build();
this.readingListCircuitBreaker = circuitBreakerFactory.create("recommended");
}
public Mono<String> readingList() {
return readingListCircuitBreaker.run(webClient.get().uri("/recommended").retrieve().bodyToMono(String.class), throwable -> {
LOG.warn("Error making request to book service", throwable);
return Mono.just("Cloud Native Java (O'Reilly)");
});
}
}
https://blog.codecentric.de/resilience-design-patterns-retry-fallback-timeout-circuit-breaker