Circuit Breaker pattern
A circuit breaker is responsible for counting successful and failed requests. If the error rate exceeds an assumed threshold, it trips and causes all further attempts to fail immediately or use a fallback mechanism.
A fallback approach is a logic that has to be performed when a request fails. For example, a service can return cached data, a default value, or an empty list of results.
After a specific period of time, the API client should get back to sending requests, and if they succeed, it closes the circuit breaker. If there are many instances of each service available and one of them works slower than others, the result is that it is overlooked during the load balancing process.
Implementation
Look at the projects from the programming repository.
Hysterix
Hystrix is a fault tolerance library designed to isolate points of access to remote systems, services, and 3rd party libraries, stop cascading failure, and enable resilience in complex distributed systems where failure is inevitable. It improves system resilience by isolating failing services and preventing failures from cascading. Hystrix achieves this by wrapping calls to external systems in HystrixCommand or HystrixObservableCommand objects, which typically execute in separate threads. It times out calls exceeding defined thresholds, maintains thread pools for each dependency, and measures successes, failures, timeouts, and rejections. Hystrix can trip a circuit breaker to stop requests to a failing service and provides fallback logic for failed requests. It also offers real-time monitoring and configuration changes.
However, Hystrix is no longer under active development. Developers are encouraged to use alternatives like Resilience4j, which offers similar functionalities such as circuit breaking, retries, rate limiting, and bulkheading.
TODO