Microservices. Circuit Breaker. Node.js

Why should we use it and how to implement it in Node.js

Nazarii Romankiv
Level Up Coding

--

Microservices are the modern way to build scalable systems, but as with any approach it has its own challenges. Especially when we talk about the reliability and availability of your software.

What is Circuit Breaker?

Before we talk about software let’s see where this idea comes from. In electronics, when the power/voltage spikes, we need to cut off the electricity from the devices, otherwise they can be damaged. A circuit breaker performs such an action.

And it has two states, closed and open. In a closed state, electricity can reach out to devices, while in an open state no electricity can reach out to devices. Then the circuit breaker needs to be reset manually by a person back to the closed state.

Circuit Breaker diagram representation

Why do we need it?

Similar to electronics, sometimes we have spikes of requests or spikes of errors inside our software that may cause our servers to malfunction, so to help our server self-heal (e.g. do autoscaling, kill unhealthy instances, etc.) we need clients that call this server to give it some time and stop sending traffic to it.

In that way, we can help our system overall to be more resilient to “transient” errors that can self-heal in a short amount of time.

Circuit Breaker in Node.js

Circuit Breaker can be represented as the following state machine.

Circuit Breaker state machine

As you can see in comparison to the basic circuit breaker we have added a third state HALF_OPEN. Our circuit breaker will test our server via a number of requests passed from the client to verify that we can pass all the traffic back to the server. Apart from that in comparison to the electric circuit breaker, we do not need to reset the circuit breaker from OPEN to CLOSED state manually, we can do it after some period of time.

You can install my npm package axios-circuit-breaker covered with tests, to start using the circuit breaker pattern in Node.js right now! Though Circuit Breaker can be used with any kind of transport layer.

Let’s take a look at the usage example. You need to define Circuit Breaker per Axios instance to divide the states of different servers.

Now each Axios instance has a separate instance of Circuit Breaker attached to it. You can configure each instance in-depth, as well as specify logger instances and strategies to define whether a request should be counted as a fault. I would say that defining a logger is a must to have visibility into the state of the circuit breaker in runtime.

The circuit breaker will switch to OPENstate after a number of failed requests during thresholdPeriodMs the reaches threshold . The circuit breaker will use isFault a method to determine that a request is failed. In OPENstate, it will reject any request to the server.

Then after ressetPeriodMs it will switch to HALF_OLPEN and let in at the max numRequestToCloseCircuit other requests will be rejected. if all requests succeed, they will enter CLOSED state again. If at least one request fails it will go back to OPEN state.

Summary

The circuit breaker can help make your backend more reliable since it gives more time for the servers to self-heal, which makes your overall distributed system more resilient.

Axios is a great and extendable HTTP client for Node.js that allows you to change the behavior of the HTTP client.

What next?

Love Node.js? You can read my other articles about it:

--

--