what is Circuit Breaker Design Pattern

What is Circuit Breaker Design Pattern?

Circuit Breaker is a software design pattern used in software development especially in Microservices based software development. Circuit breaker works as same as how an electrical circuit breaker works. When multiple services communicate with each other to serve the number of request, there are many possibilities that the services are unavailable and or not responding state. So, when service-A is busy trying to communicate the other ‘unavailable’ Service-B, Service-A can also go ‘unavailable’. Similarly, all the service which is trying to communicate ‘unavailable’ service will go cascade unavailable. In this article, let’s discuss, what is Circuit Breaker Design Pattern and how this will solve the above issue.

Problem statement Explanation

We all know that Microservices Design pattern and service-oriented architecture is having more advantage over the monolithic design pattern. Like, High availability, Easy deployment, Good Fault tolerance. But at the same time, this microservice and service-oriented design pattern have the biggest drawback that it will get into the cascade failure loop as service may be in a same or different location or even in different network. So, handling this cascade failure due to unavailable service, we need the design pattern. So, in this article, we are going to see what Circuit Breaker Design Pattern is

Assume a Service oriented software application and following scenario

  • We have four different services namely Service A, B, C & D.
  • Because of the Client request, Service A will connect with Service B and B to C and C to D.
  • But, Service D failed due to some reason.
  • So, Connections made to Service D by other Services will get to wait and unresponsive service D will remain in the same state.
  • Therefore, Service C will continuously retry to make the connection with Service D. As so, Service B and Service A.
  • This will cause Cascade failure and entire application, or the part of service will go unavailable
Cascade Failure between Services
Cascade Failure between Services

Solution for Cascade failure

Assume the same scenario mentioned above. Additionally, we are going to introduce a proxy like a system called Circuit breaker. This Circuit Breaker will act as same as the electrical circuit breaker. This means when the number of consecutive failures reaches the defined threshold level and the connection thread of the services will immediately break. After the defined time, The Circuit breaker will allow a limited number of services to communicate again. If the limited number of requests gets succeeded, then Circuit Breaker will work normally and will allow the connection.

So, Continuing with the solution to the above scenario.

  • All the connections between services will go through Circuit Breaker.
  • When Service D fails and defined the maximum number of failures is reached Circuit breaker for Service C, Circuit Breaker will trip, and connection will immediately stop.
  • Then the Opened Connection will slowly start communicating with limited allowed number of connection to test the stability of unavailable service.
  • If the test connections are succeeded, then, communication across the Services will resume normally.
  • If the Connections are failed, then Circuit Breaker will create Monitor & Alert event or if we have any Self-healing implementation, will be triggered.
Circuit Breaker Routing
Circuit Breaker Routing
FutureLearn Limited

Reference Architecture of Circuit Breaker.

Let’s discuss a simple workflow or Architecture of the Circuit Breaker design pattern. The Circuit Breaker should have three states.

  • Circuit Breaker – Closed – Allow Connection through
  • The Circuit Breaker – Open – Break the connection
  • Circuit breaker – Limited Closed – Allow Limited Number of connection to Test
Circuit Breaker Architecture
Circuit Breaker Architecture
FutureLearn Limited

When Connection request occurs, it will cross Circuit Breaker first.

  • If the connection gets succeeded, Circuit Breaker will remain closed and allow the communication.
  • When Connection fails and the number of attempts has not exceeded the limit, then the Circuit Breaker will remain closed and it will allow the communication.
  • If Connection fails and the number of attempts exceeds the limit, then the Circuit Breaker will trip, and it will open. So, all the connection request will be stopped.
  • Once the Circuit breaker Opened, then it will try to enable the Limited retry for connection. Which means, it will start allowing a defined limited number of retry to test the connection status.
  • The Circuit Breaker will close and enable further communication when the retrying connections are getting succeeded.
  • If still the connection is getting failed, then the Circuit Breaker will move to open state and trigger the Monitoring and Alert system, or it will trigger the self-healing or preventive mechanism.

Example of Circuit Breaker Implementation

Let’s see an example of a circuit breaker in python. There is an awesome module available in python to implement Circuit Breaker called “pybreaker”. So, here in this example, we will discuss how the circuit breaker is helpful in the DB connection.

As the first step, we have to create an instance of the Circuit Breaker.

import pybreaker
db_breaker = pybreaker.CircuitBreaker(fail_max=5, reset_timeout=60)

So, In this instance, we have defined the maximum number of failure as 5 times and the reset timeout as 60 seconds.

To introduce this Circuit breaker to the actual logic, we are going to use python decorator.

@db_breaker
def example_db_operation(var):
    # Your Logic goes here
    pass

example_db_obj = example_db_operation(pass_var)

Similarly, if we are not going to use python decorator, then,

def example_db_operation(var):
    # Your Logic goes here
    pass

example_db_obj = db_breaker.call(example_db_operation, pass_var)

So, if the method “example_db_operation” fails continuously for 5 times, then Circuit breaker will automatically open and stop all the communication to “example_db_operation”.

There is another module in python which implements circuit breaker is “circuitbreaker”.

Conclusion

The Circuit breaker is the design pattern. So, it does not mean that we have to use the one and only suggested a way to implement it. Circuit Breaker can be implemented in any platform or any language. Especially, where two services or instance or physical network communicate with each other. It is very useful especially when you have multiple services rely on each other like microservices. It’s about your creativity to use Circuit breaker design pattern in your software development.

In this article, we have discussed what is Circuit Breaker Design Pattern and example implementation of it. In out upcoming articles, we will discuss more on Circuit Breaker and other microservice implementation patters. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOpsAgileDevSecOps and App Development.

2 thoughts on “What is Circuit Breaker Design Pattern?”

  1. That’s cool that you could get have a fix that will help prevent all the circuits form breaking at once. I could see how that would be a good way to make sure that you don’t lose all the power at once. I’ll have to consider getting someone to help me set something like that up if I start having problems with the circuits breaking.

  2. I found it to be a comprehensive and informative resource. The author effectively explains the purpose and benefits of implementing the Circuit Breaker pattern in software development, highlighting its role in enhancing system resilience and preventing cascading failures. The inclusion of examples and code snippets further aids in understanding the practical application of this pattern, making it a valuable read for developers seeking to improve the robustness of their applications. This page

Leave a Reply