Backend / Circuit breaker pattern

Circuit breaker pattern

How a service protects itself from another one that has stopped answering. Seven steps follow a breaker through closed, open and half-open, and show how it stops a failure from spreading.

Intermediate7 steps~7 min
Shop — the caller, needs payments per order
Breaker — a switch on the line, counts failures
Payments — the service being called
// THE SHORT VERSION

In 30 seconds

When a service stops answering, everything that calls it wastes time waiting. A circuit breaker is code wrapped around those calls that notices the failures and stops making them.

After a set number of failures it opens: calls fail at once, without waiting. After a pause it goes half-open and lets one trial call through. Success closes it again; failure reopens it.

Failing fast keeps the caller responsive and gives the sick service room to recover, so one fault does not take down every service in the chain.

Circuit breaker pattern, step by step

STEPS01 / 07

The numbers are examples. Real breakers often trip on a failure rate over a time window instead of a simple count, and may allow several trial calls. The name comes from the electrical safety switch.

STEP 01 / 07 Waiting on a sick service
THE MATH
call payments wait 10 s ... timeout
  1. Step 1: Waiting on a sick service. A shop service calls a payments service for every order. Payments stops answering. Each call now waits its full 10 seconds before giving up, and new orders keep sending more calls. The shop’s workers are soon all busy doing nothing but waiting.
  2. Step 2: Put a breaker on the line. A circuit breaker is a small piece of code that every call to payments goes through. Normally it is closed, like a switch that lets current flow: calls pass, answers come back, and the breaker quietly counts failures. A success sets the count back to zero.
  3. Step 3: Failures reach the threshold. Payments starts timing out. Each failed call adds one to the count. The breaker has a threshold, here 5 failures in a row. When the count reaches it, the breaker decides the service is down and that more calls would only waste time. It trips.
  4. Step 4: Open: fail fast. The breaker is now open, like a switch that cuts the current. Calls are refused on the spot with an error, in about a millisecond instead of 10 seconds, and nothing is sent to payments. The shop can react at once, for example by asking the customer to retry shortly.
  5. Step 5: Half-open: one trial call. The breaker cannot stay open for ever, or it would never notice a repair. After a set time, here 30 seconds, it becomes half-open: it lets one real call through as a trial and keeps refusing the rest. One call is enough to test the service without flooding it.
  6. Step 6: The trial decides. If the trial call succeeds, the breaker closes, the count returns to zero and traffic flows again. If it fails, the breaker opens for another 30 seconds and tries again later. These three states and four moves are the whole pattern.
  7. Step 7: Stopping the cascade. The real danger of a hung service is that it spreads. Its callers fill up with waiting calls and stall, then their callers stall, until one fault has taken the site down: a cascading failure. A breaker stops it at the first link. The shop keeps answering, with payments marked unavailable.
VALUES SO FAR
wait10 s per callproblem
state····normal
trip at····threshold
state····fail fast
refusal····fast
retry····timeout
state····trial
trial ok····recover

Spotted a mistake on this page?