Backpressure & load shedding

In one sentence

When demand exceeds capacity, bound every queue, push the slowdown back to callers that can wait, and reject the rest early and cheaply so the work you accept still finishes in time.

Analogy: A restaurant with a full dining room stops seating people at the door. It does not take every order and let the kitchen fall hours behind until all the diners walk out.

Admission control keeps the queue short: excess work is rejected at the door or pushed back to the producer, never parked.

How it works

  • An unbounded queue turns overload into latency, then timeouts: the server works on requests nobody is waiting for.
  • Bound the queue: wait ≈ depth ÷ service rate, so size it from the latency budget.
  • Backpressure slows the producer; it only works when the source can wait, like an internal pipeline.
  • Load shedding rejects early and cheaply with a 503, lowest priority first; never shed health checks.
  • Retries can keep an overload alive after its cause is gone: a metastable failure.

Check yourself

Answer to mark it learned; your answers aren't scored.

Load is 30% above capacity. p99 latency climbs for a minute, then almost every request times out, although CPUs sit at 100%. The request queue is unbounded and clients retry on timeout. Best fix?

Go deeper

Full explanation

A server past capacity keeps accepting work, and the extra waits in a queue. With an unbounded queue, the wait grows until it passes the client's timeout. From then on the server spends its capacity on requests nobody is waiting for: throughput stays high, goodput (answers that are correct and on time) falls toward zero, and timed-out clients retry (Retries, backoff & circuit breakers), adding load.

Three defenses:

  • Bound the queue. Wait ≈ depth ÷ service rate, so size it from the latency budget.
  • Backpressure: when the queue is full, make the producer slow down (block it or stop pulling). It only works when the source can wait: internal pipelines, consumers of a log (Queues vs streams (Kafka)).
  • Load shedding: when the source can't wait (users, the open internet), reject at the door with a cheap 503 + Retry-After, before parsing or database work. Shed by priority: batch and crawlers first, never the load balancer's health checks. Drop requests whose deadline already passed.

Rate limiting caps each client; shedding protects the server when everyone is within quota. Overload can outlive its cause: in a metastable failure, retries keep the system saturated until you cut load hard.

Code Bounded queue: shed at the door, backpressure inside
pythonBounded queue: shed at the door, backpressure inside
import asyncio, time

MAX_DEPTH = 200   # ~0.2 s of work at 1,000 req/s: sized from the latency budget
queue: asyncio.Queue = asyncio.Queue(maxsize=MAX_DEPTH)

class Overloaded(Exception): ...   # map to HTTP 503 + Retry-After

def admit(req) -> None:
    # Shed at the door, before parsing or touching the database
    limit = MAX_DEPTH if req.critical else MAX_DEPTH // 2   # batch goes first
    if queue.qsize() >= limit:
        raise Overloaded()
    queue.put_nowait(req)

async def produce(job) -> None:
    await queue.put(job)   # backpressure: an internal producer blocks while full

async def worker() -> None:
    while True:
        req = await queue.get()
        if time.monotonic() > req.deadline:
            continue       # the caller already timed out: don't waste capacity
        await handle(req)
Real example

Assume a service handles 1,000 req/s, clients time out at 1 s, and load jumps to 1,300 req/s.

  • Unbounded queue: the backlog grows by 300 req/s. After ~3.3 s it holds 1,000 requests, a 1 s wait, so every new request times out before a worker reaches it. CPUs stay busy, goodput is ~0, and retries push load higher.
  • Bounded queue of 200 + shedding: the wait stays under 0.2 s. 1,000 req/s succeed and 300 get an instant 503.
Trade-offs +3 · −3

+ You gain

  • Accepted requests keep meeting their latency target during overload; goodput stays near capacity.
  • A fast rejection is a clear signal: the caller can back off or try another server right away.
  • Priorities protect what matters: checkout and health checks survive, batch waits.

− You pay

  • Some users get errors by design, and someone must decide which traffic is sheddable.
  • Rejecting is not free: a server can saturate just returning errors (Google SRE).
  • Thresholds need load tests: too tight wastes capacity, too loose sheds too late.

Use it when

Every service that can receive more work than it can finish: bounded queues and deadlines everywhere, backpressure between internal stages, priority shedding at the edge of user-facing systems.

Avoid it when

Don't block callers that can't wait (interactive users): shed instead. Don't shed work that must not be lost (payments, events): buffer it in a durable queue and scale the consumers.

Numbers to know 5
  • Queue wait ≈ depth ÷ service rate: 200 queued at 1,000 req/s is a 0.2 s wait.
  • Google SRE: for steady traffic, keep the queue at 50% or less of the thread pool size.
  • Google tags every request with one of four criticality levels: CRITICAL_PLUS, CRITICAL, SHEDDABLE_PLUS, SHEDDABLE.
  • Google clients throttle themselves once their requests exceed K × the accepted ones; K = 2 is the usual choice.
  • Metastable failures were named by Bronson et al. at HotOS 2021.
Common pitfalls 4
  • Raising the queue size or the client timeout to "absorb" overload: it adds latency and wastes more work.
  • Shedding after the expensive part (auth, parsing, queries): a rejection must cost far less than the request.
  • Shedding health checks like normal traffic: the load balancer removes servers and the rest get more load.
  • FIFO under overload serves the oldest request, the one most likely abandoned; LIFO or dropping by age favors fresh ones.
Interview questions 3

Answer out loud first, then reveal.

Why does a bigger queue make overload worse?Show answer

A queue only absorbs a burst shorter than its drain time. Under sustained overload it fills anyway, and its depth becomes pure latency: wait is depth over service rate. Once the wait passes the client timeout, I'm serving requests nobody is waiting for, and their retries add load. So I size queues from the latency budget, a fraction of the timeout, and reject beyond that.

When do you use backpressure and when load shedding?Show answer

Backpressure when the producer can slow down without harm: a batch job, a stream consumer, an internal stage. I block or stop pulling, and the backlog stays in a durable log. Shedding when it can't: a user won't wait and the internet won't slow down, so I reject early with a 503, lowest priority first. Most systems need both: shed at the edge, backpressure inside.

Load is back to normal but the service is still down. What is happening?Show answer

That's a metastable failure. A trigger, like a brief capacity dip, pushed us into overload, and a feedback loop keeps us there: timeouts cause retries, and retries keep the queues full. Removing the trigger isn't enough. I break the loop: shed most traffic at the edge, let queues drain, then ramp back up gradually. Long term I add retry budgets, bounded queues and capacity headroom.