Timeouts and circuit breakers
Why one slow service can take a whole site down, and the shields that stop it: timeouts, careful retries, a circuit breaker, a bulkhead and a fallback.
The lesson in text
Click a paragraph to hear it.01Reviews took down booking
Friday, 21:10. A new version of the reviews service goes live: the part of StayHub that shows a hotel's stars and guest comments. It contains a slow database query. Calls that took 80 milliseconds now take 8 seconds.
Four minutes later, Omar, the engineer on call, sees every page timing out. Search. Login. And booking too.
Karim, the CTO, can't believe it. "Booking doesn't even use reviews. How can booking be down?"
In this lesson, you'll learn why a slow service is more dangerous than a dead one, and the shields that stop one sick feature from taking everything down: a timeout, careful retries, a circuit breaker, a bulkhead and a fallback.
02Waiters at the hatch
Think of a restaurant with twenty waiters. One evening, the dessert station gets slow. Every waiter who takes a dessert order walks to its hatch, and stands there, waiting.
Soon all twenty waiters are standing at the dessert hatch. Nobody takes a main-course order any more. The kitchen is fine. The restaurant has stopped anyway.
StayHub's web service works the same way. It has 200 workers, and every page, booking included, needs one. A hotel page asks reviews for its stars, 150 times a second.
On a normal day, each call takes 80 milliseconds, so about twelve workers are busy with reviews at any moment. At 8 seconds, you'd need 1,200. There are only 200, and they were all stuck after a little over a second.
Booking needed a worker too, and none came free. Then the app retried every page that timed out, up to three times, sending even more calls to a service that was already drowning.
You saw this with the slow pricing service: a dead service fails fast, but a slow one holds everyone hostage. Every waiting call holds something: a worker, a connection, memory. More workers wouldn't help. What was missing were limits.
03Bound every wait
The first limit is a timeout: the longest you'll wait for one call before you give up and free what you hold. At the restaurant: wait five minutes, then tell the table dessert isn't coming.
Set it from data, not by feel. Reviews' p99 is 80 milliseconds: 99 calls out of 100 answer faster than that. So the timeout is 300 milliseconds, about four times the p99, and far below the page's own budget of one second.
Too short, and healthy calls get cut off. Too long, and workers pile up again. At 300 milliseconds, even a broken reviews service can hold at most about 45 workers: 150 calls a second, each waiting less than a third of a second.
And never trust the default. Many HTTP clients wait for minutes, or forever, unless you set a limit. Friday's code had no timeout at all.
04Retry with care
Next, retries. A retry is great for a rare blip, like one lost message. But against a service that's overloaded, every retry is more weight on something already drowning.
So retry only what can succeed the second time: timeouts, and the server errors 502, 503 and 504, which usually mean busy or briefly unreachable. Never a 400 error: your request was wrong, and it will be wrong again. And only safe actions, like reading reviews, or actions protected by an idempotency key.
Then don't come back right away. Backoff means waiting longer before each new try: up to 100 milliseconds, then 200, then 400. Jitter means each client waits a random part of that, so a thousand clients don't all come back in the same instant.
| Retry | Random wait chosen in |
|---|---|
| 1st retry | 0–100 ms |
| 2nd retry | 0–200 ms |
| 3rd retry | 0–400 ms |
| 4th retry | 0–800 ms |
| 5th retry | 0–1,600 ms |
| 6th retry and later | 0–2,000 ms (the cap) |
Last, a retry budget: retries may add at most ten percent to the traffic. Google's guide to running its own systems recommends the same limit. For reviews, the policy is one retry, after a random wait of up to 100 milliseconds.
05The circuit breaker
A timeout still sends every call to a service you know is broken, and still waits 300 milliseconds each time. A circuit breaker stops calling it. At the restaurant, it's crossing desserts off the menu for a while.
It has three states. Closed: calls go through, and the breaker counts the failures. When half of the last 100 calls have failed, with at least 20 calls counted, it opens.
Open: no calls at all, for 30 seconds. Every hotel page gets the fallback instantly, and reviews gets some quiet time to recover.
Then half-open: a few test calls go through. One failure, and it opens again for another 30 seconds. Five successes, and it closes: normal service is back.
06Walls and a plan B
The last limit is a bulkhead: one dependency gets its own small pool of workers. At the restaurant: at most four waiters at the dessert hatch. The fifth one tells the table desserts are off tonight.
At StayHub, reviews may use at most 40 of the 200 workers. The 41st call doesn't wait: it gets the fallback at once. So 160 workers always stay free for booking, search and login, even if every other setting is wrong.
The fallback is what the guest sees instead: the hotel page without the reviews widget, or the cached average rating. A breaker without a fallback just turns a slow error into a fast error.
Maya, the product manager, pushes back. Pages without reviews sell about 15% worse. Why not wait two seconds and show them? Omar does the maths: 150 pages a second, each waiting two seconds, is 300 workers. There are 200. That's Friday again.
So the fallback is the cached rating: most guests only look at the stars anyway. And an alert fires when the breaker opens, so a quiet fallback can't hide a broken service for days. Replay Friday with all this, and guests just see hotel pages without fresh reviews.
07Thirty seconds, forty minutes
Three weeks later, someone adds retries everywhere, just to be safe. The app tries three times. The gateway in front of the web service, three times. The web service itself, three times. All with a fixed one-second wait.
Tuesday, 14:02. Reviews' database switches over to its backup: a planned thirty seconds of errors. When reviews comes back, it meets a wall of traffic.
Each layer multiplies the one above it. Three attempts, times three, times three: up to 27 calls for one guest action, all on the same one-second rhythm. They arrive in waves, knock reviews over again, and every failure schedules more retries.
| Who retries | Calls per guest action | Effect |
|---|---|---|
| No retries | 1 | Reviews sees the real traffic |
| One layer, 3 tries | 3 | Survivable with jitter and a budget |
| Two layers, 3 tries each | 9 | Already a wave |
| App × gateway × web service | 27 | Tuesday: a 30-second blip became 40 minutes |
That's a retry storm: every refused waiter coming back to the hatch at the same moment. The thirty-second blip lasted forty minutes, until someone switched the retries off by hand.
The fix: one layer owns retries, the web service, closest to reviews, with jitter and the ten percent budget. The app and the gateway fail fast. And the breaker gives reviews room to breathe.
08Your job as lead
Now you ask Claude Code to make hotel pages more resilient. It proposes a reviews client with three retries, one second apart, and the library's default timeout. "Retries make pages more resilient," it says.
Reject it. No timeout is Friday's bug, and fixed retries are Tuesday's storm. Ask for the policy instead: a 300 millisecond timeout, one jittered retry within a budget, a breaker, a bulkhead of 40 workers, and the cached rating as the fallback.
Then ask your questions. What's the timeout on every remote call, and what's that service's p99? Which layers retry, and how many calls can one guest action become? What does the guest see when the breaker opens, and who gets alerted?
And ask for the test that proves it: a fake reviews service that sleeps for eight seconds. Hotel pages must still answer in well under a second, without reviews, and booking must not notice a thing.
Let's recap. A slow service is worse than a dead one, because every waiting call holds a worker. Bound every wait with a timeout from real data. Retry rarely, with backoff, jitter and a budget, at one layer only. A breaker stops calling what's broken, a bulkhead keeps it in its own box, and a fallback keeps the page useful. Next: what to do when one step of a booking succeeds and the next one fails.
Check yourself
0/10 answered1Why is a slow service often more dangerous than a dead one?
2Hotel pages call reviews 150 times a second, and each call now waits 8 seconds, with no timeout. How many workers would you need, and what happens with 200?
3Reviews' p99 is 80 ms and the page has a budget of 1 second. Which timeout fits best?
4Which error is worth retrying?
5What does jitter add to backoff?
6The breaker has been open for 30 seconds and goes half-open. The first test call times out. What happens?
7Reviews is limited to 40 of the web service's 200 workers. What happens to the 41st reviews call?
8Maya wants hotel pages to wait up to 2 seconds for reviews, because pages without them sell 15% worse. What's the best answer?
9After a 30-second database blip, reviews stays down for 40 minutes. The app, the gateway and the web service each retry 3 times, one second apart. What's happening, and what's the fix?
10Claude Code proposes a reviews client with 3 retries, 1 second apart, and the library's default timeout, saying retries make pages more resilient. What do you do?
Save your progress — create a free account
Free. No card. Your finished lessons follow you to any device, and you can ask questions in the community.
Create my free accountEvery lesson is free while we build. Lock in €9/month for later.
The first 100 members keep the founding price for as long as they stay. Reserving is free: no card, nothing is charged.
See the founding offer