Sub topic 2
Load Shedding: Rejecting Work on Purpose
Assume you understand basic producer/consumer programs, HTTP status codes, request latency, and async tasks or worker pools.
Why shed load?
Load shedding is the deliberate rejection of work when a service cannot process more requests safely. Without it, requests wait in a growing queue. Latency rises, memory fills, and even requests that could succeed may time out.
Shedding protects capacity for work already admitted. It is different from back pressure:
- Back pressure slows the producer until capacity becomes available.
- Load shedding refuses work immediately so the system can remain responsive.
A bounded queue can use either behavior:
import asyncio
async def submit(queue: asyncio.Queue, request: str) -> bool:
try:
queue.put_nowait(request)
return True
except asyncio.QueueFull:
return False
async def main() -> None:
queue = asyncio.Queue(maxsize=2)
for request in ["a", "b", "c"]:
accepted = await submit(queue, request)
print(request, "accepted" if accepted else "rejected")
asyncio.run(main())Deadlines and useless work
A deadline is the latest time a result is useful to the caller. Pass the remaining deadline through downstream calls. Stop work when that deadline expires. Continuing after a client has timed out consumes capacity without producing a useful result.
Choosing what to shed
A shedding policy should state which requests lose first.
- Priority: preserve paid, critical, or health-related work.
- Age: reject requests that have waited longer than their remaining deadline.
- Fairness: prevent one tenant or request class from consuming all capacity.
Prefer explicit policy over accidental behavior. For example, reject low-priority work when queue age exceeds one second, while reserving capacity for high-priority work.
Tell clients what happened
Use 429 Too Many Requests when the client exceeded a rate or quota. Use 503 Service Unavailable when the service is temporarily overloaded or unavailable. Include Retry-After when a retry may succeed. Its value is either a delay in seconds or an HTTP date.
Clients should honor the signal. Otherwise, synchronized retries can recreate the overload.
Exercise
A queue is full and the request belongs to a low-priority class. What should the service do?
Show answer
Reject it promptly, return an overload response such as 503, and include Retry-After when a later retry is appropriate.