05. Reliability / Lesson 3 of 4

Timeouts & Retries

Give up on slow calls, try again the safe way, and stop a failure from spreading.

  • 10 min
  • Intermediate
  • 10 steps
  • Calls in flight200 of 200 (bad)
  • Error %100% (bad)
UsersApp server⚠ 200/200 threads stuckRecommendations⚠ hung
  • Request
  • Failed request
  • Response
Step 1/10

Step 1 of 10: A call that never comes back

Read every step as text
  1. Step 1: A call that never comes back

    The app takes 100 requests a second and has 200 worker threads. 20 of those requests a second call a recommendations service. It normally answers in 50 ms.

    One day it hangs: it accepts the call and never replies. Each such request keeps a thread waiting forever. After 200 ÷ 20 = 10 seconds all 200 threads are stuck, and even requests that never touch recommendations get no answer.

    A slow dependency is often worse than a dead one: a dead one fails fast, a slow one holds your threads.

  2. Step 2: A timeout: stop waiting after a set time

    Every call to another service needs a timeout. Say we pick 1 second: far above the usual 50 ms, so slow-but-healthy replies still count, but short enough to protect the threads.

    Now a hung call holds a thread for 1 s, not forever. Calls in flight are 20 × 1 s = 20, far below 200, so the app keeps serving. Only the 20% of requests that need recommendations fail.

    Set the timeout from real data, a little above the slowest normal reply (the p99), and give the whole request a deadline too.

  3. Step 3: Retry: many failures are only a blip

    A packet is lost, a server restarts, a connection resets. Say 5% of calls fail this way, and a second try nearly always works. With one retry, a call fails only if both tries do: 0.05 × 0.05 = 0.0025, which is 0.25%.

    Of all user requests, the failing share falls from 1% to 0.05%. The cost: about 5% more calls, and a retry can leave a call waiting a little longer.

    Retry only errors that may pass by themselves (timeouts, 503). Never retry 400 or 404: the answer will not change.

  4. Step 4: When everyone retries, a sick service dies

    Now the database is overloaded and every call times out. Each of the 3 hops (app, recommendations, ranking) retries 3 times. One call at the top becomes 3^3 = 27 attempts at the bottom.

    The database gets 20 × 27 = 540 attempts a second, not 20. The extra load keeps it down, so it cannot recover: a retry storm.

    Retry at one layer only, and give a retry a budget (for example, at most 10% extra calls).

  5. Step 5: Backoff: wait longer after each failure

    Take 1,000 clients waiting on a service that is down for 10 seconds, each call timing out after 1 s. If they retry at once, there are always about 1,000 calls in flight. With exponential backoff, each client waits 1 s, then 2, 4, 8, doubling each time.

    Their tries start at 0, 2 and 5 seconds, then 10. In the first 10 seconds that is 3 tries each, so about 300 calls in flight on average instead of 1,000. The service gets room to recover.

    Cap the wait (for example at 30 s) and the number of tries, and give up when the user's own deadline has passed.

  6. Step 6: Jitter: add randomness so clients do not move together

    Backoff has a flaw: the 1,000 clients all failed together, so they all retry together. The service recovers, then 1,000 calls arrive in the same instant. It can run 200 at once, so 1,000 − 200 = 800 fail, 80%.

    With jitter, each client waits a random time, say anywhere from 0 to the full wait. The calls spread over 1 s: 1,000 a second × 50 ms each is only about 50 in flight, and none are rejected.

    Backoff spreads one client's tries in time. Jitter spreads different clients apart. Use both.

  7. Step 7: Make a retry safe with an idempotency key

    A payment call times out. Did the money move or not? If the reply was lost after the charge succeeded, a plain retry charges twice. Say 0.5% of 10,000 daily payments lose the reply: that is 50 double charges a day.

    The client sends a unique Idempotency-Key with the request. The server saves the result under that key; a repeat with the same key returns the saved result and charges nothing more.

    Reads and PUTs are safe to repeat. Creating things (POST) needs a key. Keep saved keys for a day or so.

  8. Step 8: Circuit breaker: stop calling a service that is down

    Even with timeouts, every request still spends a second finding out the service is down. A circuit breaker watches the results. Say it opens when 5 of the last 10 calls fail.

    While open, calls fail at once, without touching the service. After 30 seconds it goes half-open and lets one trial call through: success closes it again, failure keeps it open. Calls in flight to the service drop to 0.

    Pair it with a fallback, such as a popular-items list, so users see something useful instead of an error.

  9. Step 9: Bulkheads: separate pools so one leak cannot sink the ship

    Give each dependency its own limit, like the walls inside a ship. Of the 200 threads, recommendations may use 50; the other 150 serve everything else.

    If recommendations hang, they fill their 50 threads in 50 ÷ 20 = 2.5 seconds, and further recommendation calls are refused at once. The other requests never wait, so only the 20% that need recommendations fail.

    Timeouts, retries with backoff, breakers and bulkheads work as a set: each covers a gap the others leave.

  10. Step 10: Timeouts and retries in one picture

    Always set a timeout. Retry only what is safe and likely to work, with growing, random waits, and an idempotency key for anything that changes data.

    Add a circuit breaker to stop calling what is down, and bulkheads so one slow service cannot use every thread. Failures then stay small instead of spreading.