Load Shedding & Graceful Degradation
Every service has a capacity. When more work arrives than it can do, something has to give, and the only choice is what. Without a plan, the answer is "everything": queues grow, latency rises past every client's timeout, and the server spends its capacity on requests whose callers already left. Load shedding rejects some work early and cheaply so the rest is served well; graceful degradation turns off the less important parts of the work so the important parts keep working.
A traffic spike, three ways
The server completes 100 requests per tick (100 ms). Normal traffic is 80 per tick; for 10 seconds a spike brings 150. Clients give up after 1 second (10 ticks). 30% of requests are checkouts, the rest are optional (recommendations, browsing). Three server policies:
- unbounded queue: accept everything, serve in arrival order;
- bounded queue: reject new requests with a fast 503 once the queue holds 5 ticks of work (half the deadline);
- priority shedding: like the bounded queue, but optional requests are rejected earlier, once the queue holds 2 ticks of work.
from collections import deque
CAPACITY = 100 # requests the server completes per tick (100 ms)
DEADLINE = 10 # clients give up after 10 ticks (1 s)
TICKS = 300
def offered(t): # a traffic spike: 150/tick between ticks 100 and 199
return 150 if 100 <= t < 200 else 80
def run(policy):
queue = deque() # (arrival tick, critical?)
good = {True: 0, False: 0}
late = rejected = 0
total = {True: 0, False: 0}
for t in range(TICKS):
n = offered(t)
for i in range(n):
critical = i % 10 < 3 # 30% of requests are checkouts
total[critical] += 1
if policy == "bounded queue" and len(queue) >= CAPACITY * DEADLINE // 2:
rejected += 1 # fast 503: the client can retry elsewhere
continue
if policy == "priority shedding" and not critical and len(queue) >= CAPACITY * 2:
rejected += 1 # shed optional work first, keep checkouts
continue
if policy == "priority shedding" and len(queue) >= CAPACITY * DEADLINE // 2:
rejected += 1
continue
queue.append((t, critical))
for _ in range(min(CAPACITY, len(queue))):
arrived, critical = queue.popleft()
if t - arrived <= DEADLINE:
good[critical] += 1
else:
late += 1 # served after the client left: wasted work
goodput = (good[True] + good[False]) / TICKS
print(f"{policy:<18} goodput {goodput:5.1f}/tick wasted (late) {late:>5} rejected fast {rejected:>5} "
f"checkouts ok {good[True] / total[True]:5.1%} other ok {good[False] / total[False]:5.1%}")
for policy in ("unbounded queue", "bounded queue", "priority shedding"):
run(policy)unbounded queue goodput 37.2/tick wasted (late) 16850 rejected fast 0 checkouts ok 36.0% other ok 36.0%
bounded queue goodput 88.0/tick wasted (late) 0 rejected fast 4600 checkouts ok 85.2% other ok 85.2%
priority shedding goodput 87.1/tick wasted (late) 0 rejected fast 4880 checkouts ok 100.0% other ok 77.5%
The unbounded queue is the worst of all. Once the backlog exceeds what can be served within the deadline, nearly every request is served too late: the server is 100% busy doing work nobody receives, and goodput collapses to a third of capacity. It stays bad after the spike, until the backlog drains. The bounded queue rejects about 15% of requests immediately and serves everything it accepts in time; goodput stays close to capacity. Priority shedding rejects slightly more in total, but every checkout succeeds: the rejections fall on optional traffic.
How services shed load
| Technique | How |
|---|---|
| bounded queues and concurrency limits | reject when in-flight requests or queue length exceed a limit derived from Little's law (Queueing) |
| deadline-aware queues | drop requests whose deadline has already passed before starting work (Timeouts & Deadlines) |
| adaptive concurrency | adjust the limit from measured latency (Envoy's adaptive concurrency filter, Netflix's concurrency-limits library) |
| priority classes | tag requests (header, route, tenant) as critical or sheddable; shed sheddable first (Kubernetes API Priority and Fairness does this for the API server) |
| rate limits per client | stop one noisy client from using everyone's capacity (429 Too Many Requests with Retry-After) |
| shedding at the edge | the load balancer or gateway rejects before work reaches the service |
A minimal priority-aware limiter as middleware (sketch, non-run):
MAX_IN_FLIGHT = 200 # from Little's law: target throughput x latency, with headroom
SHEDDABLE_LIMIT = 120 # optional traffic gets only part of the capacity under pressure
in_flight = 0
def handle(request):
global in_flight
critical = request.headers.get("x-priority") == "critical"
limit = MAX_IN_FLIGHT if critical else SHEDDABLE_LIMIT
if in_flight >= limit or request.deadline_passed():
return Response(status=503, headers={"Retry-After": "1"}) # cheap, fast, before any real work
in_flight += 1
try:
return process(request)
finally:
in_flight -= 1Graceful degradation
Shedding drops whole requests; degradation keeps the request but does less work for it. Decide the levels in advance and wire each one to a feature flag or an automatic trigger:
| Level | Turned off | Users notice |
|---|---|---|
| 0 normal | nothing | nothing |
| 1 | personalised recommendations (show bestsellers from cache) | slightly less relevant suggestions |
| 2 | live stock counts, reviews on product pages | "in stock" instead of "3 left", no reviews |
| 3 | search autocomplete, non-essential images | plainer pages |
| 4 | everything except browse → cart → checkout | a minimal shop that still takes orders |
Two rules make degradation trustworthy: test it regularly (a degradation path that is never exercised won't work when needed; see Chaos Engineering), and make sure the degraded path is cheaper than the normal one: a "fallback" that queries the same overloaded database helps nobody.
Test yourself
Why did the unbounded queue's goodput collapse during the spike?
- The server got slower
- Requests waited longer than the client deadline, so the server spent its capacity on responses nobody received
- It rejected requests
- The model has fewer ticks
What did priority shedding achieve compared with the plain bounded queue?
- Fewer rejections in total
- All checkouts succeeded; rejections fell on optional traffic
- Higher capacity
- No rejections
Which is a sound fallback during overload?
- Query the same overloaded database a second way
- Serve bestsellers from a precomputed cache instead of personalised recommendations
- Retry the recommendation call five times
- Increase timeouts