Skip to main content
← PROBLEMS / ARCHITECTURE
An end-to-end domain learning journey

Design a 500K-RPS Priority-Aware Load Shedding
Gateway with CoDel & Little's Law.

Combine a Deficit Round-Robin + CoDel priority scheduler with Little's Law adaptive concurrency limits so P3/P2 traffic sheds with 503 + Retry-After before P0/P1 queues back up.
Start the journey5 stages· Last modified Nov 27, 2026
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

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.

Before you begin · Building blocksAlready know these? Jump to Stage 01 ↓
CoDel Sojourn-Time Load Shedding
Know how target = 5 ms / interval = 100 ms detects standing queues and sheds stale requests.
Adaptive Concurrency Limiting (Gradient2)
Understand how Little's Law (L = lambda * W) dynamically adjusts in-flight concurrency limits from RTT_min vs. RTT_sample.
Full walkthrough · 5 stages
01
L4 · FUNDAMENTALS

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.

Overload Protection

Unbounded FIFO Gateway vs. Priority CoDel + Little's Law

Before · Unbounded Shared Queue
  • 75,000 in-flight saturates DB
  • P3 analytics starves P0 checkout
  • 3x retries collapse gateway CPU
After · Priority CoDel + Gradient2
  • P3/P2 shed at edge (<1 ms)
  • P0/P1 stays at 100% goodput
  • Retry-After + 10% retry budget
02
L4 · FUNDAMENTALS

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.

4-Tier Admission

Route Classifier -> P0..P3 CoDel Queues -> Gradient2 Permit Pool

P0 Checkout/AuthP1 Core ReadsP2 AnalyticsP3 Background
03
L5 · APPLIED DESIGN

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.

Admission & Shedding Flow

Edge Classify -> Priority CoDel -> Gradient2 Permit -> Upstream / 503

ClientGateway ClassifierCoDel + Gradient2Downstream Pool1. Request (route -> P0..P3)2. Check limit & sojourn3. Admit P0/P1; update RTT_min4. Shed P2/P3 -> 503 + Retry-After
04
L5 · APPLIED DESIGN

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.

Gateway Overload State

Normal (All Tiers) -> Shed P3 -> Shed P2+Retries -> P0 Floor Protected

L > 70% limitCoDel sojourn > 5ms30% P0 floorAll Tiers AdmittedShed P3 + RetriesShed P2 + P3 (503)P0 Floor Protected
Engineering Decision Framework
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.
Decision Exercise · Make the Call

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?

05
L6 · SCALE & TRADE-OFFS

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.

End-to-End Edge Gateway

Route Classifier -> Priority CoDel + Gradient2 -> Downstream + 503 Retry-After

Route Classifier(P0-P3)ingressDRR + CoDel QueuescomputeGradient2ConcurrencycontrolHealthy Downstreamstoragetier tagadmit / shed 503bounded L = lambda*W
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

Why must the API gateway derive request criticality (`P0`–`P3`) from route + authenticated tenant metadata rather than a client-supplied header?

Recommended Learning Flow