Skip to content
Lesson 52

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.

Workers held by reviews calls
Normal day: 80 ms per call12 workers
With a 300 ms timeout45 workers
Friday: 8 s per call, no timeout1,200 workers
The thin line marks the 200 workers the web service has.
Calls waiting at once = calls per second × seconds each call waits. At 150 calls a second: 150 × 0.08 = 12, 150 × 0.3 = 45, 150 × 8 = 1,200. The web service has 200 workers for every page, so on Friday they were all stuck after 200 ÷ 150 ≈ 1.3 seconds.

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.

Backoff with full jitter
RetryRandom wait chosen in
1st retry0–100 ms
2nd retry0–200 ms
3rd retry0–400 ms
4th retry0–800 ms
5th retry0–1,600 ms
6th retry and later0–2,000 ms (the cap)
Each window doubles (100 ms × 2 for every retry, capped at 2 s), and each client waits a random time inside it, so retries spread out instead of arriving together. For reviews, StayHub allows a single retry, so its only wait is a random 0–100 ms.

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.

The circuit breaker's three states
Closed
Calls go through; failures are counted.
Half of the last 100 calls fail (at least 20 counted) → open.
Open
No calls for 30 seconds; every page gets the fallback at once.
After 30 seconds → half-open.
Half-open
A few test calls go through.
One failure → open for another 30 s. Five successes → closed.
The breaker protects the caller (no more 300 ms waits on a broken service) and the service (quiet time to recover). It needs a fallback and an alert, or guests get fast errors and nobody knows.

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.

A bulkhead: 40 of 200 workers for reviews
Reviews
≤ 40
Booking · search · login
160 always free
Reviews calls may use at most 40 workers. The 41st call gets the fallback immediately instead of waiting. Whatever happens to reviews, 160 workers stay free for booking, search and login.

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.

Retries multiply across layers
Who retriesCalls per guest actionEffect
No retries1Reviews sees the real traffic
One layer, 3 tries3Survivable with jitter and a budget
Two layers, 3 tries each9Already a wave
App × gateway × web service27Tuesday: a 30-second blip became 40 minutes
Every layer that retries multiplies the calls of the layer above. With fixed waits they also land at the same moment. The fix: one layer owns retries (the web service, closest to reviews), with jitter and a 10% budget; the others fail fast.

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 answered
  1. 1Why is a slow service often more dangerous than a dead one?

  2. 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?

  3. 3Reviews' p99 is 80 ms and the page has a budget of 1 second. Which timeout fits best?

  4. 4Which error is worth retrying?

  5. 5What does jitter add to backoff?

  6. 6The breaker has been open for 30 seconds and goes half-open. The first test call times out. What happens?

  7. 7Reviews is limited to 40 of the web service's 200 workers. What happens to the 41st reviews call?

  8. 8Maya wants hotel pages to wait up to 2 seconds for reviews, because pages without them sell 15% worse. What's the best answer?

  9. 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?

  10. 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?

Free account

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 account
Founding members

Every 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

Questions from the community

All questions →