Circuit Breaker Pattern

As the name suggests, the “circuit breaker” is a design pattern in software architecture, which has gain prominence in the distributed environment.

MCB
Miniature Circuit Breaker

Circuit Breaker Pattern in detail

Almost each one of us knows about the MCB – miniature circuit breaker that is used in our homes. Its function is to break the electric supply of a particular circuit – either of a room or of an entire house – if it detects some anomaly in the electric supply or usage. Once the anomaly is removed, we can restore earlier state and the electricity resumes.

In this design pattern, there’s a component named “Circuit Breaker” which does exactly same. It acts as a proxy for certain function/API calls. If the call fails beyond a certain threshold, this component starts returning an error to the caller, even without checking if the proxied function/API is working.

Unlike MCB, this being a software component, it can be made more intelligent. This Circuit Breaker component can have a threshold and timeout defined. Once the error rate crosses the threshold, the component starts returning an error to the caller for a timeout period. After that, it tries to call the proxied function/API few times. If successful, it can slowly resume the service over a period of time.

Circuit Breaker
Circuit Breaker diagram – Source: https://martinfowler.com/bliki/CircuitBreaker.html

But, wait, wouldn’t it cause a problem for the users?

It could. However, in the distributed architecture, failure is always a possibility. This design pattern teaches to embrace failure rather than denying the possibility of failure. The overall architecture can handle such failures gracefully. e.g. if a service providing recommendations for titles of movies or books, you could easily show something else or not show anything at all. That would still allow your users to use the overall application – albeit w/o some functionality. This would anyway be better than failing entire application.

Also, the component can include the monitoring feature as well. This can push data about the failure rate to a central dashboard, which will allow administrators to check the failed components and take corrective measures.

Example of Circuit Breaker Pattern

Netflix has implemented this pattern very effectively and has also released the library which provides various functionalities they are using internally – Hystrix. There are some very good articles available which are mentioned in the links below.

Related Links

Related Keywords

Microservices Architecture, Hystrix, API Gateway

 

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.