š Backpressure & Load Shedding
How Healthy Systems Say āNoā to Stay Alive
Systems donāt fail because they get traffic.
They fail because they accept too much traffic.
The Core Problem: Unbounded Demand
What Most Systems Do ā
Accept all requests
Queue indefinitely
Let latency explode
Die under their own queues
Queues are not backpressure. Queues are deferred failure.
What Is Backpressure?
Backpressure is the ability of a system to slow down or stop upstream traffic when it is overloaded.
It answers:
āI am unhealthy ā please send me less.ā
What Is Load Shedding?
Load shedding is intentionally dropping requests to protect the system.
It answers:
āI cannot handle this request ā fail fast.ā
Why This Matters (Tail Latency Link)
When load > capacity:
Queues grow
Context switches explode
GC pressure rises
P99 goes to infinity
Protecting P99 requires rejecting work.
š§© Failure Mode ā No Backpressure
ā Naive Server
app.get("/data", async (req, res) => {
const result = await db.fetch();
res.send(result);
});
What Happens
DB slows
Requests pile up
Memory explodes
Process crashes
š§© Solution 1 ā Backpressure via Concurrency Limits
ā Semaphore-Based Control
import pLimit from "p-limit";
const limit = pLimit(100); // max 100 concurrent
app.get("/data", async (req, res) => {
try {
const result = await limit(() => db.fetch());
res.send(result);
} catch {
res.status(503).send("Overloaded");
}
});
Why This Works
ā Caps resource usage
ā Prevents queue explosion
ā Stabilizes latency
š§© Solution 2 ā Load Shedding (Fail Fast)
ā Reject Excess Traffic
let inFlight = 0;
const MAX = 200;
app.get("/data", async (req, res) => {
if (inFlight > MAX) {
return res.status(429).send("Too Busy");
}
inFlight++;
try {
res.send(await db.fetch());
} finally {
inFlight--;
}
});
Key Insight
A fast failure is better than a slow success.
š§© Solution 3 ā Adaptive Load Shedding
Shed Based on Latency
let shed = false;
setInterval(() => {
shed = metrics.p99 > 500;
}, 1000);
app.get("/data", async (req, res) => {
if (shed) return res.status(503).send("Degraded");
res.send(await db.fetch());
});
ā Protects P99
ā Self-healing
ā Production-grade
š§© Solution 4 ā Priority-Based Load Shedding ā
Idea
Not all requests are equal.
ā Code
app.get("/data", async (req, res) => {
if (overloaded && req.headers["x-priority"] !== "high") {
return res.status(503).send("Shed");
}
res.send(await db.fetch());
});
ā Premium users survive
ā Internal traffic survives
ā Graceful degradation
š§© Solution 5 ā Queue with Backpressure (Correct Way)
Queues must be bounded.
ā Bad Queue
Infinite size
Hides overload
ā Good Queue
const queue = [];
const MAX_QUEUE = 1000;
function enqueue(req) {
if (queue.length >= MAX_QUEUE) {
throw new Error("Queue Full");
}
queue.push(req);
}
š§ Backpressure vs Load Shedding
| Aspect | Backpressure | Load Shedding |
|---|---|---|
| Goal | Slow traffic | Drop traffic |
| User impact | Delay | Failure |
| Latency | Controlled | Minimal |
| Complexity | Medium | Low |
Healthy systems use both.
