Sub topic 8
Choosing what to shed: priority, age, and fairness
This chapter assumes you know what a bounded queue is, why load shedding rejects work on purpose, and how deadlines work. Once you have decided to shed, the next question is which requests to reject. A policy that picks at random is better than none, but it often drops the work that matters most.
Three criteria
Most shedding policies combine three questions.
- Priority: how much does this request matter?
- Age: how long has it waited, and is anyone still waiting for the answer?
- Fairness: is one client using most of the capacity?
| Criterion | Sheds first | Protects |
|---|---|---|
| Priority | Low-value work, such as analytics or prefetch | Health checks, logins, payments |
| Age | The oldest requests | Requests that can still finish in time |
| Fairness | Clients over their share | Small and well-behaved clients |
Priority
Give each request a class when it arrives. Shed the lowest class first and keep the highest class until the end. The class should come from the request type or from a trusted header that your own gateway sets. Do not trust a value that clients can set freely, or every client will mark its traffic as critical.
Age: drop the oldest, not the newest
A plain first-in-first-out queue under overload has a bad property. Every request waits a long time, so many of them time out on the client side before a worker reaches them. The server then does full work for answers nobody reads.
Checking age when a worker takes an item fixes this. If an item has waited longer than its deadline allows, drop it and take the next one. This is sometimes called head dropping.
import asyncio
import time
MAX_WAIT = 0.5 # seconds a request may wait before it is useless
async def worker(queue: asyncio.Queue, process) -> None:
while True:
enqueued_at, request = await queue.get()
if time.monotonic() - enqueued_at > MAX_WAIT:
continue # stale: the client has probably given up
await process(request)
def submit(queue: asyncio.Queue, request) -> bool:
try:
queue.put_nowait((time.monotonic(), request))
return True
except asyncio.QueueFull:
return False # caller returns 503Some systems also switch the queue from first-in-first-out to last-in-first-out while overloaded. The newest requests have the most time left, so serving them first completes more useful work. The cost is that old requests may wait indefinitely, which is why they need the age check above.
Fairness
Without a per-client limit, one noisy client can fill the queue and starve everyone else. Two common fixes:
- Keep a separate token bucket or quota for each client, and reject only the clients that exceed it.
- Use one queue per client or per class, and have workers take from them in turn.
Per-client limits mean shedding hits the source of the overload first, instead of spreading the pain evenly.
Trade-offs and common mistakes
- Too many priority classes are hard to reason about. Two or three are usually enough.
- Priority starvation: if high-priority traffic alone can fill capacity, low-priority work never runs. Reserve a small share for it if that matters.
- Dropping a request after it has used resources wastes them. Check as early as you can.
- Shed decisions must be cheap. A policy that costs more than the work it avoids makes overload worse.
Exercise
A queue is full and requests wait about 2 seconds, but clients time out after 1 second. Why does dropping the oldest requests at dequeue time raise the number of successful responses, and what risk comes with switching to last-in-first-out?
Show answer
Requests that already waited past the client timeout can never produce a useful answer, so processing them wastes worker time. Dropping them lets workers spend that time on requests that can still finish within the timeout. With last-in-first-out, the oldest requests can wait indefinitely, so they need an age check or they will be stuck in the queue and never served.