Load Shedding & Admission Control
the work you must refuse in order to stay up
System designsystem design / resilience
The mental model: a triage desk, not a queue
Picture an overloaded emergency room. A queue promises "first come, first served." Triage puts someone at the door who turns people away, so the people inside survive.
That door is admission control. Load shedding is rejecting work early, fast and cheap, before it takes a thread, a connection or a DB handle.
Where the analogy breaks — and this is the intermediate part: real triage sorts by severity, and your server usually cannot tell which request matters. What it can measure precisely is how long a request has already been waiting. That turns out to be the better signal anyway.
Why a FIFO queue melts instead of degrading
Concrete numbers. Capacity 100 req/s, service time 100 ms, client timeout 1s. Traffic arrives at 120 req/s.
backlog grows 20/s
t=10s queue=200 wait=2.0s -> clients already gone
t=30s queue=600 wait=6.0s
CPU=100%, useful throughput = 0
Nothing errored. Nothing was slow in isolation. But every reply now lands after its caller gave up, so you are doing maximum work for zero value. Little's Law says it plainly: W = L / λ, and nobody bounded L.
Fix 1: put the deadline on queue wait, not on the work
Check staleness at dequeue, before touching anything expensive:
const WAIT_BUDGET_MS = 250;
function handle(req) {
if (Date.now() - req.enqueuedAt > WAIT_BUDGET_MS) {
metrics.shed.inc();
return reply(503, { retry_after: 1 });
}
return doWork(req);
}
A shed request costs microseconds, so the queue now self-drains. Under the same 120 req/s you serve a healthy 100 and reject 20 in 2 ms each — instead of failing all 120 slowly.
Fix 2: flip to LIFO when the queue is deep
Counterintuitive, and correct: when the backlog is long, serve the newest request first. The oldest entries belong to clients that have already timed out. FIFO distributes the pain so evenly that everyone misses their deadline; LIFO keeps fresh arrivals fast and concentrates the loss on a bounded set. Envoy and Finagle ship this; CoDel-style controllers watch sojourn time and switch FIFO → LIFO once it exceeds target.
Four traps practitioners hit
- Expensive rejection. If your 503 path runs auth plus a Redis lookup, shedding adds load. Shed before auth, at the cheapest layer that can see the queue.
- Retry amplification. 503 with a client that retries 3× is 3× traffic at your worst moment. Always pair shedding with
Retry-Afterand a client-side retry budget (cap retries at ~10% of requests). - The queue you forgot to bound.
somaxconn, the LB backlog,new LinkedBlockingQueue()with no capacity — each is an unbounded buffer hiding latency. Bounded queue + fast reject is the mechanism. - Confusing it with a circuit breaker. A breaker protects you from a failing dependency. Shedding protects you from your own callers. You need both.
Your exercise
Take your busiest endpoint and write down three numbers: capacity in req/s, p50 service time, and the client's timeout.
Now find the queue in front of it and its bound — thread-pool queue size, LB backlog, somaxconn. If any is unbounded, compute the wait after a 2× spike held for 60 seconds. Then pick a WAIT_BUDGET_MS that keeps served requests inside the client timeout, and ask: once that budget is live, what does your p99 latency graph look like during a spike, and why is that a good thing?