Circuit Breaker Pattern
Let's learn about how circuit breakers help keep our services available.
We'll cover the following...
Introduction
Circuit breakers are a staple in every modern house. The electricity in our houses comes through the main grid and flows through the circuit breakers. There’s a chance that the grid might behave abnormally once in a while, causing an electrical surge that the wiring of our house might not be able to tolerate. Circuit breakers help us prevent the above scenario, protecting our wiring and appliances by switching them off if they detect an abnormally high amount of power.
The circuit breaker pattern that’s often used in API design functions is quite similar to electrical circuit breakers. It acts as a protective layer of our APIs, preventing them from receiving more requests than they can handle. It’s an effective method for increasing the availability of our services by detecting and potentially preventing cascading failures. It allows us to create a fault-tolerant system that can survive when key services are unavailable.
The circuit breaker pattern
The circuit breaker pattern is straightforward and has three states in its life cycle:
Closed: This is considered to be the normal state. In the closed state, all the calls being made to the service pass through, and the number of failures is monitored.
Open: If the microservice experiences slowness, the circuit breaker begins getting failures for requests to that service. Once this number of failures passes a certain limit, the breaker goes into the open state and activate a
for the service. In this state, any requests sent to the microservice are stopped by the circuit breaker immediately. Instead, the circuit breaker can send a response to the client, informing it about the timeout of the microservice and if an alternative service is available. It also advises the clients to send the requests to that service. The client can then either choose ...timeout This is a period of time during which the circuit breaker will remain in the open state.