What is a circuit breaker in system design? Closed, open and half-open explained

When a service you depend on starts failing, calling it again and again only makes things worse. A circuit breaker notices the failures, stops the calls for a while, and then carefully tries again. Here is how it works, its three states, and how to tune it.

A fuse box labelled main supply with four switches: lights, fan, kitchen and fridge. The kitchen switch is red, off and marked tripped, and a toaster with a yellow spark is plugged into it. The other three switches are green and on. Caption: one switch trips, everything else stays on.
Fig. 1 — A fuse box trips one switch to stop a fault spreading, so the rest of the house keeps its power. A circuit breaker in software does the same for a failing service.

Every home has a fuse box or a set of circuit breakers. If your toaster develops a fault and starts drawing too much current, one switch trips and cuts power to that circuit. The wires don’t overheat, the house doesn’t catch fire, and your lights and fridge on other circuits keep working. Once the toaster is fixed, you flip the switch back on.

Software systems borrow this idea, and even the name. A circuit breaker sits around calls to another service and “trips” when that service keeps failing. This guide explains why that helps, how its three states work, and how to set one up sensibly.

The problem: failures that spread

Say your checkout service calls a recommendations service to show “You might also like…”. One day, recommendations becomes very slow: each call hangs for 30 seconds before failing.

Without protection, here’s what happens:

  1. Every checkout request waits 30 seconds for recommendations.
  2. Checkout’s threads and connections fill up with these waiting requests.
  3. New checkout requests can’t get a thread, so checkout itself starts failing.
  4. Anything that calls checkout starts failing too.

One slow, unimportant service has taken down checkout, the most important page you have. This chain reaction is called a cascading failure, and it’s one of the most common ways large systems go down. Our guide to designing for failure covers it in more depth.

On top of that, all those calls keep hammering the struggling recommendations service, making it even harder for it to recover.

What is a circuit breaker?

A circuit breaker is a small piece of logic wrapped around calls to another service. It watches how those calls go. When too many fail, it stops making the calls for a while and fails fast instead, usually returning a sensible fallback. After a pause, it lets a few trial calls through to see if the service has recovered.

The pattern was popularised by Michael Nygard in his book Release It! (2007), and libraries such as Resilience4j (Java), Polly (.NET) and the older Netflix Hystrix made it common in microservices.

The three states

A state diagram with three circles. Closed, in green: calls go through and failures are counted. An arrow labelled too many failures, for example 50 percent of the last 20 calls, leads to Open, in red: calls are not made, fail fast with a fallback. An arrow labelled wait time passes, for example 30 seconds, leads to Half-open, in yellow: let a few trial calls through. From half-open, an arrow labelled trial calls succeed goes back to closed, and an arrow labelled a trial call fails goes back to open.
Closed means normal. Open means stop calling. Half-open means test carefully. The thresholds shown are example values you tune.

1. Closed (normal). Calls go through to the service as usual. The breaker quietly counts results: successes, failures, and calls that were too slow. (The name comes from electricity: a closed circuit lets current flow.)

2. Open (tripped). When failures cross a threshold (for example, more than 50% of the last 20 calls failed or timed out), the breaker opens. Now calls aren’t made at all. Each one fails immediately, in microseconds, and your code returns a fallback instead. The struggling service gets a break from traffic.

3. Half-open (testing). After a set wait (say 30 seconds), the breaker lets a small number of trial calls through. If they succeed, the service seems healthy again and the breaker closes. If they fail, it opens again and waits another round.

What does the user see? Fallbacks

Failing fast is only half the story. The other half is deciding what to return when the breaker is open:

  • A default. No recommendations? Show “Popular this week” from a cache, or simply hide that section.
  • Cached data. Show the last known value, marked as possibly out of date.
  • A clear error. For things that truly need the service, like a payment, fail quickly with a clear “Please try again in a minute”, instead of a 30-second spinner.
  • A queue. Save the request and process it later, using a message queue.

This is called graceful degradation: the product gets a little less rich, but it keeps working.

Circuit breakers and other resilience tools

A circuit breaker works best alongside a few other patterns, all covered in resilience patterns:

Tool What it does How it relates to the breaker
Timeout Stops waiting after a set time Slow calls count as failures; without timeouts, the breaker sees nothing
Retry Tries a failed call again, with growing pauses Good for one-off blips. The breaker stops retries piling onto a service that is truly down
Bulkhead Gives each dependency its own limited pool of threads Stops one slow service from using up everything, even before the breaker trips
Rate limit Caps how many calls are sent Protects the other service from overload

Tuning a circuit breaker

The defaults in libraries are a starting point. The main settings:

  • Failure threshold: the share of failed calls that trips it (for example 50%).
  • Minimum number of calls: don’t trip on 1 failure out of 2 calls; wait until there are enough calls (say 20) to judge.
  • Slow-call threshold: treat calls slower than, say, 2 seconds as failures, because slowness is often the first sign of trouble.
  • Open wait time: how long to stay open before testing (seconds to a minute is common).
  • Trial calls in half-open: how many test calls to allow.

Watch your breakers in your dashboards. A breaker opening is a useful alert in itself: it tells you exactly which dependency is in trouble. See observability.

Common misconceptions

“A circuit breaker fixes the failing service.” It doesn’t. It protects your service and gives the other one room to recover. Someone still has to fix the cause.

“Open means everything is broken.” Open means “stop calling for now”. With a good fallback, users may barely notice.

“One breaker for the whole app is enough.” Use one per dependency (and sometimes per operation), so one bad service doesn’t block calls to healthy ones.

Key takeaways

  • A circuit breaker stops calling a service that keeps failing, and fails fast instead.
  • It prevents cascading failures and gives the struggling service room to recover.
  • Three states: closed (normal), open (stop calling), half-open (test with a few calls).
  • Pair it with a fallback so users get a useful answer, and with timeouts, retries and bulkheads.
  • Tune the thresholds, use one breaker per dependency, and alert when one opens.

Frequently asked questions

What is a circuit breaker in simple words?

It’s a safety switch around calls to another service. If that service keeps failing, the switch trips and your app stops calling it for a while, answering quickly with a fallback instead of waiting and failing.

What are the three states of a circuit breaker?

Closed, where calls go through normally; open, where calls are blocked and fail fast; and half-open, where a few trial calls test whether the service has recovered.

Why is it called a circuit breaker?

It’s named after the electrical circuit breaker in a building, which cuts power to one circuit when there’s a fault so the problem can’t spread. In both cases, “closed” means things flow and “open” means they are cut off.

What is the difference between a retry and a circuit breaker?

A retry tries a failed call again, which helps with brief glitches. A circuit breaker stops calling altogether when failures continue, which protects both sides during a real outage. They are usually used together.

Which libraries provide circuit breakers?

Resilience4j is popular in Java and Spring, Polly in .NET, and many service meshes and API gateways, such as Envoy and Istio, offer circuit breaking too. Netflix Hystrix was an early, influential library that is now in maintenance mode.

Comments

LEAVE A COMMENT
No sign-up. Comments appear after a quick review.