top of page

Design Patterns | Circuit Breaker | States | Part 2

After gaining understanding of the problem statement and solution in the first article: https://www.techlearnings.org/single-post/design-patterns-circuit-breaker-part-1 , it's time now to further deep dive into circuit breaker design pattern.


If you're still not clear, I highly recommend to read the 1st part before proceeding further. Let me reuse the image from 1st part, so that the understanding refreshes in mind.




As we know, the circuit breaker acts as a proxy in between the source of the call and the destination. There can be different states of the circuit breaker -


i. Open: Means no request is allowed to flow to destination. The requestor will immediately get an error response.


ii. Half-Open: Means limited number of requests are allowed to flow to destination. By limited here we mean a certain threshold of requests. There are two possibilities in this case - either the requests will fail or succeed.


If all requests are successful, it means the failure at destination is now resolved and thus, circuit moves to closed state which allows a request to reach destination. In other words, success threshold is reached, and circuit it back to closed state. (More about closed state in next point)


If any of the request fails, it means the failure or fault at destination is still present, therefore circuit moves to open state in this case. This allows the destination to recover completely before it starts serving the requests again.


iii. Closed: Means a request is allowed to flow to destination. Again, there are two possibilities in this case - either the request will succeed or fail.


Question comes will next subsequent request be allowed to destination or not in case of failure?


The way it works is, circuit breaker maintains a count of the failures. If a call fails it increments the counter, and when counter breaches a certain threshold within a given time period, the circuit moves to open state.


Therefore, the answer is subsequent call is allowed until a failure threshold is breached in certain time duration.


Next question comes, how will circuit move to half open state?


When the circuit moves from closed to open state, a timeout timer is started. When the timeout expires, the circuit is moved to half-open state. Now what's the benefit of it? As mentioned previously, the half-open state allows the destination to recover.




Therefore, the main benefit we get out of the circuit breaker is it can stop requests flowing to the destination as it knows the request is likely to fail, and also allows the external system to recover. The different states provide more stability to the system and thus reduces the impact on performance by saving critical resources. The request which is going to timed out is returned immediately, thus we'll not see high response times in Grafana at-least due to request timed outs.


One good use case of circuit breaker pattern can be or many of us might have already seen. e.g. If a bank's payment API is under load or not responding as expected, we can still enrich user experience by implicitly removing that Bank from the payment options with the help of circuit breaker pattern or by giving appropriate message regarding payment success rate of that bank at UI, and adding it automatically once the API is performing well.


In the next part which will be final, we'll see some code to implement this pattern.


Till then Happy Learning! Do connect or follow me at LinkedIn, subscribe at YouTube/Facebook/Twitter!



Comments


bottom of page