In the distributed system especially microservices there are lots are services working in together to full-fill a user request.
Now think of a scenario where a service is experiencing high latency or has become unresponsive due to an unexpected issue, as this service will now take time to respond to each request, this will not stop the ingress, it can cause dependent services to become slow or unresponsive as well, leading to a cascading effect (called Domino effect) that impacts the entire system. This can result in widespread performance degradation or even a complete system outage.
What will Circuit Breaker do?
The circuit breaker monitors the communication between a client and an external service. When a predefined number of failures occur, the circuit breaker trips
(red) and blocks further requests to the failing service, allowing the service time to recover. After a specified timeout, the circuit breaker will allow a limited number of requests
(yellow) to pass through to determine if the service has recovered. If the service is stable, the circuit breaker resets
(green) and normal operation resumes. If not, the circuit breaker remains open and the process repeats.
Service: This represents your application or service that relies on an external dependency (e.g., a database, API, or another service) represented by
Circuit Breaker: The Circuit Breaker component has three states:
- Closed: In this state, the Circuit Breaker allows requests to pass through. It monitors the responses for failures.
- Open: When a specified failure threshold is reached, the Circuit Breaker transitions to the Open state. In this state, all requests are blocked, and no further requests are sent to the external dependency for a predefined duration.
- Half-Open: After the timeout period expires, the Circuit Breaker transitions to the Half-Open state. In this state, it allows a limited number of test requests to pass through. If these test requests succeed, the Circuit Breaker transitions back to the Closed state; otherwise, it returns to the Open state.
The circuit breaker can also be used in the case of third-party systems. For example, suppose an e-commerce site is running a sale that depends on a payment gateway (PG) A to collect payments from customers. If PG A experiences latency and the payment success rate drops, we can use a circuit breaker to halt the usage of PG A and switch to another payment provider, such as PG B.