LESSON 04 · Timeouts and breakers

Stop slow work from spreading

A timeout bounds one wait, a circuit breaker stops calling a failing dependency, and load shedding refuses work a server cannot finish in time.

SYSTEM VIEW

The failure path

Diagram view
Threads held—
Breaker—
Pages—

Watch a hanging dependency drain a page server’s threads, then the timeout, breaker and shedding that contain it.

  1. The Page server has 200 threads and calls Recommendations 100 times a second. Recommendations hangs and no timeout is set, so each call holds its thread. The pool is gone in 2 s, and pages that never needed recommendations stop too.

    Pool
    200 threads
    Gone in
    200 ÷ 100/s = 2 s

    What changed

    • Recommendations hangs
    • Each call holds a thread
    • 200 of 200 threads held
  2. The Timeout gives up on each Recommendations call after 1 s and frees its thread. At 100 calls a second, that caps the threads held at about 100, and the Page server stays up. Set timeouts from measured latency, and pass the remaining deadline downstream.

    Held at most
    100/s × 1 s = 100
    Deadline
    passed downstream

    What changed

    • Timeout: 1 s per recs call
    • About 100 of 200 threads held
    • Pages served again
  3. Failures pass the Circuit breaker’s threshold and it opens. Calls now fail at once without touching Recommendations, which gives it room to recover, and no thread waits. The fallback renders the page with no recommendations.

    Open
    fail fast, no call
    Fallback
    page, no recs

    What changed

    • Circuit breaker, open
    • Calls fail fast
    • Fallback: page without recs
  4. After a pause the Circuit breaker goes half-open and lets a few trial calls through. They succeed, so it closes, and pages carry recommendations again. Track failures per dependency or shard, so one failing shard doesn’t open the breaker for all.

    Half-open
    a few trial calls
    Track failures
    per dependency or shard

    What changed

    • Breaker half-open
    • Trial calls succeed
    • Breaker closed: recs back
  5. Load shedding is the server’s own defense. The Load shedder lets in what the Page server’s 200 threads can finish and rejects the rest early with a fast 503, so accepted requests still finish within their deadline. It decides from in-flight count or queue delay.

    In flight
    at most 200
    Decide from
    in-flight or queue delay

    What changed

    • Timeout, breaker, recs, out of view
    • Load shedder
    • 200 in flight, the rest get 503
  6. Shed by priority, so checkout survives when recommendations do not: a recommendations request gets a fast 503 while checkout goes through. The costs: rejected users see errors, thresholds need tuning, and a rejection must be far cheaper than the work it avoids.

    Kept
    checkout
    Shed first
    recommendations

    What changed

    • Priority: checkout first
    • Recommendations get 503
    • Rejected users see errors

Space plays · ← → step · Home resets

THE IDEA

How it works

A slow dependency can do more harm than a dead one, because callers hold threads and connections while they wait. For example, a page server has 200 threads and calls a recommendations service 100 times per second. If that service hangs with no timeout set, the pool is exhausted in 2 seconds, and pages that never needed recommendations stop too. A 1-second timeout caps the threads held by this call at about 100 (100 per second times 1 second), and the server stays up. Set timeouts from measured latency, and pass the remaining deadline downstream, or lower services keep working on requests nobody is waiting for.

With a circuit breaker, the caller tracks recent failures, and past a threshold the breaker opens: calls fail immediately without touching the dependency, which also gives it room to recover. After a pause it goes half-open and lets a few trial calls through. If they succeed, it closes. Pair it with a fallback where one exists, such as rendering the page with no recommendations. Notification System uses a breaker for unhealthy providers. Track failures per dependency or shard, so one failing shard does not open the breaker for all.

Load shedding is the server’s own defense. It rejects excess requests early with a fast 503, so accepted requests still finish within their deadline. Shed by priority, so checkout survives when recommendations do not, and decide from in-flight count or queue delay. The costs: rejected users see errors, thresholds need tuning, and a rejection must be far cheaper than the work it avoids.

DECISION POINT

A product page calls inventory, which it cannot render without, and recommendations, which are optional. Recommendations normally answers in 80 ms at p99 and has started hanging. How do you treat each dependency?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • A remote call with no timeout lets a slow dependency take its callers down with it.
  • A timeout limits one wait, a breaker stops calling for a while, and shedding refuses work at the server.
  • A fast rejection protects the requests a server has already accepted.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

A server has 50 threads and calls a service 40 times per second. That service starts hanging. Which timeout keeps this one call from using every thread?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.