Circuit Breaker in Microservices Architecture

Rishi Sharma
December 29, 2020

The closest circuit breaker that we can relate to in our daily lives is MCB, which is present in every home to protect devices from electrical circuit failures which could lead to increased flow of current beyond a specific capacity. It achieves protection from high in-flow of current by disrupting or opening the circuit to stop the flow.

Let’s consider we have microservices A, B and C. Assume that A calls B and B further calls C. Now, if at any point of time, if C experiences some issues (like CPU usage is high, or service is down) due to which it takes long time for responding. This can lead to issues in B as C’s response time has increased, which will lead to resources (threads) at B being occupied for a longer period of time. This will further slow down the responses from B to A also. Such failures are called cascading failures. So these cascading failures should be handled to prevent them from translating to other services and causing failures.

A cascading failure is a process in which failure of a downstream service can trigger the failure of upstream services.

Circuit breaker provides a way to cut off the connection to other service if failure threshold limit has been reached for that service and the connection is turned on after a specified cooling period which would give time to that service to recover

Circuit Breaker has 3 states:

  1. CLOSE — connection to downstream service is allowed
  2. OPEN — connection to downstream service is forbidden and we may handle such cases using fallback method or default response
  3. HALF OPEN — some calls to downstream service are allowed

State Cycle

Initially, circuit breaker will be in CLOSE state, i.e., all communication to downstream service is allowed. Once the failure threshold limit is reached for downstream service, the state changes to OPEN, i.e., communication to downstream service is forbidden/denied. After cooling period, state is changed to HALF OPEN, if any request comes for the downstream service, then only certain number of calls are allowed. If threshold success rate is achieved, the state changes to CLOSE, else it again moves to OPEN state and again it needs to wait for the cooling period.

OPEN state helps in preventing cascading failures by forbidding any requests to downstream service, thus not blocking any resources in the current service. Further, a fallback method can be defined to gracefully handle negative scenarios.

Popular libraries available for circuit breaker are :

  • Hystrix
  • Resilience4j

In another article, I have discussed how to build basic circuit breaker. Please go through it.


About Quinbay

Quinbay is a dynamic one stop technology company driven by the passion to disrupt technology today and define the future.
We private label and create digital future tech platforms for you.

Digitized . Automated . Intelligent