Drop load before you fall over
Traffic exceeds capacity and every request slows until all time out.
Say it like this
When demand exceeds capacity the worst outcome is every request timing out, so I shed load early: the gateway rejects the least valuable traffic with a 429 when queue depth or latency crosses a line, and the requests we keep finish fast.
Basic solution
Reject excess requests early (429 or 503) based on queue depth or latency so the rest succeed.
Shed by class at the gateway
A queue-wait signal trips the shedder. Lowest-value classes are rejected first with a cheap 429, so active trips and new riders still reach the matcher within capacity.
How it works
- Choose a signal that rises before requests start failing: the depth of the request queue, the number of requests currently in flight, p99 latency over the last few seconds, or CPU above a threshold. Queue wait time is usually the best choice because it does not depend on knowing the capacity in advance.
- Set the line at which shedding starts: for example, in-flight requests above 80% of the capacity you measured in a load test, or queue wait above 100 ms.
- Tag every incoming request with a value class at the edge, from its route and its authentication: interactive user requests first, then API clients, then background and bulk jobs, then retries and scraper traffic last.
- When the signal crosses the line, the gateway rejects requests from the lowest class immediately with a 429 or 503 and a Retry-After header. The rejection happens before the request takes a database connection or does any real work.
- If the signal stays over the line, shed the next class up as well; as soon as the signal drops back, stop shedding, lowest class last.
- Count shed requests per class and export the counts, so capacity planning can see how often you dropped traffic and how much.
Where to shed
- As early as possible: the load balancer or gateway, before TLS work, before database connections. Shedding after the expensive part is done wastes the work.
Worked example
Ride matching on New Year’s Eve
- A city region normally takes 500 ride requests/s; the matcher can handle 1,500/s. At midnight demand hits 6,000/s.
- No shedding: the matcher’s queue grows by 4,500/s. After 20 seconds there are 90,000 requests queued, the oldest already 15 s old and ageing faster than it drains. Every rider’s app has timed out, most retry, and now the queue grows faster still. Effective throughput is near zero because the matcher works on requests whose riders have given up.
- With shedding at 1,200 in-flight: the first 1,500/s are admitted; the rest get an immediate "high demand, try again in 15 s" with jittered Retry-After. Admitted riders are matched in under 3 seconds. 4,500/s are rejected quickly and cheaply; with pricing and the Retry-After spreading them out, demand smooths to about 2,000/s within a minute.
- Classes: riders with an active trip (status polls) are never shed; new requests are shed first; driver location updates are throttled to every 15 s rather than rejected.
Variants and when each wins
Static threshold on in-flight requests
- The gateway rejects any request that would push in-flight work above a fixed number N. It wins for simplicity and predictability; N must be re-measured whenever hardware or code changes.
Queue-based shedding (CoDel style)
- The gateway rejects a request when the oldest request still waiting has waited longer than a target. It wins because it tunes itself to whatever capacity is available right now, with no N to measure.
Priority shedding
- Each traffic class has its own threshold, so low-value traffic is dropped first and high-value traffic keeps the full capacity. It wins in production, where it is the usual form.
Probabilistic shedding
- Instead of a hard line, the gateway drops a fraction of requests that grows as load rises. It wins when a hard threshold would make the system oscillate between accepting everything and rejecting everything.
Recommendation: priority classes with a queue-wait trigger, applied at the gateway, with probabilistic ramp so the line is not a cliff.
Failure modes
Shedding too late
- Requests are rejected only after a database query has already run for them. It happens because the shedding check sits deep inside the handler. Move it to the gateway or the first middleware, before any I/O happens.
Threshold set from a guess
- Shedding starts at 40% of real capacity, or never starts at all. It happens because N was chosen without a measurement. Run a load test to find the knee of the latency curve, set the line just below it, and prefer a queue-wait trigger that adapts on its own.
Clients retry instantly
- Rejected traffic comes straight back and doubles the load. It happens because the 429 carries no Retry-After, so clients retry in a tight loop. Send Retry-After with jitter, make sure the clients honour it, and shed requests that carry a retry header before anything else.
Shedding the wrong traffic
- Riders on active trips stop getting status updates while scrapers keep receiving pages. It happens because requests are not classified, so the shedder treats them all the same. Tag each request with a class at the edge, derived from its route and auth, and shed by class.
Oscillation
- The system flips between 0% and 100% rejection every second. It happens because the threshold is a hard line and the signal lags reality. Use a probabilistic ramp between two thresholds and smooth the signal over a short window.
Interviewer follow-ups
Answer out loud before you open each one.
What is the difference between load shedding and rate limiting?
Rate limiting is per client and enforces a contract regardless of load. Load shedding is per server and reacts to actual saturation regardless of who is calling. You need both: limits stop one client from being unfair, shedding stops the sum of fair clients from sinking the server.
How do you choose the saturation signal?
Pick the one that rises before errors do. Queue wait time is usually best because it is capacity-independent. CPU is a poor signal for I/O-bound services, and error rate is too late. Measure over a short window, a second or two, so the reaction is fast but not jittery.
What should the response to a shed request look like?
Cheap and honest: 429 or 503, a Retry-After with jitter, a short body the client can show, and no logging of the full request. The whole point is that a rejected request costs almost nothing, so it must not trigger alerts, traces or expensive error handling.
How does shedding relate to autoscaling?
Autoscaling takes minutes; shedding takes milliseconds. Shedding buys the time for new capacity to arrive and keeps the existing capacity productive while it does. Shed counts are also the best input to the scaler, since they measure demand you could not serve.
Connections
- In study paths
- Staying up under load
- Built on
- Heap / priority queue
- Pairs with
- Admit only what you can finish · Limit requests per client · Slow the producer when the consumer is behind · Never wait forever · Scale out, not up
- Pulls against
- Retry transient failures carefully
- Used in
- Distributed Rate Limiter · Ride Matching (Uber / Lyft)
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon API Gateway | Gateway shedder | API Gateway, Apigee | API Management | Kong, Tyk |
| Amazon EC2 Auto Scaling | Matcher | Compute Engine managed instance groups | Virtual Machine Scale Sets | Kubernetes Deployment with a HorizontalPodAutoscaler, Nomad |
| Amazon Aurora | Trip store | AlloyDB for PostgreSQL, Cloud SQL | Azure Database for PostgreSQL or MySQL | PostgreSQL, MySQL, CockroachDB |