Designing for failure
The payment provider slows down at dinner peak and the menus stop loading. Timeouts, retries, breakers, bulkheads and load shedding, worked through with Little’s law.
This chapter lets you predict how a slow dependency turns into a full outage, and design the small set of mechanisms that stop it: timeouts, careful retries, circuit breakers, bulkheads, load shedding and planned degradation. Each one is a way of giving up on some work early so that the rest can finish.
The situation is a Friday in Dabba. At 20:25, near the top of the dinner peak, the payment provider’s latency rises. Its p99, normally around 300 ms, reaches 8 seconds. It is not down; most calls still succeed, slowly. By 20:31 Dabba’s checkout is failing, which is expected. What is not expected is that restaurant menus will not load, order tracking shows spinners, and riders cannot mark orders as picked up. None of those touch the payment provider.
We will work out why, then design the defences, then replay the evening with them in place. First, though, we need to say what “working” means, because every defence in this chapter trades some failures for others.
Decide what “working” means before the incident
A service level indicator (SLI) is a measurement of something users notice, and a service level objective (SLO) is the target for it. The first chapter set Dabba’s: placing an order answers within 1.5 s at the 99th percentile (not counting time in the customer’s UPI app), and 99.95% of order attempts between 11:00 and 23:00 succeed. The gap between the objective and 100% is the error budget: at a million orders a day, about 500 failed attempts a day before the objective is missed. That is a small budget, and an evening like the one below can spend a week of it in minutes.
SLOs matter here for one reason. During an incident the system will be making choices about which requests to fail, and those choices need a ranking. Dabba ranks them as follows: rider actions (pickup, delivery confirmation) above checkout, checkout above order tracking, tracking above browsing, browsing above recommendations. A rider stuck at a restaurant door is worse than a customer seeing a generic menu instead of a personalised one. That ranking will reappear in every mechanism below.
Why a slow payment provider stopped the menus
Little’s law says that the number of requests in progress equals the arrival rate times the time each one takes. It is the most useful single equation for this kind of incident.
The writes chapter already learned part of this lesson: a database transaction no longer waits on the payment provider, so the connection pool survives a slow provider. The request slots do not. Each of Dabba’s 30 API servers handles checkout, order tracking and the rider actions, with, say, 200 concurrent request slots. Take a festival evening near the design target of 150 orders a second: each server receives about 5 checkouts a second, each making a payment-creation call, and about 330 other requests a second (the 10,000 a second design target spread over 30 servers), each taking around 20 ms. Normally the payment calls occupy 5 × 0.3 s = 1.5 slots and the other requests 330 × 0.02 s ≈ 7. Under ten of 200 slots busy; plenty of headroom.
Now the payment calls take 8 seconds. They occupy 5 × 8 = 40 slots. Still fine, it seems. But customers whose checkout spins for 8 seconds tap “Pay” again, and the app has its own retry, so checkout arrivals triple: 15 a second, holding 120 slots. The extra calls load the provider further, its latency drifts towards 12 seconds, and the payment calls hold 180 slots. The remaining 20 slots must carry 330 other requests a second, which works only if each finishes in 60 ms; each request now also waits for a free slot, so its time grows, so it holds its slot longer. Within a minute every slot is waiting on the payment provider and every tracking and rider request is queued behind them. The slots were shared, so the slowness was shared.
There is a second, quieter effect. The menu service does not call the payment provider, but the API gateway in front of everything has its own connection pool and timeouts. As order-service requests hang, gateway connections hang with them, and the gateway’s capacity to serve menu requests shrinks too. Then health checks start failing on the busiest order-service instances, the load balancer removes them, and the survivors receive their traffic. The defences we already had (retries, health checks) pushed the failure outwards.
A timeout is a decision about what a request is worth
The first defence is to stop waiting. Most HTTP clients default to very long timeouts, sometimes none at all. A timeout should come from the budget of the request it serves. The order SLO allows 1.5 seconds at p99. The order service needs some of that for its own transaction, the app needs some for the network, and the payment-creation call gets the rest. With the provider’s normal p99 at 300 ms, a 500 ms timeout cuts off only the slowest few calls in a thousand and leaves room for one retry inside the 1.5-second budget.
Timeouts along a call chain must shrink as they go deeper. If the app gives up after 3 seconds but the order service waits 10 seconds for the provider, the order service keeps working (and holding a slot) for 7 seconds after anyone is waiting for the answer. That is pure waste at exactly the moment capacity is scarce. A simple way to enforce this is to pass a deadline with each request (“answer needed by 20:31:04.200”) and have each service refuse to start work that cannot finish in time.
A payment timeout raises a harder question than most: when the call times out, did the payment happen? We do not know. The provider may have charged the card and the response was lost, or the request may never have arrived. The order service must not tell the customer “payment failed”, because they may pay again and be charged twice. It records nothing new: the order simply stays in PAYMENT_PENDING, the app shows “confirming your payment”, and the webhook handler and reconciliation job from the writes chapter settle it, using the order ID that Dabba passed as the provider’s idempotency key. A timeout ends our waiting; it does not end the operation.
The Dabba app retries a failed checkout call up to 3 times. The order service retries a failed payment call up to 3 times. The payment client library, configured by a different team, also retries up to 3 times. The provider is overloaded and failing. How many calls reach the provider for one customer tap?
Show the worked answer
Twenty-seven. Each of the app’s 3 attempts produces 3 order-service attempts, and each of those produces 3 client-library attempts: 3 × 3 × 3. On an ordinary dinner hour of 42 orders a second, the provider receives up to about 1,130 calls a second from Dabba instead of 42, at the moment it is already failing because of load.
Retries look harmless at each layer, because each layer sees only its own three. The multiplication is only visible from above. The fix in the next section is to retry at one layer only, and to budget those retries.
Retry once, at one layer, with backoff, jitter and a budget
Retries are worth having. Many failures are brief: a connection reset, a pod restarting, one bad host behind the provider’s load balancer. A single retry turns most of these into successes the customer never sees. The danger is in retrying the wrong failures, too soon, at too many layers.
Dabba’s rules for the payment call are these. Only the order service retries; the app and the client library are configured not to, and the app shows a clear state instead of retrying silently. The order service retries only errors that suggest the request did not take effect or that a retry might succeed (connection failures, 503, 429), never a decline or a validation error. Every attempt carries the same idempotency key, so a retry after an unknown outcome cannot charge twice. And retries wait: exponential backoff with full jitter, meaning each wait is a random time between zero and a growing cap.
delay = random.uniform(0, min(cap, base * 2 ** attempt))
# base = 100 ms, cap = 1 s → attempt 0: 0–100 ms, 1: 0–200 ms, 2: 0–400 msThe jitter matters more than it looks. When the provider has a two-second blip, a few hundred Dabba requests fail in the same instant. Without jitter, they all retry after exactly 100 ms, arriving as a synchronised burst, then again at 200 ms, and so on. The provider sees spikes at the moments it is trying to recover. With jitter, the same retries are spread across each interval and arrive as a gentle increase. The AWS Builders’ Library article on timeouts and backoff shows this effect with simulations and is worth reading for the plots alone.
Finally, a retry budget. The order service tracks the ratio of retries to first attempts over the last few seconds and stops retrying once retries exceed, say, 10% of calls. When the provider is having a brief blip, the budget is not reached and retries help. When the provider is failing broadly, the budget caps Dabba’s extra load at 1.1× normal rather than 3× or 27×. Retries help when failures are rare and hurt when they are common, and the budget switches between those cases automatically.
Circuit breakers: stop calling what is not answering
Timeouts and budgets bound the damage per call. When the provider is clearly unhealthy, we would rather not call it at all for a while. A circuit breaker wraps the payment client and watches outcomes. While failures are rare it is closed and calls pass through. When the failure rate crosses a threshold (for example more than half of the last 50 calls failed or timed out, within 10 seconds) it opens: calls fail immediately, without touching the network. After a cool-down it lets a few probe calls through; if they succeed it closes, and if not it opens again.
An open breaker is only useful if failing fast leads somewhere better than failing slowly. For Dabba the breaker’s open state triggers a fallback for checkout. Dabba integrates a second payment provider for UPI and cards, used for a small share of traffic normally so that it stays warm and tested. When the primary’s breaker opens, new checkouts go to the secondary. If both are failing, there is a further option that the first chapter left out of scope on purpose: cash on delivery. Offering it only while both breakers are open, and only to customers whose account history makes it a reasonable risk, is a product decision we make here, and it has a price. Riders must carry and reconcile cash, a share of cash orders are refused at the door after the food is cooked, and the payout and support processes need a cash path that is otherwise unused. In exchange, “cannot order dinner” becomes “pay the rider”. Dabba accepts that trade for outages of the payment providers and for nothing else.
Breakers have their own failure modes. A breaker with too low a threshold opens on a handful of unlucky timeouts and sends traffic to the more expensive secondary for no reason. A breaker per process sees only that process’s calls; with 30 API servers each seeing about 1.4 calls a second in an ordinary dinner hour, each breaker has little data and they open at different times. Dabba sets thresholds from the provider’s normal error rate (a few tenths of a percent) with a minimum call count before the breaker may open, and exports breaker state as a metric so the 30 servers’ breakers can be seen together.
Bulkheads: keep the payment problem inside the payment pool
The menus failed because one pool of request slots was shared by work with very different dependencies. A bulkhead, named after the walls that divide a ship’s hull so that one leak does not sink it, gives each dependency its own bounded pool. In the order service on each server, payment calls get at most 30 concurrent slots. If those are full, a new checkout is refused immediately with “payments are slow, try again in a minute” (or routed to the fallback) instead of taking a slot from tracking and rider actions.
Where does 30 come from? Little’s law again. Normal need at the design target is 1.5 slots; 5 checkouts a second at a slow but survivable 2 seconds each need 10, and with the retry allowance about 11. Thirty leaves room for a bad minute, while guaranteeing that at least 170 slots stay available for everything else. The cost is that during a real surge of healthy checkouts, the pool may refuse some that would have succeeded. Sizing a bulkhead is choosing which mistake to make.
Bulkheads also apply at larger scale. Rider actions can run on a separate deployment of the order service, with its own instances and its own database connection pool, so that nothing a customer does can starve the riders. That doubles some operational work, which is why it is usually done only for the traffic ranked highest.
Shed load before the queues grow
Even with timeouts, retries, breakers and bulkheads, there are evenings when total demand simply exceeds capacity: a festival, a cricket final, a rain storm across Mumbai. If every request is accepted, every request waits in a queue, and when the wait exceeds the client’s timeout, the server finishes work nobody is waiting for. Throughput stays high while useful throughput (goodput) falls towards zero.
Load shedding refuses some work early, deliberately, so the rest completes in time. Each service watches a signal of overload, such as concurrent requests or the time requests spend queued, and when it crosses a limit, rejects the lowest-priority requests first with a fast 503. Dabba tags each request with a priority at the gateway from the ranking above, so the order service sheds recommendation and browsing calls before tracking, and tracking before checkout and rider actions. A rejected request costs microseconds; a request that times out after queueing for 5 seconds costs a thread for 5 seconds and still fails.
Degradation is the planned version of shedding: each feature has a cheaper mode that the system can switch to by flag when under pressure.
| Degraded mode | What the user loses | What it saves |
|---|---|---|
| Generic restaurant list instead of personalised ranking | relevance | the recommendation service’s whole load |
| Menus from a cache up to 10 minutes old | occasional stale price or item | most menu database reads |
| Rider location every 10 s instead of 5 s | smoothness of the tracking map | half of 10,000 writes/s |
| Customer map falls back from SSE to a poll every 30 s | smoothness of the live map | open connections and per-update fan-out |
| Pause new orders in one overloaded zone | ordering, for that zone only | rider and kitchen capacity for orders already placed |
Each mode is built and tested in advance. A degraded mode invented during an incident is new, untested code deployed under pressure, which is how one incident becomes two. Dabba runs each switch in production on a quiet afternoon every quarter to check that it still works and still saves what it claims.
The same Friday, with the defences in place
Replay the evening. At 20:25 the provider’s latency climbs. Payment calls start hitting the 500 ms timeout; those orders stay in payment-pending and the reconciliation job settles them within a couple of minutes, so no customer is told “failed” for a charge that went through. Retries rise, reach the 10% budget within seconds, and stop. The app, no longer retrying on its own, shows “confirming payment”.
At 20:26 the bulkhead fills: 30 payment calls in flight on each server. New checkouts beyond that are refused quickly. Tracking and rider actions keep their own slots and do not notice. Menus load normally, because nothing is holding gateway connections for 8 seconds any more. At 20:27 the breaker’s failure rate crosses its threshold and it opens; new checkouts go to the secondary provider. Checkout latency is slightly higher and a few percent of customers whose saved payment method only works with the primary are asked to choose another. At 20:40 the breaker’s probes succeed; it closes and traffic returns to the primary.
The cost of the evening: some checkouts refused during the first minutes, a higher transaction fee on the secondary for fifteen minutes, and a few hundred payments left pending for a minute or two before reconciliation settled them. The evening spent several days of error budget, which is what the budget is for. Without the defences, the cost was the whole of Dabba for the same fifteen minutes.
When these tools hurt
Each mechanism adds a new way to fail. A fallback to a secondary provider is a second integration that must be kept working; one that has not carried real traffic for months will fail when it is needed, which is why Dabba sends it a small share all the time. Load shedding thresholds set too low refuse traffic on a normal busy evening. Breakers that open on a dependency’s brief blip cause more disruption than the blip. Timeouts set from guesses rather than measured latency either never fire or fire on healthy calls.
The principle for when not to add them is proportion. A service with one dependency and modest traffic needs timeouts and a single retry, not a breaker and three pools. Add each mechanism when a specific failure in this chapter’s style (walk it through, step by step, with Little’s law) shows that it would have changed the outcome. Revisit the numbers after each incident: the thresholds that were right at 10 cities will be wrong at 50.
This chapter dealt with one dependency failing while Dabba itself was healthy. The next chapter, on multi-region design, asks the larger question: what if the whole region Dabba runs in becomes unavailable, and is it worth paying to survive that?
Free in full. The chapters on SLOs, error budgets and cascading failure repay reading even outside SRE.
The practical companion — how to actually set an SLO rather than admire the idea of one.
Eighteen short observations about failure. Ten minutes, and it reframes every postmortem you write after it.