- [Instructor] We just saw that
our website loads indefinitely,
waiting for a response from a faulty service.
And this might take down the whole conference app.
So how can we make sure that the app keeps running
in such a scenario?
One way to solve that is a so-called circuit breaker.
Let's see how it works.
Circuit breakers work like an electrical circuit breaker.
First of all, they can be either closed,
this means that the circuit is interrupted.
Let's look at the respective sequence diagrams
to see what this means for our application.
In closed state, the circuit breaker will simply allow
requests to the server.
In open state, the circuit breaker will not try
to connect to the service
and will return back to the caller immediately.
Now the question is,
which events should change the state of the circuit breaker?
Let's look at the state machine behind it.
So we have a closed state and an open state,
and additionally, and differently to electrical circuits,
we also have a half-open state.
As long as the request succeeds,
the circuit breaker stays in state closed.
Also, if the failure is below a defined threshold,
like an accepted number of failures,
it stays closed, it will just increment the failure count.
If a failure occurs and the threshold is reached,
the circuit breaker changes to state open.
After a defined cooldown period, we go into half-open.
This means we will let another request through.
If this request succeeds, we are back at closed state,
and we reset the failure count.
If it fails again, we go back to closed state
and reset the cooldown period,
or alternatively, increase it.
For such retry patterns,
an exponential backoff is often used.
This means that the cooldown period
will be increased with every failure.
So we might have first a wait time of one second,
then two, then four, then eight, you get it.
This might also mean that we have to reset
the cooldown period to its default,
once we go back from closed to open.
In the next video, we will build such a circuit breaker
into our application.