Skip to main content
← CONCEPTS / DISTRIBUTED SYSTEMS
An end-to-end domain learning journey

CoDel (Controlled Delay) &
Sojourn-Time Load Shedding.

CoDel tracks how long requests actually wait in a queue (sojourn time) over a sliding 100 ms window and drops head-of-queue expired items at a 1/sqrt(n) cadence when minimum sojourn exceeds a 5 ms target.
Start the journey5 sections· Last modified Nov 27, 2026
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

When static queue limits cause goodput collapse under overload, use CoDel sojourn shedding with per-tenant FQ-CoDel queues.

TL;DR30-second simplified summary
▾

By tracking dequeue_ts - enqueue_ts over 100 ms intervals and ramping drops at interval / sqrt(N), CoDel absorbs short bursts while shedding standing overload automatically.

Before you begin · Building blocksAlready know these? Jump to Stage 01 ↓
Little's Law & Queueing Delay
Know L = lambda * W: a queue of 1,000 items takes 100 ms when draining at 10,000 req/s, but 10 seconds when downstream slows to 100 req/s.
Monotonic Clocks
Understand why enqueue_ts and dequeue_ts must use a monotonic clock (CLOCK_MONOTONIC) rather than wall-clock time.
Full walkthrough · 5 stages
01
L4 · FUNDAMENTALS

P · Problem & Why Static Queue Depth Collapses Goodput.

Suppose your service has a 1,000-slot FIFO queue and a 500 ms client timeout. Normally it processes 5,000 req/s, so a full queue drains in 200 ms.

Now a slow database query drops throughput to 500 req/s. A full 1,000-slot queue now takes 2,000 ms to drain! Every single request sits in the queue for 2 seconds, times out on the client after 500 ms, and gets retried—yet workers still execute all 1,000 dead requests, dropping useful goodput to 0%.

Queue Overload Behavior

Static FIFO Depth (2,000 ms Stale Queue) vs. CoDel Sojourn (<10 ms)

Before · Static Queue Depth (1,000)
  • Measures item count, not wait time
  • Standing queue hits 2,000 ms
  • 0% goodput on dead retries
After · CoDel Sojourn Shedding
  • Measures dequeue_ts - enqueue_ts
  • 5 ms target / 100 ms window
  • Sheds stale items; keeps ~100% goodput
02
L4 · FUNDAMENTALS

R · Representation: Sojourn Time, Target & Interval.

CoDel attaches a monotonic enqueue_ts to every queued request. On dequeue, it computes sojourn = now - enqueue_ts.

A short traffic burst might push sojourn to 30 ms for 20 ms, then drain back to 0 ms—that's healthy buffering, so CoDel does NOT drop! Only if sojourn stays above target (5 ms) continuously for an entire interval (100 ms) does CoDel classify the queue as a standing bottleneck.

Sojourn Measurement

Enqueue Timestamp -> Dequeue Sojourn Check -> Drop or Serve

Incoming ReqCoDel QueueSojourn CheckWorker Pool1. Tag monotonic enqueue_ts2. Compute sojourn = now -enqueue_ts3. sojourn > 5ms for 100ms -> Drophead4. Fresh request -> Execute
03
L5 · APPLIED DESIGN

I · Invariant: 1 / sqrt(N) Drop Cadence & Head-Drop.

Once CoDel enters dropping mode, it shouldn't drop every single request at once (which would cause wild oscillation), nor drop too slowly.

CoDel schedules the next drop at next_drop = now + interval / sqrt(drop_count). Drop #1 waits 100 ms, Drop #2 waits 70.7 ms, Drop #4 waits 50 ms, smoothly ramping shedding rate to match the overload slope until sojourn < target.

CoDel Control Loop

Healthy -> Above Target (100 ms) -> Dropping (1/sqrt(N)) -> Recovered

sojourn >= targetstayed > 100 mssojourn < targetHealthy (<5 ms)Sojourn > 5 msDropping 1/sqrt(N)
04
L5 · APPLIED DESIGN

M · Mechanics at Scale: FQ-CoDel & Tenant Isolation.

If all tenants share a single CoDel queue, one tenant flooding 100K RPS pushes sojourn time above 5 ms and causes CoDel to drop innocent tenants' requests.

FQ-CoDel (Fair Queueing + CoDel) hashes requests by tenant_id (or priority class) into separate virtual queues scheduled via Deficit Round-Robin (DRR), running CoDel independently per queue.

FQ-CoDel Isolation

Tenant Hash -> Per-Tenant CoDel Queues -> DRR Scheduler -> Workers

Tenant / PriorityClassifieringressP0 / Healthy TenantCoDelcomputeNoisy Tenant CoDel(Shed)storageDRR Worker Dispatchcontrolisolated queueflood queue0 dropshead-drop stale
Engineering Decision Framework
Problem
How to shed overload without tuning brittle queue-depth constants or hurting innocent tenants.
Naive Solution
Use a single shared FIFO queue with a fixed max_size = 1000.
Why It Fails
Fixed depth cannot adapt when downstream throughput drops, and a single queue lets one noisy tenant starve everyone.
Alternatives
Static FIFO tail-drop vs. Single CoDel vs. FQ-CoDel (per-tenant/priority Deficit Round-Robin + CoDel).
Engineering Decision
Use CoDel sojourn-time shedding paired with per-tenant/priority queues (FQ-CoDel) and explicit 503 + Retry-After responses.
Trade-Offs
Requires slightly more memory for per-tenant/priority buckets in exchange for automatic adaptation and noisy-neighbor isolation.
05
L6 · SCALE & TRADE-OFFS

E · Edge Cases, Assumptions & When NOT to Use CoDel.

CoDel assumes requests are short-lived RPCs where waiting in a queue longer than target (5 ms) signals overload.

When NOT to use CoDel: (1) Long-running offline batch/ETL queues (e.g., Spark/Celery jobs meant to wait minutes in a backlog) should use backpressure or autoscaling, not 5 ms CoDel drops; (2) If clients immediately retry 503 without exponential backoff + jitter, shedding only shifts load to retry parsing.

Applicability Matrix

Low-Latency RPC (CoDel) vs. Batch Backlog (Autoscaled Queue)

Sync RPC (<50ms)CoDel 5ms/100msSlow AI/DB (500ms)Scale Target ~50msAsync Batch (mins)Do NOT Use CoDel
Decision Exercise · Make the Call

A background video-transcoding job queue intentionally buffers jobs for 2–5 minutes during traffic spikes. An engineer enables default CoDel (target = 5 ms, interval = 100 ms) on that queue. What happens?

Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

Why does CoDel measure queue sojourn time (`dequeue_ts - enqueue_ts`) instead of queue item count?

Recommended Learning Flow