In this lesson · topic 2 of 7
Topic 2
Backoff, jitter, budgets and breakers
5 min read
Start with a timeout
Everything below depends on the client giving up. A call with no timeout can hang for as long as the dependency does, and it holds a thread, a connection and memory the whole time. Pick the timeout from what the user is willing to wait, and from the dependency’s normal latency at a high percentile. Too short and you retry work that was about to finish. Too long and a slow dependency ties up its callers. When a request passes through several services, send the remaining time along with it, so that a service deeper down does not keep working after the caller has stopped waiting.
Retry only what is safe
A retry sends the request again, so the operation must be safe to repeat. Reads are. A write that charges a card is not, unless it carries an idempotency key that lets the server recognise the repeat. Retry on errors that might clear up: a timeout, a connection reset, a 503. Do not retry on errors that will not, such as a bad request.
The tools
Immediate retry. Try again on the next tick. It is what a naive loop does. It adds the most load when the dependency has the least spare capacity.
Exponential backoff. Wait longer after each failure, typically doubling up to a cap. The dependency gets room to recover, and the load from a long-failing client falls off.
Full jitter. Pick each wait at random between zero and the backoff ceiling. Plain backoff has a problem when many clients fail at the same moment. They all wait the same time and come back in step, wave after wave. Randomising each wait spreads the retries over the whole window.
Retry budget. Allow retries only up to a fraction of recent requests. In the lab every new request earns a tenth of a token, and a retry costs a whole one. When the dependency fails, the budget runs out after a few retries and the rest fail at once. The extra load can never exceed that fraction, whatever the failure rate. The Google SRE book describes limiting retries per request and per process, and the gRPC retry design has a throttling rule of this kind.
Circuit breaker. A guard that watches recent outcomes. It is a small state machine with three states.
A circuit breaker through one incident
Step 1 / 5- state
- closed
- recent failures
- 0 of 20
Closed
Everything is healthy. Requests flow to the dependency and the breaker only watches how the recent ones went.
Failures pile up
The dependency slows down and attempts start to fail or time out. The breaker is still closed and still sending, because it needs enough evidence to act.
Open
Half of the recent attempts failed, so the breaker opens. Now requests fail at once, without being sent. The dependency gets no new load and can drain its backlog.
Half-open
After a cool-down the breaker lets a few probe requests through. Everything else still fails fast. This is a test of whether the dependency is back, made with as little load as possible.
Closed again
The probes succeeded, so the breaker closes and traffic returns. If a probe had failed, it would have gone back to open for another cool-down.
- Closed. Everything is healthy. Requests flow to the dependency and the breaker only watches how the recent ones went. state: closed; recent failures: 0 of 20.
- Failures pile up. The dependency slows down and attempts start to fail or time out. The breaker is still closed and still sending, because it needs enough evidence to act. state: closed; recent failures: 9 of 20.
- Open. Half of the recent attempts failed, so the breaker opens. Now requests fail at once, without being sent. The dependency gets no new load and can drain its backlog. state: open; traffic to dependency: none.
- Half-open. After a cool-down the breaker lets a few probe requests through. Everything else still fails fast. This is a test of whether the dependency is back, made with as little load as possible. state: half-open; probes allowed: 3.
- Closed again. The probes succeeded, so the breaker closes and traffic returns. If a probe had failed, it would have gone back to open for another cool-down. state: closed; probes passed: 3 of 3.
- Closed. Requests go through. The breaker counts failures.
- Open. Too many recent attempts failed. Requests fail immediately and are not sent, for a cool-down period.
- Half-open. After the cool-down, a few probes are let through. If they succeed, the breaker closes. If one fails, it opens again.
In the lab, one breaker stands for all the clients. It opens when half of the last 20 attempts failed, waits 20 ticks, then sends 3 probes. In production the thresholds and the cool-down are tuned per dependency.
What to watch
Two numbers tell you whether the policy is safe. Attempts per request says how much load retries add. Dependency load says whether the sum of new work and retries is above what the dependency can serve. The lab plots both.