Sub topic 6
Load shedding: rejecting work on purpose
This chapter assumes you know how bounded queues and back pressure work, can read short Python examples, and know HTTP status codes. Back pressure slows a producer that you control. Load shedding is for the case where you cannot slow the producer, or slowing it is not enough.
Why shed load
Many producers sit outside your control: browsers, mobile apps, other teams' services. They keep sending work whether or not you can finish it. If you accept everything, the queue grows, latency rises, and eventually every request is too slow to be useful.
Load shedding means rejecting some work on purpose so that the rest still completes quickly. A fast "no" costs far less than a slow "yes" that arrives after the caller has given up.
Back pressure versus shedding
| Row | Back pressure | Load shedding |
|---|---|---|
| Producer's experience | Waits | Gets an error |
| Work lost | None | Some |
| Works when producer is external | Rarely | Yes |
| Typical tool | `await queue.put()` | `queue.put_nowait()`, limits |
Choose back pressure when producers are internal and can wait, such as a pipeline stage. Choose shedding when producers are many, independent, or sensitive to latency.
Shedding at a full queue
The simplest form uses a bounded queue and refuses to wait:
import asyncio
class Overloaded(Exception):
pass
def submit(queue: asyncio.Queue, request) -> None:
try:
queue.put_nowait(request)
except asyncio.QueueFull:
raise Overloaded("queue full") from Noneput_nowait raises asyncio.QueueFull at once. The caller converts Overloaded into an error response without any delay.
Shedding by work in flight
A queue is not the only place to shed. You can also cap how many requests run at once:
MAX_IN_FLIGHT = 50
in_flight = 0
async def handle(request, process):
global in_flight
if in_flight >= MAX_IN_FLIGHT:
return 503, {"Retry-After": "1"}
in_flight += 1
try:
return 200, await process(request)
finally:
in_flight -= 1This is safe in a single asyncio event loop because there is no await between the check and the increment. With threads you would need a lock or a semaphore. The finally block matters: without it, an exception leaks a slot, and the limit slowly shrinks until the service rejects everything.
Common errors
- Shedding too late. Rejecting only after the work is done saves nothing. Check capacity before you spend resources, ideally before parsing the body or opening database connections.
- Setting the limit by guess. If the limit is too high, shedding never triggers. If it is too low, you reject work you could have served. Derive it from Little's law and measure it under load.
- Silent drops. Dropping a request without telling the client leaves it waiting for a timeout. Return an explicit error.
- Not counting rejections. If you do not record shed requests, an outage can look like normal traffic.
Exercise
A service has 50 worker slots and each request takes 100 ms. A burst arrives at 1,000 requests per second. Using the in-flight limit above, roughly how many requests per second can be served, and what happens to the rest?
Show answer
Each slot handles 10 requests per second (1 / 0.1 s), so 50 slots serve about 500 requests per second. The other roughly 500 requests per second are rejected immediately with 503 and a Retry-After header, so the accepted requests keep their 100 ms latency instead of waiting in a growing queue.