Circuit breaker in Java

Ahmet DELLAL
3 min readMay 29, 2021

--

A problem that I encountered on a project I was working on at home at West-World was constantly occupying my brain, and I had to gasp myself and get to work as soon as possible to solve this problem. Having said that it is another subject that I would enjoy learning, I went to my study late.
I turned on my yellow nightlight, which is said to damage brain cells. I sorted the music in my epic fantasy list that I set to play low on Spotify (I created my cat). tones I started the weighted concentration album. I took a sip of my hot coffee and started searching the browser.
First of all, I tried to understand what circuit breaker means from the sources I found.

It’s 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 a 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 very simple. You wrap a protected function call in a circuit breaker object, which monitors for failures. Once the failures reach a certain threshold, 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’ll also want some kind of monitor alert if the circuit breaker trips.

Different States of the Circuit Breaker

The circuit breaker has three distinct states: Closed, Open, and Half-Open:

  • Closed — When everything is normal, the circuit breaker remains in the closed state and all calls pass through to the services. When the number of failures exceeds a predetermined threshold the breaker trips, and it goes into the Open state.
  • Open — The circuit breaker returns an error for calls without executing the function.
  • Half-Open — After a timeout period, the circuit switches to a half-open state to test if the underlying problem still exists. If a single call fails in this half-open state, the breaker is once again tripped. If it succeeds, the circuit breaker resets back to the normal, closed state.

What would trigger this behavioural change, also known as graceful degradation? A mechanism should be in place to automatically adapt to predictable circumstances maintaining the service operational with limited functionalities. If an application is calling a web API, it is easy to predict that the API might be unavailable at some point in time and the application should be ready to switch to a plan B.

The Java code below is a simple implementation of the circuit breaker pattern to change the application behaviour if the API call is not successful after a few attempts. It wraps the API call, watching for failures, and once the failures reach a certain threshold, the circuit opens, no further calls are made at that point, and the flow changes to do something else. Follow the documentation in the code to understand the logic.

--

--

Ahmet DELLAL
Ahmet DELLAL

No responses yet