Backpressure and Load Shedding
Every part of a system drains work at some rate, and demand does not ask what that rate is. When more arrives than leaves, the excess goes somewhere: into a queue, into memory, into every caller's wait, or onto the floor. Backpressure and load shedding decide where, on purpose.
When the interviewer asks what your queue does under load, the real question is whether it absorbs a burst or hides an outage. The answer starts with a subtraction.
A Queue Is Borrowed Time
A queue absorbs a burst only while arrivals, averaged over time, stay below the drain rate. Assume workers drain 2,000 messages a second, and a burst brings 5,000 a second for one minute. The queue grows by 3,000 a second. After the minute it holds 180,000 messages.
The last message in waits for everything ahead of it. At 2,000 a second, 180,000 messages take 90 seconds to drain. When arrivals fall back to 1,000 a second, the queue shrinks by 1,000 a second, and it is empty in three minutes.
The wait is the depth divided by the drain rate. In a steady queue that is Little's law; in a burst it is simply the line ahead of the last message, and it is the number a user feels. A minute and a half of delay is fine for a weekly report and far too long for someone watching a spinner, so the subtraction always ends on who is waiting.
Bounded Queues
A bound on a queue is sized by the wait its callers tolerate. Assume callers give up after 10 seconds. At 2,000 a second, 10 seconds of wait is 20,000 messages, so every message past 20,000 is work for someone who has already left.
An unbounded queue turns overload into slow failure for everyone, and then into exhausted memory. A bound turns it into fast refusal. The cost is that some requests are refused that a longer wait would have served. The trigger to raise the bound is a caller that waits longer. Work with no waiting caller, such as a nightly export, is bounded by the time it must finish by instead.
Backpressure Up the Chain
Backpressure tells the sender to slow down instead of dropping its work. Inside the system it works. A consumer pulls only what it can process, and a producer blocks when the broker's buffer is full, so the pressure travels back one hop at a time and nothing is lost.
At the edge it stops working, because users do not slow down when asked. Pressure that reaches the edge becomes refusal: a 429 or a 503, with a hint of when to retry. Deciding who gets the 429 across a whole fleet is the rate limiter problem in Chapter 11.
How one connection slows its sender is Networking Deep Dive's lesson on flow control, and one service's backpressure is in Backend Deep Dive's chapter on performance and scale. The question here is where, in a system of many parts, the excess is allowed to land.
Shedding by Priority
Shedding drops the least valuable work first, and drops it at the door, before any work is spent on it. The classes come from what a loss costs: a sign-in code ahead of a marketing message, which the notification system in Chapter 15 designs, and a checkout ahead of a recommendation.
Each class gets its own queue and its own workers, so a flood of low-priority work never stands in front of high-priority work. A priority field in one shared queue cannot promise that: once every worker is busy with low-priority messages, the urgent one still waits for a worker to come free. A request older than its caller's timeout is dropped unread. The cost is that the shed class sees errors, and the design names that class out loud.
Absorbing a Burst or Hiding an Outage
In a burst, the depth rises and falls, and the oldest message's age stays bounded. In an outage the drain is zero. At 1,000 arrivals a second, the queue gains 60,000 messages a minute and 3.6 million an hour, while everything upstream looks healthy, because the broker keeps accepting.
So the signal is the oldest message's age, together with arrivals against drain, never depth alone. Running the monitoring that raises that alarm is Observability Deep Dive's subject; the design only has to say which number the alarm watches.
Recovery has its own trap. The backlog drained after the outage is itself a burst on whatever the workers write to, so the catch-up rate is set on purpose, not left at whatever the workers can reach. How overload at one tier spreads to the services around it is the next lesson.
Backpressure slows the sender, so nothing is lost and everything waits longer. Use it inside the system, between parts that can wait and can be told to slow down.
Load shedding refuses work, so the rest is served on time and some callers get an error. Use it at the edge, where the sender is a user who will not slow down, and in any tier where a late answer is worth nothing.
How Interviewers Probe This
Overload probes all have the same answer shape: the rates first, the subtraction second, and the choice of who waits and who is refused third.
A typical one is "Traffic triples for ten minutes. What does your queue do?" With this lesson's numbers, three times 1,000 a second is 3,000, against a drain of 2,000. The queue grows by 1,000 a second, and after ten minutes it holds 600,000. The last message waits 600,000 over 2,000, which is five minutes. You might say: "Five minutes is longer than any user will wait, so I bound this queue and shed what doesn't fit."
Another is "Your consumers were down for an hour and nobody noticed. How?" A good answer names the queue that hid the outage, the alarm on the oldest message's age that would have caught it within minutes, and the catch-up rate once the consumers return.
The blunt one is "You can't serve everyone. Who loses?" A good answer ranks the classes by what a loss costs, sheds the lowest at the door, and keeps a separate path for the highest.
- "A queue absorbs any spike." Only while arrivals average below the drain rate. Past that it only grows, and so does its wait.
- "An unbounded queue never loses work." It loses it later and worse, as memory runs out or as answers arrive after their callers have gone.
- "Refusing requests means the design failed." Refusing a tenth quickly keeps nine tenths fast, while taking all of them can make all of them slow.
- "Queue depth is the health signal." A deep queue draining fast is fine; a shallow one whose oldest message is an hour old is an outage.
- "Autoscaling makes shedding unnecessary." New capacity takes time to start, and the overload arrives first. Shedding covers the minutes before it is ready.
- Bound every queue a waiting caller sits behind at the drain rate times that caller's wait.
- Refuse at the edge, before any work is spent, with a status and a retry hint the client can act on.
- Give each priority class its own queue and workers, so a low-priority flood never stands in front of the high-priority path.
- Alarm on the oldest message's age and on arrivals above drain, not on depth alone.
Knowledge Check
Workers drain 2,000 messages a second. A burst brings 5,000 a second for one minute. How long does the last message of the burst wait?
- About 2.5 seconds, the ratio of arrivals to the drain
- No wait at all, since the burst ended before it was read
- About 90 seconds, behind a backlog of 180,000 messages
- About 150 seconds, the burst's 300,000 messages in all
Workers drain 2,000 messages a second and callers give up after 10 seconds. Where should the queue's bound be?
- About 20,000 messages, ten seconds of draining
- About 2,000 messages, one second of draining
- No bound at all, so that no request is refused
- As large as the broker's memory can hold safely
Users flood the public API at the edge. Why shed load there rather than apply backpressure?
- Backpressure drops work silently, while shedding reports it
- Users do not slow down when asked, so it has to be refusal
- Backpressure is too costly to run on the busiest tier there
- Shedding serves every request, only some a little later
Sign-in codes and marketing messages share one queue with a priority field. What can still go wrong?
- The queue ignores the field and hands messages out strictly in arrival order
- A sign-in code waits while every worker is busy with marketing messages
- Marketing messages are sent twice, because high priority repeats delivery
- The priority field doubles the size of every message held in the queue
Which signal tells a queue absorbing a burst from a queue hiding an outage?
- The queue's depth, compared with its usual depth
- The error rate that producers see when they send
- The age of the oldest message waiting in the queue
- The disk used by the broker that holds the queue
You got correct