Circuit Breaker Design Pattern in 30 Seconds

Mirco | VerboseMode.dev
Javarevisited
Published in
2 min readNov 10, 2021
A physical circuit breaker.
Photo by Troy Bridges on Unsplash

In a microservice architecture, remote services may be unavailable for a long time. Retrying a request won’t help in this case.

The Circuit Breaker pattern offers a solution to this problem.

The CB has three states: closed, half-open, and open.

  • It starts in closed state. Your service executes all calls to the remote service.
  • CB switches to open if many requests fail. Your service starts a timer and does not execute any calls to the service. It returns a default answer or throws an exception instead.
  • CB switches to half-open when the timer is up. Your service executes a fraction of the calls. If all pass, it switches to “closed”. If one fails, it switches to “open” again and restarts the timer.

To summarize, the Circuit Breaker prevents the execution of calls which are doomed to fail. Look here for more information.

Learn more on Cloud Design Patterns!

Disclosure: Links marked with * are Amazon affiliate links. This means that, at zero cost to you, I will earn an affiliate commission if you click through the link and finalize a purchase.

--

--