Handling Load — Backpressure, Shedding & Resilience Patterns
How systems survive overload and partial failure — queueing theory basics (Little's law, utilisation vs latency), timeouts, retries with backoff and jitter, retry budgets, circuit breakers, bulkheads, backpressure, load shedding and prioritisation, queue-based load levelling, autoscaling, thundering herds, graceful degradation, and cell-based architecture.
Queueing theory you need
| Utilisation | Latency multiplier (M/M/1) |
|---|---|
| 50% | 2× |
| 80% | 5× |
| 90% | 10× |
| 99% | 100× |
Little's law: in-flight requests = arrival rate × latency. Use it to size thread pools and connection pools: 2,000 req/s × 50 ms DB time = 100 concurrent DB connections needed.
The resilience toolbox
Timeouts and deadlines
Every network call needs a timeout. Set it from the dependency's latency distribution (e.g., a bit above p99.9), and propagate deadlines: if the user request has 800 ms left, downstream calls get less than that (gRPC does this natively). Without timeouts, a slow dependency exhausts every thread in the caller.
Retries with backoff and jitter
import random, time
def call_with_retry(fn, attempts=3, base=0.1, cap=2.0, deadline=None):
for attempt in range(attempts):
try:
return fn()
except TransientError:
if attempt == attempts - 1:
raise
sleep = random.uniform(0, min(cap, base * 2 ** attempt)) # "full jitter"
if deadline and time.monotonic() + sleep > deadline:
raise
time.sleep(sleep)
async function callWithRetry<T>(fn: () => Promise<T>, attempts = 3, base = 100, cap = 2000): Promise<T> {
for (let i = 0; ; i++) {
try {
return await fn();
} catch (e) {
if (i === attempts - 1 || !isTransient(e)) throw e;
const sleep = Math.random() * Math.min(cap, base * 2 ** i); // full jitter
await new Promise((r) => setTimeout(r, sleep));
}
}
}
Circuit breaker
Bulkheads
Isolate resources per dependency or per tenant (separate thread pools, connection pools, queues, even clusters) so one slow dependency or noisy tenant can't sink everything — like watertight compartments on a ship.
Hedged requests
For tail latency: if a request hasn't returned by the p95 time, send a duplicate to another replica and take the first response (idempotent reads only). Cuts p99 dramatically for ~5% extra load (Google's "The Tail at Scale").
Backpressure
When consumers are slower than producers, something must give: buffer (bounded!), slow the producer, or drop.
| Mechanism | Where |
|---|---|
| TCP flow control | transport |
| HTTP/2, gRPC flow control windows | RPC |
| Bounded queues that block or reject | in-process |
| Pull-based consumers (Kafka) + lag monitoring | messaging |
429 / 503 + Retry-After | APIs |
Reactive streams request(n) | libraries (Reactor, RxJava, Akka) |
Load shedding and prioritisation
Graceful degradation examples: serve the homepage from cache with stale data, hide recommendations, disable expensive search filters, switch to lower video bitrates, make writes async ("we'll email your receipt").
Queue-based load levelling
Put a durable queue between the bursty front and the capacity-limited back end (e.g., order processing, video encoding, third-party API with quotas). The API acknowledges with 202 Accepted; workers drain at a sustainable rate; autoscale workers on queue depth/age.
Autoscaling
| Type | Signal | Notes |
|---|---|---|
| Reactive (HPA, ASG target tracking) | CPU, RPS, queue depth, lag | lag of 1–5 min — not enough for spikes alone |
| Scheduled | time of day | known peaks (9 a.m., sales) |
| Predictive | ML on history | AWS predictive scaling |
| Pre-warming | manual before events | Super Bowl ads, ticket drops; also warm caches and pre-scale DBs |
Scaling is limited by the slowest-scaling dependency: you can add 100 app servers in minutes but not 10 more database primaries.
Cell-based architecture and shuffle sharding
Interview questions
Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.