Its a fundamental pattern for protecting your system from all manner of integration point problems. It is a way to fail fast while there is a known problem with an integration point.
The circuit breaker pattern was first described in detail by Michael Nygard in the Stability Patterns chapter of his book "Release It!" Design and Deploy Production-Ready Software.
Breakout is a C# .NET implementation of Michael Nygard's Circuit Breaker state machine, using the Gang of Four's STATE design pattern. I've created a NuGet package for it here.
This implementation is thread safe, lightweight and easily adapted into your existing codebase. Unlike other circuit breaker implementations, it leaves the responsibility for calling the third party service with your client code. Your code only needs to inform the circuit breaker of the success or failure of every call to the third party service, via
OperationSucceeded()
and OperationFailed()
. Check the README on github for example usage.Breakout - State Machine Diagram |
State Machine Explanation
The state machine starts in the CLOSED state. While in the CLOSED state, calls flow through as normal to the third party service. If the operation succeeds, the failure count is reset. If the operation fails, the failure count is incremented. When the failure count threshold is reached, the trip breaker action is performed, which transitions the state to OPEN.
While in the OPEN state, no calls flow through to the third party service. The caller just returns immediately, without performing the service call. After the open timeout has passed, the attempt reset action is performed, which transitions the state to HALF OPEN.
While in the HALF OPEN state, only one call is let through to the third party service. If the operation succeeds, we reset the circuit breaker which transitions the state to CLOSED. If the operation fails, the trip breaker action is performed, which transitions the state to OPEN.
UML Design
The design uses the Gang of Four's STATE design pattern.
Breakout - UML diagram |
Follow @dodgy_coder on Twitter