2 min read

Circuit Breaker Pattern

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.

graph LR subgraph Circuit Breaker C{Closed} D{Open} E{Half-Open} end subgraph Service A((Service)) --> B[External Dependency] end B -->|Request| C B -->|Request| D B -->|Request| E C -->|Success| B D -->|Failure| E E -->|Timeout| C E -->|Success| B E -->|Failure| D

Explanation

Service: This represents your application or service that relies on an external dependency (e.g., a database, API, or another service) represented by External Dependency.

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.

I have seen usage in ruby using stoplight gem. There are other soultions this one by Netflix Hystrix, one more by Shopify semian.

bliki: CircuitBreaker
You use software circuit breakers on connections to remote services. These breakers trip when the supplier becomes unresponsive, once tripped the breaker no longer calls the supplier until reset.