This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: axum-codel-adaptive-concurrency-limit

Axum 0.8 CoDel Adaptive Concurrency Limiting and Bufferbloat Mitigation

Enforces deterministic tail latency in Axum 0.8 services by applying Controlled Delay (CoDel) adaptive concurrency limiting. Tracks queue residency (sojourn) time rather than static concurrency thresholds, dynamically shedding excess traffic with HTTP 503 when minimum queue delays exceed targets across sliding observation windows.

Prohibited Anti-Pattern: Static Concurrency Limits and Unbounded Buffering Inducing Bufferbloat and Latency Collapse

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 CoDel Adaptive Concurrency Limiting and Bufferbloat Mitigation - Enforces deterministic tail latency in Axum 0.8 services by applying Controlled Delay (CoDel) adaptive concurrency limiting. Tracks queue residency (sojourn) time rather than static concurrency thresholds, dynamically shedding excess traffic with HTTP 503 when minimum queue delays exceed targets across sliding observation windows.
globs: src/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-codel-adaptive-concurrency-limit"
---

# Axum 0.8 CoDel Adaptive Concurrency Limiting and Bufferbloat Mitigation

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Static Concurrency Limits and Unbounded Buffering Inducing Bufferbloat and Latency Collapse

## System Constraints
You are a Principal Rust Systems Architect specializing in high-throughput Axum 0.8 microservices and Tower ecosystems. Mandate strict adaptive load-shedding using CoDel (Controlled Delay) algorithms based on queue sojourn time. Eliminate naive static semaphore concurrency limits, unbounded Tower buffers, and unresponsive request accumulation that trigger bufferbloat, cascading failure, and p99 tail latency collapse.

## Strict Guidelines
- Engineers MUST ALWAYS measure queue residency (sojourn) time independently from execution latency to assess bufferbloat accurately.
- Engineers MUST NOT use unmetered static concurrency layers (e.g., raw `tower::limit::ConcurrencyLimitLayer` with arbitrary static constants) for ingress APIs susceptible to variable execution times.
- Services MUST return `StatusCode::SERVICE_UNAVAILABLE` (HTTP 503) or `StatusCode::TOO_MANY_REQUESTS` (HTTP 429) immediately upon CoDel load shedding without executing downstream extractors, middleware, or handler logic.
- Code SHOULD calibrate CoDel parameters with a default Target Sojourn Delay (e.g., 5ms–15ms) and an Observation Interval (e.g., 100ms) aligned with client timeout envelopes.
- Components NEVER allow unbounded buffers or channels between ingress network sockets and service execution pools.

## Bad vs Good Implementations
### Naive Static Concurrency Limit vs. Adaptive CoDel Load Shedding Layer

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Static concurrency limits coupled with large buffers cause bufferbloat. Inbound requests sit in the buffer queue while client-side timeouts expire, wasting downstream compute on dead requests and degrading tail latency under traffic surges.
```rust
use axum::{routing::get, Router};
use tower::limit::ConcurrencyLimitLayer;
use tower::buffer::BufferLayer;

// ANTI-PATTERN: Fixed concurrency limit with deep unbounded buffering.
// Under load spikes, requests wait hundreds of milliseconds in the buffer,
// causing extreme bufferbloat, client timeouts, and cascading server failure.
pub fn app() -> Router {
    Router::new()
        .route("/api/v1/resource", get(|| async { "processed" }))
        // Unbounded buffer retains stale requests while p99 spikes uncontrollably
        .layer(BufferLayer::new(10_000))
        // Static limit cannot adapt to database saturation or variable handler execution times
        .layer(ConcurrencyLimitLayer::new(100))
}
```

#### Production Standard (Correct)
> **Standard**: This implementation tracks queue residency (sojourn) time using a CoDel state engine. When queue wait times stay consistently above the 5ms target over a 100ms interval, the middleware actively sheds load with HTTP 503 Service Unavailable, preventing downstream bufferbloat and preserving stable p99 latencies for accepted traffic.
```rust
use axum::{
    extract::Request,
    http::StatusCode,
    response::{IntoResponse, Response},
    routing::get,
    Router,
};
use std::{
    sync::{Arc, Mutex},
    time::{Duration, Instant},
};
use tower::{Layer, Service};
use std::task::{Context, Poll};
use futures_util::future::BoxFuture;

#[derive(Clone)]
pub struct CoDelState {
    pub target: Duration,
    pub interval: Duration,
    pub state: Arc<Mutex<CoDelMetrics>>,
}

pub struct CoDelMetrics {
    pub first_above_time: Option<Instant>,
    pub drop_next: Instant,
    pub dropping: bool,
}

#[derive(Clone)]
pub struct CoDelLayer {
    target: Duration,
    interval: Duration,
    metrics: Arc<Mutex<CoDelMetrics>>,
}

impl CoDelLayer {
    pub fn new(target: Duration, interval: Duration) -> Self {
        Self {
            target,
            interval,
            metrics: Arc::new(Mutex::new(CoDelMetrics {
                first_above_time: None,
                drop_next: Instant::now(),
                dropping: false,
            })),
        }
    }
}

impl<S> Layer<S> for CoDelLayer {
    type Service = CoDelService<S>;
    fn layer(&self, inner: S) -> Self::Service {
        CoDelService {
            inner,
            target: self.target,
            interval: self.interval,
            metrics: self.metrics.clone(),
        }
    }
}

#[derive(Clone)]
pub struct CoDelService<S> {
    inner: S,
    target: Duration,
    interval: Duration,
    metrics: Arc<Mutex<CoDelMetrics>>,
}

impl<S> Service<Request> for CoDelService<S>
where
    S: Service<Request, Response = Response> + Send + Clone + 'static,
    S::Future: Send + 'static,
{
    type Response = Response;
    type Error = S::Error;
    type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, req: Request) -> Self::Future {
        let enqueued_at = Instant::now();
        let target = self.target;
        let interval = self.interval;
        let metrics_ref = self.metrics.clone();
        let mut inner = self.inner.clone();

        Box::pin(async move {
            let sojourn_time = enqueued_at.elapsed();
            let now = Instant::now();
            let mut drop_request = false;

            {
                let mut metrics = metrics_ref.lock().unwrap();
                if sojourn_time > target {
                    if metrics.first_above_time.is_none() {
                        metrics.first_above_time = Some(now + interval);
                    } else if now >= metrics.first_above_time.unwrap() {
                        metrics.dropping = true;
                        if now >= metrics.drop_next {
                            drop_request = true;
                            metrics.drop_next = now + target; // Shed control interval
                        }
                    }
                } else {
                    metrics.first_above_time = None;
                    metrics.dropping = false;
                }
            }

            if drop_request {
                return Ok((
                    StatusCode::SERVICE_UNAVAILABLE,
                    [("Retry-After", "1")],
                    "Overloaded: Queue sojourn time exceeded target threshold",
                ).into_response());
            }

            inner.call(req).await
        })
    }
}

pub fn app() -> Router {
    Router::new()
        .route("/api/v1/resource", get(|| async { "processed successfully" }))
        .layer(CoDelLayer::new(Duration::from_millis(5), Duration::from_millis(100)))
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Axum 0.8 CoDel Adaptive Concurrency Limiting and Bufferbloat Mitigation

Axum 0.8 CoDel Adaptive Concurrency Limiting and Bufferbloat Mitigation defines an industrial-grade behavioral standard for backend engineering. Enforces deterministic tail latency in Axum 0.8 services by applying Controlled Delay (CoDel) adaptive concurrency limiting. Tracks queue residency (sojourn) time rather than static concurrency thresholds, dynamically shedding excess traffic with HTTP 503 when minimum queue delays exceed targets across sliding observation windows. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Static Concurrency Limits and Unbounded Buffering Inducing Bufferbloat and Latency Collapse Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftStatic Concurrency Limits and Unbounded Buffering Inducing Bufferbloat and Latency CollapseCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS measure queue residency (sojourn) time independently from execution latency to assess bufferbloat accurately.

Engineers MUST ALWAYS measure queue residency (sojourn) time independently from execution latency to assess bufferbloat accurately.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NOT use unmetered static concurrency layers (e.g., raw `tower

Engineers MUST NOT use unmetered static concurrency layers (e.g., raw `tower::limit::ConcurrencyLimitLayer` with arbitrary static constants) for ingress APIs susceptible to variable execution times.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Services MUST return `StatusCode

Services MUST return `StatusCode::SERVICE_UNAVAILABLE` (HTTP 503) or `StatusCode::TOO_MANY_REQUESTS` (HTTP 429) immediately upon CoDel load shedding without executing downstream extractors, middleware, or handler logic.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Code SHOULD calibrate CoDel parameters with a default Target Sojourn Delay (e.g., 5ms–15ms) and an Observation Interval (e.g., 100ms) aligned with client timeout envelopes.

Code SHOULD calibrate CoDel parameters with a default Target Sojourn Delay (e.g., 5ms–15ms) and an Observation Interval (e.g., 100ms) aligned with client timeout envelopes.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Components NEVER allow unbounded buffers or channels between ingress network sockets and service execution pools.

Components NEVER allow unbounded buffers or channels between ingress network sockets and service execution pools.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Axum 0.8 CoDel Adaptive Concurrency Limiting and Bufferbloat Mitigation critical for AI coding assistants?

Enforces deterministic tail latency in Axum 0.8 services by applying Controlled Delay (CoDel) adaptive concurrency limiting. Tracks queue residency (sojourn) time rather than static concurrency thresholds, dynamically shedding excess traffic with HTTP 503 when minimum queue delays exceed targets across sliding observation windows. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Static Concurrency Limits and Unbounded Buffering Inducing Bufferbloat and Latency Collapse) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

Axum