Circuit Breaker Pattern in Microservices

 The circuit breaker pattern is a design pattern that falls under the sustainable design patterns category. It allows developers to prevent cascading failures in microservices architecture by invoking remote services through a proxy.

Microservices architecture has become the new norm for large-scale applications. Because it has more advantages compared to traditional monolithic architecture. However, microservices also come with several challenges. One such challenge is preventing cascading failures. For example, network or service failure in one microservice can quickly cascade into other services and cause a system-wide failure.

Why We Need Circuit Breaker Pattern?

In a microservices architecture, services have to communicate with each other. Sometimes, there can be service failures, or it takes significant time to respond to requests due to network connectivity issues

For example, assume that there are 2 microservices named catalog and orders. When the catalog service needs to communicate with the orders service, it creates a new thread and sends a request to the orders service. However, if there are any network issues or timeout failures from the orders service, the catalog service will not get an instant response. Also, there is no way to inform the catalog service about the failure so it will wait for a response. As a result, the catalog service will send continuous requests to the orders service until its resources are exhausted, resulting in catalog service failure.

Understanding and implementing the circuit breaker pattern is pretty easy. It has three states: Closed, Open, and Half Open

  • Open



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. If the failure count exceeds the specified threshold value, the circuit breaker will move to the Open state. If not, it will reset the failure count and timeout period.

  •     Closed

Once the circuit breaker moves to the Open state, it will completely block the communication between microservices. So, the article service will not receive any requests, and the user service will receive an error from the circuit breaker.

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.

As you can see, the concept of the circuit breaker pattern is pretty simple. Furthermore, there are specialized third-party libraries for almost every major language to simplify your work


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

For Python — pycircuitbreaker
For .NET — Polly


Advantages of using 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












Comments

Popular posts from this blog

Email Sending through O365 using OAuth Protocol

IISRESET vs App Pool Recycling ?

Deploy .Net6.0 Web api with docker