by BehindJava

What is Circuit Breaker Pattern in Microservices

Home » microservices » What is Circuit Breaker Pattern in Microservices

In this tutorial we are going to learn about Circuit Breaker Pattern in detail.

Circuit Breaker Pattern

  • Applies to API that makes point to point calls between the microservices.
  • We set a threshold for failing API calls i.e., 3. If the failing API calls exceed the threshold we reject the further API calls to that microservice.
  • we do that and create a microservices that is basically called as Circuit breakers. These are microservices themselves that we put it in front of the target microservices that act as a proxy.

Circuit breakers can have three states

  1. Open State

    • This is quiet opposite to the closed state, when the circuit is open client makes a call to a circuit breaker and it decides that supplier microservice is not able to respond due to the breakage of microservice or suffering from overloading where scaling up is required. cbp
    • Circuit breaker returns an error to the client and client has to handle that error gracefully and try again.
  2. Closed State

    • We have a client microservice that needs to make an API call to the supplier microservice and in between we have the circuit breaker. cbp
    • When client wants to make a call to the supplier microservice it has to go through the circuit breaker microservice in which it passes the request to the supplier MS and it returns the value to the circuit breaker and it returns it to the client in which the circuit breaker acted as a proxy and we call this scenario as a Closed state.
  3. Half-Open State

    • The state Half-Open i.e., in between the open and closed where the circuit breaker just rejects the half of the requests and passes the other half to the supplier microservices so that there is no case of overloading.

Notes:

  • The idea of having one centralized circuit breaker service is not a good idea because we are going to have one single point of failure. instead we can have the circuit breaker built into the microservices.
  • In .net 3.1 this capability was added to web API’s where you can check the state of the CPU usage i.e., high or low and also check weather the downstream microservices are returning error based on which you can take the immediate action.
  • Its better to have the mix and match of circuit breaker for key microservices that they are not supposed to get overloaded by any means for example payment gateway’s on big billion sale day where as we can have a inbuilt circuit breaker mechanism for other microservices.