One of those questions that helps determine whether you’ve worked with complex systems. The first thing to understand is that a Circuit Breaker is a pattern that stops a system from attempting an operation that is very likely to fail. This in turn, will save resources and stop the endless flow of identical logs.
Example usage
We have Service A that sends a request to Service B. In turn, it calls a third-party API and stores something in a database. A Circuit Breaker can act as a proxy between Services A and B. This proxy works like a fuse and, if a critical error occurs, it can block traffic to B. The conditions under which the breaker trips can be different. The most common approach is a threshold based on the number of errors of a certain type (for example, 500/502 responses) within a specific time window (for example, 60 seconds).
After that, it will check whether the issue has been resolved, and once the service is working again, it will restore traffic to it. Technically, a Circuit Breaker has several states that determine how requests to the service are handled.
Closed
This state means that everything is fine with the service and requests are being routed where they should be. If critical errors occur, it starts counting them, and once their number exceeds the specified threshold, the proxy transitions to the Open state. In addition, a timer starts, and once it expires, the proxy moves to the Half-Open state, where the service’s health is checked.
Open
In this state, the Circuit Breaker will not route requests to the service. Instead, it will return an error or a fallback response. It depends on the implementation, but the main purpose of this state is to prevent traffic from reaching the service.
Half-Open
In this state, we allow a limited number of requests to reach the service. If all of them complete successfully, we move the Circuit Breaker to the Closed state and resume traffic to the service. If at least one request fails, we move it back to the Open state and restart the timer.