Backpressure and load shedding

Topic 3 of 8 in Week 6: Production concerns9 min readIntermediateFree course

Overview

Every system has a maximum throughput. When more work arrives than it can handle, it must do something with the excess. The worst option, and the default if you don’t design for it, is to accept everything: queues grow, latency explodes, memory runs out and the service crashes, often repeatedly. Backpressure and load shedding are the deliberate alternatives.

Why unbounded queues fail

If requests arrive at 15,000 per second and the service can handle 10,000, an unbounded queue grows by 5,000 every second. Within a minute, each new request waits behind 300,000 others. Users gave up long ago, but the service is still doing their work: it’s busy, but producing nothing useful. Eventually it runs out of memory.

The rule: bound every queue, and decide what happens when it’s full.

Backpressure and load shedding. Incoming traffic of 15 thousand requests per second enters a bounded queue with a maximum of 1,000 items, feeding a service with a capacity of 10 thousand requests per second. When the queue is full, requests are rejected early with 503 or 429 and Retry-After. Shed by priority: keep checkout and login, degrade browse and search, and shed recommendations and analytics first.

Backpressure

Backpressure means pushing the problem back to the sender so it slows down:

  • Blocking: a producer writing to a full bounded queue waits (common inside a process or between threads).
  • Rejecting: the service returns 429 or 503 with Retry-After; well-behaved clients back off.
  • Flow control: the receiver tells the sender how much it can accept (TCP windows, gRPC and reactive-streams credits).
  • Consumer lag: in Kafka-style pipelines, the log absorbs the backlog durably; producers or upstream jobs check lag and slow down or pause.

Backpressure must propagate all the way to the source, or the pressure just builds somewhere else.

Load shedding

Sometimes you can’t slow the source (millions of users pressing refresh). Then shed load: reject some requests early so the rest are served well.

  • Reject cheaply: decide at the edge or first thing in the service, before doing expensive work. A fast rejection costs microseconds; a timeout costs a thread for seconds.
  • Use good signals: queue length, concurrency in flight, CPU, or latency (when p99 rises above target, start shedding). Adaptive concurrency limits adjust automatically, similar to TCP congestion control.
  • Shed by priority: tag requests at the gateway (checkout > browsing > recommendations > analytics) and drop the lowest first. Within a priority, prefer keeping requests from users who are mid-session over brand-new ones.
  • Drop stale work: if a queued request’s deadline has passed, skip it rather than process it.

Graceful degradation

Instead of rejecting outright, do less work per request:

  • serve cached or static versions of pages,
  • turn off expensive features (personalisation, related items, live counts),
  • use cheaper algorithms (approximate search results),
  • make non-critical writes asynchronous.

Feature flags that switch these modes on during incidents are invaluable.

Waiting rooms

For predictable extreme spikes (concert tickets, product drops), a virtual waiting room admits users to the protected flow at a fixed rate and shows everyone else their place in line. It turns an overload into an orderly queue with honest feedback.

Autoscaling isn’t enough

Autoscaling adds capacity in minutes; spikes arrive in seconds. It also can’t scale databases or third-party APIs. Shedding and backpressure hold the line while autoscaling catches up, and protect the parts that can’t scale at all.

In an interview

When discussing peaks or the “10× traffic” follow-up: “Queues are bounded; when they fill, the gateway sheds lowest-priority traffic first, recommendations and analytics, while checkout keeps its capacity. Pages degrade to cached versions, and for ticket on-sales we put a waiting room in front of checkout.”

Recap

When demand exceeds capacity, unbounded queues turn overload into crashes. Bound queues, use backpressure to slow senders, and shed load early, cheaply and by priority so that critical flows keep working. Degrade gracefully, use waiting rooms for known spikes, and don’t rely on autoscaling alone.

Exercise

Black Friday overload

On Black Friday an online store receives 3× its tested capacity for 20 minutes. Traffic includes: checkout and payment, product pages, search, recommendations, and internal analytics events. The order service's queue grows without limit, memory runs out and it crashes and restarts repeatedly. Propose a plan using bounded queues, backpressure, load shedding and degradation, saying what users experience.

Text
Bounded queues / backpressure:

Load shedding by priority:

Degradation:

What users experience:

Summary

  • When demand exceeds capacity, unbounded queues only delay the failure and add latency; bound every queue.
  • Backpressure signals upstream to slow down: blocking, rejecting, or flow-control credits.
  • Load shedding deliberately rejects some work early so the rest succeeds; reject cheaply and quickly.
  • Shed by priority: protect critical flows such as checkout and login, and drop optional work first.
  • Combine with graceful degradation, waiting rooms and autoscaling, which is too slow to handle sudden spikes on its own.