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 bench | What it means in a real system |
|---|---|
| Box at the left | The client: a program that calls another service. |
| Six lamps on the client | Its threads. A lit lamp is a thread that waits for an answer. |
| Ball | One call. |
| Breaker box and handle | The circuit breaker. Up: closed, calls go through. Middle: half-open, one test call. Down: open, calls fail fast. |
| Flap and front tray | Calls the open breaker failed at once. |
| Box at the right | The service. Its lamp is lit while it works; a cross means it is broken, and calls wait in front of it. |
| Meters | The 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.