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

Axum 0.8 Adaptive Queue-Latency Load Shedding & Concurrency Limiting

Enforces dynamic, latency-driven load-shedding and adaptive concurrency control at the Tower service boundary in Axum 0.8. Shields async worker runtimes from catastrophic queue tail-latency collapse and Tokio thread pool starvation under burst traffic.

Prohibited Anti-Pattern: Static Unbounded Concurrency with In-Handler Latency Checks Causing Worker Thread Starvation

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 Adaptive Queue-Latency Load Shedding & Concurrency Limiting - Enforces dynamic, latency-driven load-shedding and adaptive concurrency control at the Tower service boundary in Axum 0.8. Shields async worker runtimes from catastrophic queue tail-latency collapse and Tokio thread pool starvation under burst traffic.
globs: src/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/rust-axum-adaptive-load-shedding-concurrency"
---

# Axum 0.8 Adaptive Queue-Latency Load Shedding & Concurrency Limiting

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Static Unbounded Concurrency with In-Handler Latency Checks Causing Worker Thread Starvation

## System Constraints
You are a Principal Systems Architect specializing in high-throughput, low-latency Rust services with Axum 0.8 and Tokio. You enforce zero-cost abstraction safety, strict backpressure, and dynamic adaptive concurrency limits. You reject static hard-coded worker pools and prevent tail-latency death spirals by shedding load at the Tower service layer before request body consumption and thread scheduling.

## Strict Guidelines
- Services MUST measure socket-to-handler queue latency (the time between connection acceptance and service execution) before pulling request payloads into memory.
- Engineers MUST NOT rely on static Tower `ConcurrencyLimitLayer` instances with arbitrary thresholds, as static limits fail during downstream degradation and underutilize runtimes during low-latency regimes.
- Engineers MUST reject shed requests immediately at the middleware layer using `StatusCode::SERVICE_UNAVAILABLE` (HTTP 503) along with a deterministic `Retry-After` header.
- Code MUST employ an adaptive gradient or windowed latency tracking algorithm (e.g., CoDel, Vegas, or EMA gradient estimators) to adjust concurrency ceilings dynamically based on measured RTT/queue delay against target SLOs.
- Components NEVER buffer unbounded request bodies (e.g., `axum::extract::Json` or `Bytes`) prior to passing concurrency admission gates, preventing memory exhaustion under sustained queue backlogs.
- Middleware MUST record latency metrics (P50, P90, P99) and load-shed drop counts into an atomic sliding-window tracker or Prometheus/OpenTelemetry counter.

## Bad vs Good Implementations
### Adaptive Latency-Based Load Shedding Middleware in Axum 0.8

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Static concurrency limits blindly permit 1,000 tasks regardless of whether downstream dependencies are failing. Request bodies are deserialized before any admission control, consuming memory. Handling overload *inside* the route handler means Tokio worker threads are already committed, causing queue build-up and latency collapse.
```rust
use axum::{routing::post, Router, Json};
use tower::limit::ConcurrencyLimitLayer;
use serde::Deserialize;
use std::time::Instant;

#[derive(Deserialize)]
struct HeavyPayload { data: Vec<u8> }

// ANTI-PATTERN: Static concurrency limits do not adapt to downstream slowness.
// Consuming JSON payloads before checking queue health starves memory and Tokio workers.
async fn bad_handler(Json(payload): Json<HeavyPayload>) -> &'static str {
    let start = Instant::now();
    // In-handler latency checks are too late; worker thread is already consumed
    if start.elapsed().as_millis() > 500 {
        panic!("Queue overload"); // Catastrophic tail-latency collapse
    }
    "ok"
}

pub fn app() -> Router {
    Router::new()
        .route("/process", post(bad_handler))
        // Naive static limit: drops or stalls requests arbitrarily without backpressure guidance
        .layer(ConcurrencyLimitLayer::new(1000))
}
```

#### Production Standard (Correct)
> **Standard**: Adheres to Axum 0.8 patterns by intercepting requests at the middleware layer before body extraction. It manages an adaptive concurrency envelope (gradient descent on observed round-trip latency against a 50ms target SLO). Over-saturated requests immediately fail fast with HTTP 503 and a `Retry-After: 1` header, eliminating queue buildup and protecting Tokio workers from starvation.
```rust
use axum::{
    body::Body,
    extract::Request,
    http::{header, HeaderValue, StatusCode},
    middleware::{self, Next},
    response::{IntoResponse, Response},
    routing::post,
    Router,
};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

#[derive(Clone)]
pub struct LatencyShedController {
    inflight: Arc<AtomicUsize>,
    max_concurrency: Arc<AtomicUsize>,
    target_latency: Duration,
}

impl LatencyShedController {
    pub fn new(target_latency: Duration, initial_limit: usize) -> Self {
        Self {
            inflight: Arc::new(AtomicUsize::new(0)),
            max_concurrency: Arc::new(AtomicUsize::new(initial_limit)),
            target_latency,
        }
    }

    pub fn try_acquire(&self) -> Option<LatencyPermit> {
        let current = self.inflight.fetch_add(1, Ordering::SeqCst);
        let limit = self.max_concurrency.load(Ordering::Relaxed);
        
        if current >= limit {
            self.inflight.fetch_sub(1, Ordering::SeqCst);
            None
        } else {
            Some(LatencyPermit { controller: self.clone(), start: Instant::now() })
        }
    }

    fn release(&self, elapsed: Duration) {
        self.inflight.fetch_sub(1, Ordering::SeqCst);
        // Gradient-style adaptive concurrency adjustment
        let current_limit = self.max_concurrency.load(Ordering::Relaxed);
        if elapsed > self.target_latency && current_limit > 10 {
            // Shed: throttle concurrency limit down during latency spikes
            self.max_concurrency.fetch_sub(1, Ordering::Relaxed);
        } else if elapsed < self.target_latency && current_limit < 5000 {
            // Probe: expand concurrency limit when within SLO
            self.max_concurrency.fetch_add(1, Ordering::Relaxed);
        }
    }
}

pub struct LatencyPermit {
    controller: LatencyShedController,
    start: Instant,
}

impl Drop for LatencyPermit {
    fn drop(&mut self) {
        self.controller.release(self.start.elapsed());
    }
}

pub async fn load_shed_middleware(
    controller: LatencyShedController,
    req: Request<Body>,
    next: Next,
) -> Response {
    let permit = match controller.try_acquire() {
        Some(permit) => permit,
        None => {
            // Fast-fail before body buffering with 503 and Retry-After header
            return (
                StatusCode::SERVICE_UNAVAILABLE,
                [(header::RETRY_AFTER, HeaderValue::from_static("1"))],
                "Service Unavailable: Adaptive Load Shedding",
            ).into_response();
        }
    };

    let response = next.run(req).await;
    drop(permit);
    response
}

pub fn app() -> Router {
    let controller = LatencyShedController::new(Duration::from_millis(50), 256);
    Router::new()
        .route("/process", post(|| async { "processed safely" }))
        .layer(middleware::from_fn(move |req, next| {
            load_shed_middleware(controller.clone(), req, next)
        }))
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Axum 0.8 Adaptive Queue-Latency Load Shedding & Concurrency Limiting

Axum 0.8 Adaptive Queue-Latency Load Shedding & Concurrency Limiting defines an industrial-grade behavioral standard for backend engineering. Enforces dynamic, latency-driven load-shedding and adaptive concurrency control at the Tower service boundary in Axum 0.8. Shields async worker runtimes from catastrophic queue tail-latency collapse and Tokio thread pool starvation under burst traffic. 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 Unbounded Concurrency with In-Handler Latency Checks Causing Worker Thread Starvation 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 Unbounded Concurrency with In-Handler Latency Checks Causing Worker Thread StarvationCritical
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

Services MUST measure socket-to-handler queue latency (the time between connection acceptance and service execution) before pulling request payloads into memory.

Services MUST measure socket-to-handler queue latency (the time between connection acceptance and service execution) before pulling request payloads into memory.

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 rely on static Tower `ConcurrencyLimitLayer` instances with arbitrary thresholds, as static limits fail during downstream degradation and underutilize runtimes during low-latency regimes.

Engineers MUST NOT rely on static Tower `ConcurrencyLimitLayer` instances with arbitrary thresholds, as static limits fail during downstream degradation and underutilize runtimes during low-latency regimes.

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

Engineers MUST reject shed requests immediately at the middleware layer using `StatusCode

Engineers MUST reject shed requests immediately at the middleware layer using `StatusCode::SERVICE_UNAVAILABLE` (HTTP 503) along with a deterministic `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.
4

Code MUST employ an adaptive gradient or windowed latency tracking algorithm (e.g., CoDel, Vegas, or EMA gradient estimators) to adjust concurrency ceilings dynamically based on measured RTT/queue delay against target SLOs.

Code MUST employ an adaptive gradient or windowed latency tracking algorithm (e.g., CoDel, Vegas, or EMA gradient estimators) to adjust concurrency ceilings dynamically based on measured RTT/queue delay against target SLOs.

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 buffer unbounded request bodies (e.g., `axum

Components NEVER buffer unbounded request bodies (e.g., `axum::extract::Json` or `Bytes`) prior to passing concurrency admission gates, preventing memory exhaustion under sustained queue backlogs.

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.
6

Middleware MUST record latency metrics (P50, P90, P99) and load-shed drop counts into an atomic sliding-window tracker or Prometheus/OpenTelemetry counter.

Middleware MUST record latency metrics (P50, P90, P99) and load-shed drop counts into an atomic sliding-window tracker or Prometheus/OpenTelemetry counter.

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 Queue-Latency Load Shedding & Concurrency Limiting critical for AI coding assistants?

Enforces dynamic, latency-driven load-shedding and adaptive concurrency control at the Tower service boundary in Axum 0.8. Shields async worker runtimes from catastrophic queue tail-latency collapse and Tokio thread pool starvation under burst traffic. 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 Unbounded Concurrency with In-Handler Latency Checks Causing Worker Thread Starvation) 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