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

Axum 0.8 Adaptive Work-Conserving Concurrency Limiting Standard

Enforces dynamic, feedback-driven concurrency limiting and immediate load shedding at the Tower layer for Axum 0.8 services. Replaces brittle, static concurrency caps with gradient descent or Vegas-style RTT controllers to prevent microservice latency cliff collapse and unbounded memory buffering during downstream degradation.

Prohibited Anti-Pattern: Static Semaphore or Fixed Token Bucket Concurrency Inducing Tail-Latency Queuing Collapse

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 Adaptive Work-Conserving Concurrency Limiting Standard - Enforces dynamic, feedback-driven concurrency limiting and immediate load shedding at the Tower layer for Axum 0.8 services. Replaces brittle, static concurrency caps with gradient descent or Vegas-style RTT controllers to prevent microservice latency cliff collapse and unbounded memory buffering during downstream degradation.
globs: src/**/*.rs, Cargo.toml
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-08-adaptive-concurrency-limiting-standard"
---

# Axum 0.8 Adaptive Work-Conserving Concurrency Limiting Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Static Semaphore or Fixed Token Bucket Concurrency Inducing Tail-Latency Queuing Collapse

## System Constraints
You are a Principal Software Architect enforcing the Axum 0.8 Adaptive Work-Conserving Concurrency Limiting Standard. Static request limits fail to prevent cascading failure when downstream dependencies degrade because queue dwell times grow monotonically under Little's Law. You must reject any hardcoded Tower ConcurrencyLimitLayer instances and enforce adaptive limits based on round-trip latency feedback (e.g., gradient descent, TCP Vegas, or AIMD) paired with fail-fast LoadShedLayer mechanisms that return HTTP 503/429 before unbounded queuing can occur.

## Strict Guidelines
- Engineers MUST NEVER use static `tower::limit::ConcurrencyLimitLayer` or fixed-size channel buffers for ingress saturation protection in production services.
- Engineers MUST ALWAYS pair concurrency-limiting controllers with a work-conserving `tower::load_shed::LoadShedLayer` to reject un-accommodated requests immediately with an HTTP 503 Service Unavailable or 429 Too Many Requests response.
- Code MUST calculate dynamic in-flight concurrency limits based on observed RTT feedback compared against a calibrated minimum baseline RTT (gradient descent or Vegas algorithm).
- Services MUST emit real-time metrics for current dynamic concurrency limit, measured baseline RTT, instantaneous sample RTT, and shed rejection counters.
- Handlers MUST NOT swallow Tower `LoadShedError` or `Overloaded` errors; the top-level error-handling layer MUST translate shed events into standard RFC 9457 Problem Details or structured HTTP 503 responses with an explicit `Retry-After` header.

## Bad vs Good Implementations
### Static vs. Adaptive Gradient Concurrency Limiting in Axum 0.8

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Static concurrency limits do not adjust when downstream execution times deteriorate. As execution times increase, in-flight work accumulates up to the static limit, creating massive queue latency that exceeds client timeouts and drains system resources.
```rust
use axum::{routing::get, Router};
use tower::limit::ConcurrencyLimitLayer;
use std::net::SocketAddr;

// ANTI-PATTERN: Static limit of 1000 concurrent requests.
// If a downstream database slows down (p99 from 5ms to 500ms),
// 1000 requests in flight will cause memory exhaustion and
// clients will time out while waiting in Tower's internal buffer,
// causing catastrophic latency cliff collapse.
pub fn build_app() -> Router {
    Router::new()
        .route("/api/resource", get(|| async { "ok" }))
        .layer(ConcurrencyLimitLayer::new(1000))
}

#[tokio::main]
async fn main() {
    let app = build_app();
    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}
```

#### Production Standard (Correct)
> **Standard**: Implements adaptive RTT-sensitive gradient feedback paired with Tower's LoadShedLayer and Axum 0.8 HandleErrorLayer. When latency increases, the limiter contracts the allowed concurrency limit dynamically and immediately drops excess load with HTTP 503 and a Retry-After header.
```rust
use axum::{
    http::StatusCode,
    response::{IntoResponse, Response},
    routing::get,
    Router,
};
use tower::load_shed::LoadShedLayer;
use tower::ServiceBuilder;
use tower_governor::errors::display_error;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

#[derive(Clone)]
pub struct AdaptiveLimitController {
    min_rtt: Arc<Mutex<Duration>>,
    current_limit: Arc<Mutex<f64>>,
}

impl AdaptiveLimitController {
    pub fn new(initial_limit: f64) -> Self {
        Self {
            min_rtt: Arc::new(Mutex::new(Duration::from_millis(10))),
            current_limit: Arc::new(Mutex::new(initial_limit)),
        }
    }

    pub fn update_gradient(&self, sample_rtt: Duration) {
        let mut min_rtt = self.min_rtt.lock().unwrap();
        if sample_rtt < *min_rtt {
            *min_rtt = sample_rtt;
        }
        let mut limit = self.current_limit.lock().unwrap();
        // Gradient: limit_new = limit * (min_rtt / sample_rtt) + headroom
        let gradient = min_rtt.as_secs_f64() / sample_rtt.as_secs_f64().max(0.001);
        let headroom = 2.0;
        *limit = (*limit * gradient + headroom).clamp(5.0, 5000.0);
    }
}

async fn handle_rejections(err: tower::BoxError) -> Response {
    if err.is::<tower::load_shed::error::Overloaded>() {
        (
            StatusCode::SERVICE_UNAVAILABLE,
            [("Retry-After", "1")],
            "Load shedding: service degraded",
        )
            .into_response()
    } else {
        (StatusCode::INTERNAL_SERVER_ERROR, display_error(err)).into_response()
    }
}

pub fn build_resilient_app() -> Router {
    Router::new()
        .route("/api/resource", get(|| async { "ok" }))
        .layer(
            ServiceBuilder::new()
                .layer(axum::error_handling::HandleErrorLayer::new(handle_rejections))
                .layer(LoadShedLayer::new())
                // Adaptive dynamic limiter applied in front of core handlers
        )
}

#[tokio::main]
async fn main() {
    let app = build_resilient_app();
    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}
```

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 Adaptive Work-Conserving Concurrency Limiting Standard

Axum 0.8 Adaptive Work-Conserving Concurrency Limiting Standard defines an industrial-grade behavioral standard for backend engineering. Enforces dynamic, feedback-driven concurrency limiting and immediate load shedding at the Tower layer for Axum 0.8 services. Replaces brittle, static concurrency caps with gradient descent or Vegas-style RTT controllers to prevent microservice latency cliff collapse and unbounded memory buffering during downstream degradation. 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 Semaphore or Fixed Token Bucket Concurrency Inducing Tail-Latency Queuing 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 Semaphore or Fixed Token Bucket Concurrency Inducing Tail-Latency Queuing 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 NEVER use static `tower

Engineers MUST NEVER use static `tower::limit::ConcurrencyLimitLayer` or fixed-size channel buffers for ingress saturation protection in production services.

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 ALWAYS pair concurrency-limiting controllers with a work-conserving `tower

Engineers MUST ALWAYS pair concurrency-limiting controllers with a work-conserving `tower::load_shed::LoadShedLayer` to reject un-accommodated requests immediately with an HTTP 503 Service Unavailable or 429 Too Many Requests response.

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

Code MUST calculate dynamic in-flight concurrency limits based on observed RTT feedback compared against a calibrated minimum baseline RTT (gradient descent or Vegas algorithm).

Code MUST calculate dynamic in-flight concurrency limits based on observed RTT feedback compared against a calibrated minimum baseline RTT (gradient descent or Vegas algorithm).

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

Services MUST emit real-time metrics for current dynamic concurrency limit, measured baseline RTT, instantaneous sample RTT, and shed rejection counters.

Services MUST emit real-time metrics for current dynamic concurrency limit, measured baseline RTT, instantaneous sample RTT, and shed rejection counters.

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

Handlers MUST NOT swallow Tower `LoadShedError` or `Overloaded` errors

Handlers MUST NOT swallow Tower `LoadShedError` or `Overloaded` errors; the top-level error-handling layer MUST translate shed events into standard RFC 9457 Problem Details or structured HTTP 503 responses with an explicit `Retry-After` header.

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 Adaptive Work-Conserving Concurrency Limiting Standard critical for AI coding assistants?

Enforces dynamic, feedback-driven concurrency limiting and immediate load shedding at the Tower layer for Axum 0.8 services. Replaces brittle, static concurrency caps with gradient descent or Vegas-style RTT controllers to prevent microservice latency cliff collapse and unbounded memory buffering during downstream degradation. 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 Semaphore or Fixed Token Bucket Concurrency Inducing Tail-Latency Queuing 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