What is Circuit Breaker?

circuit breaker is an automatically operated electricalswitch designed to protect an electrical circuit from damage caused by excess current from an overload or short circuit. Its basic function is to interrupt current flow after a fault is detected. Unlike a fuse, which operates once and then must be replaced, a circuit breaker can be reset (either manually or automatically) to resume normal operation. — Wikipedia.

But, wait….!

This is a Software Programming topic, and why I’m bring something related to Electrical Technology into this topic?

Because the way we apply Circuit Breaker Pattern in Software Programming is very similar. And I just want to bring something that you are familiar with in order to make the topic easier to understand.

Back to the definition of Circuit Breaker in Software Programming…

Circuit Breaker pattern 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. —Wikipedia

Bear in mind that, It’s not available to prevent the broken service, but not to make it worse than it was. While other design patterns are born for making our solution easy to maintain, maximize reusability, better performance… then Circuit Breaker is the one for “when sh*t happens”.

image source: me.me

Why Circuit Breaker?

Back to several years ago — the time I started digging and applying Microservices architecture (MSA), MSA was something very new with not only me but also other developers. Nowadays, you can easily find technical articles, sample projects and case studies about MSA as it’s becoming very popular. There is a fact which can’t be denied that MSA brings so many advantages and benefits, it also makes our solution brittle because a request from user will need to call multiple micro-services. It replaces the in-memory calls of a Monolithic architecture with remote calls across the network, which works well when all services are up and running. But when one or more services are unavailable, slow network connection or timeout… it results in a cascading failure across the application.

And, Circuit Breaker is here to help us can build a fault-tolerant and resilient system that can survive gracefully when key services are either unavailable or have high latency. Circuit Breaker pattern enables an application to detect whether the fault has been resolved. If the problem appears to have been fixed, the application can try to invoke the operation.

Circuit Breaker States

Image for post

Photo by Blake Weyland on Unsplash

Although the name “Circuit Breaker” is mimicked from Electrical Technology, in software programming, Circuit Breaker can do much more than turning on/off the electricity (tripping).

  • Both of them can detect when there is a problem and automatically turn on. But while Circuit Breaker in software programming can turn off automatically, then Circuit Breaker in Electrical Technology couldn’t.
  • Besides having fully opened and fully closed state, Circuit Breaker in software programming can have an intermediate state, called “Half-open” where an application can operate at degraded functionalities.

#polly #software-architecture #design-patterns #circuit-breaker #microservices

What Is Circuit Breaker and How to implement It With .NET Core?
2.50 GEEK