When designing a priority load-shedding gateway, pair CoDel sojourn queues with Gradient2 Little's Law limits and Retry-After budgets.
TL;DR30-second simplified summary▾
Per-pod CoDel + Gradient2 detects downstream slowdowns locally with zero Redis hops, shedding P3/P2 and retries first while reserving a 30% P0 floor.
target = 5 ms / interval = 100 ms detects standing queues and sheds stale requests.L = lambda * W) dynamically adjusts in-flight concurrency limits from RTT_min vs. RTT_sample.R & E · Requirements, Little's Law & Overload Math.
By Little's Law (L = lambda * W), 500,000 req/s with a healthy 10 ms downstream service time holds L = 500,000 * 0.010 = 5,000 concurrent in-flight requests across the gateway fleet.
Illustrative Sizing: Suppose 100 gateway pods handle 5,000 RPS each (L = 50 in-flight/pod). If a downstream DB slows 5x (W = 50 ms) while traffic bursts 3x (1.5M RPS), unthrottled concurrency would jump to L = 1,500,000 * 0.050 = 75,000 (15x overload!). By capping per-pod concurrency dynamically at ~50–65 and shedding P3 (30% of traffic) + P2 (25% of traffic) early, P0/P1 (45% of traffic) stays inside the healthy capacity envelope.
Unbounded FIFO Gateway vs. Priority CoDel + Little's Law
- 75,000 in-flight saturates DB
- P3 analytics starves P0 checkout
- 3x retries collapse gateway CPU
- P3/P2 shed at edge (<1 ms)
- P0/P1 stays at 100% goodput
- Retry-After + 10% retry budget
S · Policy & Priority Tier Classification.
Never trust a client-supplied X-Priority: P0 header—every caller would mark their traffic P0!
The edge gateway derives criticality deterministically from route + authenticated tenant tier: e.g., POST /v1/checkout & /v1/auth -> P0; GET /v1/catalog -> P1; POST /v1/telemetry -> P2; POST /v1/export -> P3. Each gateway pod maintains 4 lock-free ring queues (Q0..Q3) with independent CoDel sojourn timers.
Route Classifier -> P0..P3 CoDel Queues -> Gradient2 Permit Pool
H & A · Gateway Data Plane & Adaptive Concurrency.
Each gateway pod runs its own local Gradient2 + CoDel controller—zero cross-pod Redis round-trips on the hot path!
When a request arrives, the pod checks in_flight < limit. If limit is reached, it immediately sheds the lowest-priority queued item (P3, then P2) rather than dropping an incoming P0! As downstream responses return, Gradient2 updates limit_new = limit * (RTT_min / RTT_sample) + queue_allowance, automatically shrinking limit during DB slowdowns.
Edge Classify -> Priority CoDel -> Gradient2 Permit -> Upstream / 503
D · Detailed Design: Anti-Retry-Storm & Edge Shedding.
Shedding a request at the gateway only saves downstream CPU—if 500K clients retry immediately, the gateway itself still spends CPU on TLS and routing.
Three defenses stop retry storms: (1) Return HTTP 503 with a jittered Retry-After header (e.g., 2–5s for P2/P3); (2) Enforce a 10% retry-ratio token bucket in official SDKs (retry_tokens refilled by successful responses); (3) Tag retried requests (X-Retry-Attempt: 1) so CoDel drops retries before fresh P0 attempts during overload.
Normal (All Tiers) -> Shed P3 -> Shed P2+Retries -> P0 Floor Protected
- Problem
- How to protect P0 checkout traffic and prevent retry storms during 3x spikes or 5x DB slowdowns.
- Naive Solution
- Use a static global rate limit and blind client retries on 503.
- Why It Fails
- Static rate limits can't adapt when DB latency rises 5x, and blind retries turn 3x spikes into 10x metastable outages.
- Alternatives
- Static global token bucket vs. Priority CoDel + Gradient2 + Retry Budgets.
- Engineering Decision
- Run per-pod Priority CoDel + Gradient2 concurrency limits, reserve a P0 floor, and enforce 10% SDK retry budgets + jittered Retry-After.
- Trade-Offs
- Requires maintaining route-to-priority mappings and SDK retry-budget discipline in exchange for autonomous overload survival.
During a DB slowdown, your gateway sheds 40% of P2/P3 requests with HTTP 503, but gateway CPU jumps to 100% and P0 latency spikes anyway. Logs show X-Retry-Attempt on 70% of incoming traffic. What is missing?
E & D · Evaluation, Assumptions, Limitations & Glossary.
This design assumes gateway pods are stateless per-request and that downstream services degrade gracefully when concurrency is capped.
Assumptions & Limitations: Sizing assumes 500K RPS normal / 1.5M RPS burst across 100 pods with P0/P1 <= 50% of total traffic. Because Gradient2 relies on RTT_min as its baseline, periodically decay or window RTT_min (e.g., reset every 5–10 minutes) so a topology change or cross-AZ shift doesn't permanently pin RTT_min to a stale value.
Route Classifier -> Priority CoDel + Gradient2 -> Downstream + 503 Retry-After
Practice: Make the trade-off yourself.
Why must the API gateway derive request criticality (`P0`–`P3`) from route + authenticated tenant metadata rather than a client-supplied header?