Why and where to use Circuit Breaker Pattern on Microsoft Azure?It is almost certain that every single person reading this article feels annoyed to wait for an elevator which is held and blocked by a person. In this case elevator might be a third party web service and person blocking it might be the developer maintaining it. OK, there might be a problem on the elevator, or the guy blocking it might have a reasonable explanation, but at some point you need to take the stairs. Same rule applies to the projects running on Microsoft Azure.
There needs to be a threshold and a time-out for an operation to complete. So lets say, if you are working with an external web service, you have a slight chance that the service might be down. So, you need to carry on your transaction without tolerating this to hold your process. For this kind of situation, we have a pattern called Circuit Breaker Pattern.
Simply, pattern acts as proxy on processes that has a chance to fail. We implement the pattern as a state machine containing three states as Open, Half-open and Closed.
Idea is to monitor the failures (log as necessary) and give decision. Lets take a look at the states:
Open: States that the circuit is in open state running the transaction. Gets set on start of the trip.
Half-open: States that circuit time-out elapsed and running until the threshold reached. Vital to have this state as process might be delayed for a reason and we should not flood it with requests.
Closed: Initial state or state set after trip resets.
Which can be seen on the diagram below.
If we take a look at to the code I wrote, we might have a better perspective. Note that, constructor takes default time-out of 3 seconds.
If you like, you can download the code from github link below. To test, I have published a simple service to Azure running on http://servicetests.azurewebsites.net/api/test/GetDelayedTest and simply run the following method as:
comments powered by Disqus