Circuit Breaker Pattern

By | November 3, 2018

An implementation of the Circuit Breaker pattern prevents an application from attempting an operation that is likely to fail, acting much like the circuit breaker for the electrical system in a house

The circuit breaker acts like a proxy for the application when invoking operations that may fail, particularly where the failure is long lasting.

If everything is working as expected, the circuit breaker is said to be in the closed state, and any requests from the application are routed through to the operation.

If the number of recent failures invoking the operation exceeds a threshold over some de ned period of time, the circuit breaker is tripped and changes to the open state.

In the open state, all requests from the application fail immediately without an actual attempt to invoke the real operation (for example, without trying to invoke the operation on a remote service).

In the open state, a timer controls the “cool- down” period of the proxy.

When this cool-down period expires, the circuit breaker switches to a half-open state, and a limited number of trial requests are allowed to ow through to the operation while the rest fail immediately, or the code queries the health of the service hosting the operation.

In the half-open state, if the trial requests succeed or the service responds as healthy, then the failure is deemed repaired, and the circuit breaker changes back to the closed state.

Conversely, if the trial requests fail, then the circuit breaker returns to the open state, and the timer starts a new.

 

 

 

 

 

 

 

For an application that consumes an external service, implementing the Circuit Breaker pattern in your client code increases your website resiliency because it more efficiently handles faults from the external service that may be long lasting, but eventually self-correct.

If your application is a service provider, encouraging clients to use the Circuit Breaker pat- tern when invoking your service increases the scalability of your website because your service logic does not have to deal with a flood of repeated requests that all result in the same exception while there is an issue.

Leave a Reply

Your email address will not be published. Required fields are marked *