System design lab · Episode 7

How a circuit breaker stops one failure from spreading

When a service breaks, the programs that call it can break too: they wait for answers that never come. A circuit breaker notices the failures and stops calling for a while, so calls fail fast instead of hanging. Here a breaker box stands next to the lane. Follow the lessons under the picture.

  1. Episode 1Load balancer
  2. Episode 2Cache
  3. Episode 3Rate limiter
  4. Episode 4Message queue
  5. Episode 5Database index
  6. Episode 6Replication and consensus
  7. Episode 7Circuit breaker
  8. Episode 8Backpressure
  9. Episode 9Retries and jitter
  10. Episode 10Consistent hashing
  11. Episode 11Partitioned log
  12. Episode 12Write-ahead log
  13. Episode 13Quorums
  14. Episode 14Unique IDs
  15. Episode 15The last ticket
  16. Episode 16Saga
  17. Episode 17Cache problems
  18. Episode 18Bloom filter
The test call while the breaker is half-openThe breaker box: handle up is closed, down is openThe tray in front: calls the breaker failed at once

Controls

Service
Breaker
Time

Want the details?

What is a circuit breaker?

Programs call other programs all the time: a shop calls the payment service, the payment service calls a bank. If one of them breaks and stops answering, its callers wait. While they wait, they hold resources: threads, memory, connections. Soon they cannot do anything else, and their callers start to wait too. One failure spreads through the whole system. This is called a cascading failure.

A circuit breaker sits in front of a call, like the breaker in the fuse box at home. When it sees too many failures, it trips and stops sending calls for a while. Calls fail at once instead of waiting.

How to read the bench
Part on the benchWhat it means in a real system
Box at the leftThe client: a program that calls another service.
Six lamps on the clientIts threads. A lit lamp is a thread that waits for an answer.
BallOne call.
Breaker box and handleThe circuit breaker. Up: closed, calls go through. Middle: half-open, one test call. Down: open, calls fail fast.
Flap and front trayCalls the open breaker failed at once.
Box at the rightThe service. Its lamp is lit while it works; a cross means it is broken, and calls wait in front of it.
MetersThe breaker's state, the busy threads, and the last 20 calls: answered, timed out, failed fast, or not started because no thread was free.
The three states
  • Closed: calls go through. The breaker counts failures.
  • Open: after too many failures, calls fail at once. Nothing is sent to the service.
  • Half-open: after a cool-down, one test call goes through. If it works, the breaker closes. If not, it opens again.
Why failing fast is better
  • The caller stays healthy. Its threads are free, so it can still do everything that does not need the broken service.
  • The user gets an answer. A quick “try again later”, or a saved older answer, is better than a page that loads forever.
  • The broken service can recover. A service that is struggling gets no new calls, instead of a pile of retries.
The numbers
  • A call every 0.45 seconds. The client has 6 threads.
  • A working service answers in about a second, counting the trip there. Usually 2 or 3 threads are busy.
  • A broken service: each call waits 4 seconds. That needs about 9 threads, so all 6 fill up and about one call in three cannot even start.
  • The breaker trips after 3 failures in a row, waits 5 seconds, then sends one test call.
In real systems
  • Libraries. Resilience4j (Java), Polly (.NET) and opossum (Node.js) give you a circuit breaker. Netflix made the idea well known with Hystrix.
  • Service meshes. Envoy and Istio can break circuits between services without any code change.
  • Timeouts first. A breaker only helps if calls have a timeout. A call with no timeout can wait forever.
  • Fallbacks. When the breaker is open, return something useful: a cached value, a default, or a clear error.
  • Retries with care. Retrying a failed call right away makes things worse for a struggling service. Retry a few times, wait longer each time, and stop when the breaker is open.
Words to know
Thread
One line of work in a program. A program has a limited number.
Timeout
How long a call waits for an answer before it gives up.
Cascading failure
One failure that causes failures in other parts of the system.
Trip
When the breaker opens because of too many failures.
Half-open
The breaker lets one test call through to see if the service works again.
Fail fast
Give up at once instead of waiting for something that will not work.
Fallback
What you return when the real answer is not available.