How is the Circuit Breaker Pattern implemented in Microservice Architecture?

Spandan Pyakurel
3 min readMay 30, 2020

Microservice architecture is the style of designing an application where an individual application can be viewed as a number of loosely coupled services. The architecture has been proven to be a scalable solution for many software applications. There are certain things to be handled in the architecture for the proper functioning of the system.

SCENARIO

Let us imagine a system built-in microservice architecture. Let server A and server B be two of the many servers of the system. If server A requests server B for a particular service, we can say that if server A is to serve an incoming request, it calls server B and waits for the response. If the timeout of the request is high, it takes time to throw a failure error to the server A as well. Without being aware of the situation of failure of server B, server A may retry many times and hence result in filling all the pools dedicated to server A. In this situation, the failure in server B results in failure of server A as well. In systems containing more than 2 services involved, there may be cascading failures of many services and thus result in failure of the system as a whole.

Figure 1 The Flow request-response in Microservice Architecture

Comparison of MicroService System with Electrical Circuit System

Excessive requests can result in the failure of the microservice system as a whole. In the same way, excessive current can damage electric circuit systems. Both kinds of situations can be prevented with the use of a circuit breaker. In electric circuit systems, the circuit breaker is used to protect an electric circuit from being damaged due to overload, short circuit, or excessive current. In normal conditions, the circuit breaker allows an electrical connection between two nodes, which is called a closed circuit. Whenever a failure condition arises, the circuit breaker prevents the current flow between two nodes, which is called an open circuit.

What is a Circuit Breaker Pattern in Microservice Architecture?

The analogy of an electrical circuit breaker can be derived for the case of a system built-in microservice architecture as well. A circuit breaker can be employed between two services. In normal conditions, the circuit breaker is in a closed state. It records the no. of successes and failures whenever Service A tries to connect to service B. Whenever failure ratio crosses a certain threshold, the circuit breaker goes into an open state. After a certain timeout, the circuit breaker again goes to a closed state.

Figure 2 Circuit Breaker Pattern in Microservice Architecture

The need of Half-Open State in Circuit Breaker

Let us imagine a situation where, even after the transition from open to closed state, service B is not ready to provide service. The circuit breaker will have to wait for the threshold failure ratio to go to an open state. So, to avoid this situation, there comes a third state, half-open. In this state, the circuit breaker allows only a defined maximum number of failed requests from Service A to Service B to check whether Service B is working or not. If not, the circuit will go back to the open state without having to go to a closed state and suffer the consequence of failures. The state management in the circuit breaker pattern can be visualized in Figure 3.

Figure 3 State Management in Circuit Breaker

CONCLUSION

The parameters used to define a circuit breaker (Failure Threshold, Timeout, and Maximum Requests) may vary as per the system designed.

This article is a general overview for those not familiar with the concept of the circuit breaker pattern. Hopefully, this article gave you an introductory idea of the circuit breaker pattern and how it can be implemented in a microservice architecture.

--

--