Circuit Breaker Design Pattern

Shyam Ramath
3 min readNov 23, 2020

--

Wikipedia says

Circuit breaker is a design pattern used in modern software development. It is used to detect failures and encapsulates the logic of preventing a failure from constantly recurring, during maintenance, temporary external system failure or unexpected system difficulties.

Advantage of Circuit Breaker Pattern

Circuit Breaker allows graceful handling of failed remote services. The pattern is very handy when all parts of our application are extremely decoupled from each other, and also when one component is out of service doesn’t mean the other parts will stop working.

Handle costly remote service calls in such a way that the failure of a single service/component cannot bring the entire system down, and we can reconnect to the service once it is restored.

These days it is very common for software systems to make remote calls to software running in different processes, probably on different machines across a network. One of the big differences between in-memory calls and remote calls is that remote calls can fail, or hang without a response until some timeout limit is reached. What’s worse if you have many callers on an unresponsive supplier, then you can run out of critical resources leading to cascading failures across multiple systems

The basic idea behind the circuit breaker is pretty simple. You wrap a protected function call in a circuit breaker object, which monitors for failures. Once the failures reach a certain limit, the circuit breaker trips, and all further calls to the circuit breaker return with an error, without the protected call being made at all. Usually, you might need some type of alert mechanism if the circuit breaker trips.

A service client should invoke a remote service via a proxy that functions in a similar fashion to an electrical circuit breaker. When the number of consecutive failures crosses a threshold, the circuit breaker trips, and for the duration of a timeout period all attempts to invoke the remote service will fail immediately. After the timeout expires the circuit breaker allows a limited number of test requests to pass through. If those requests succeed the circuit breaker resumes normal operation. Otherwise, if there is a failure the timeout period begins again

There are multiple implementations of the Circuit Breaker Design pattern in Java. Spring provides some perfect integration of this design pattern using Hystrix

Listed down some great implementation of Circuit Breaker pattern in Java. Take a look at those.

If you like this article please give a cheer !!

--

--

No responses yet