top of page

Circuit Breaker Pattern

How to Use the Circuit Breaker Software Design Pattern to Build Microservices?

  1. One challenge with microservice architecture is cascading failure.

  2. For example, network or service failure in one microservice can quickly cascade into other services and cause a system-wide failure

  3. The circuit breaker pattern was introduced to address these issues in this context.

  4. The circuit breaker pattern is a design pattern that falls under the sustainable design patterns category.

  5. It prevents cascading failures in microservices architecture by invoking remote services through a proxy.

  6. For example, this pattern works the same as the electric circuit breakers at your home. The electric circuit breaker at your home will automatically turn off and protect your electric devices in case of abnormal behavior in the power supply.

  7. Similarly, the proxy used in the circuit breaker pattern literally works as an electric circuit breaker.

  8. With the circuit breaker pattern, you can define a threshold value for the number of failures between 2 microservices.

  9. The proxy will count the number of failures between microservices and automatically stop the request sending for a specific time if the number of failures exceeds the threshold value.

  10. Once the timeout ends, the proxy will enable a limited number of requests to check whether the microservice is working. If those requests succeed, the proxy will allow microservices to continue normal operations. If not, the proxy will again start the timeout.

How does it Works?

Circuit breaker has three states. Open, Closed and Half Open

Closed State

The initial state of the circuit breaker or the proxy is the Closed state. The circuit breaker allows microservices to communicate as usual and monitor the number of failures occurring within the defined time period.

Open State

Once the circuit breaker moves to the Open state, it will completely block the communication between microservices.The circuit breaker will remain in the Open state until the timeout period ends. Then, it will move into the Half-Open state.

Half Open

In the Half-Open state, the circuit breaker will allow a limited number of requests to reach article service. If those requests are successful, the circuit breaker will switch the state to Closed and allow normal operations. If not, it will again block the requests for the defined timeout period.

Third-Party Libraries for Implementing Circuit Breaker Pattern

Here are some of the most common third-party libraries for implementing the circuit breaker pattern.

For Java SpringBoot — gs-cloud-circuit-breaker

For TypeScript — circuit-breaker-js, @fastify/circuit-breaker

For Python — pycircuitbreaker

For .NET — Polly

Advantages of Circuit Breaker Pattern

  • Helps to prevent cascading failures.

  • Handles errors gracefully and provides better under experience.

  • Reduces the application downtimes.

  • Suitable for handling asynchronous communications.

  • State changes of the circuit breaker can be used for error monitoring.

Challenges of Circuit Breaker Pattern

  • Need good infrastructure management to maintain circuit breakers.

  • Throughput issues in services if not properly configured.

  • Difficult to test.

Final Thoughts

The circuit breaker pattern is beneficial in modern microservices architecture. It allows developers to prevent cascading errors due to network issues and improves application availability. However, you should not use the circuit breaker pattern for every microservice implementation. Before implementing, you need to evaluate the improvements it brings to your system, your team’s technical knowledge, and the circuit breakers’ maintainability. If not, you won’t be able to extract the full potential of this pattern. I hope you have found this article helpful. Thank you for Reading!

bottom of page