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

Axum 0.8 Adaptive Load-Shedding & Dynamic Concurrency Guardrail

Protects Tokio asynchronous runtime worker threads from catastrophic queue saturation and metastable latency collapse by enforcing dynamic Little's Law concurrency thresholds per route. Replaces static, unbounded channel buffers with adaptive latency-gradient load shedders that immediately reject excess traffic with HTTP 503 and accurate Retry-After directives. Guarantees that internal runtime task scheduling queues remain bounded under heavy p99 tail-latency degradation.

Prohibited Anti-Pattern: Unbounded Task Queuing and Static Concurrency Limits Leading to Tokio Worker Starvation

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 Adaptive Load-Shedding & Dynamic Concurrency Guardrail - Protects Tokio asynchronous runtime worker threads from catastrophic queue saturation and metastable latency collapse by enforcing dynamic Little's Law concurrency thresholds per route. Replaces static, unbounded channel buffers with adaptive latency-gradient load shedders that immediately reject excess traffic with HTTP 503 and accurate Retry-After directives. Guarantees that internal runtime task scheduling queues remain bounded under heavy p99 tail-latency degradation.
globs: src/**/*.rs, crates/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-adaptive-load-shedding-guardrail"
---

# Axum 0.8 Adaptive Load-Shedding & Dynamic Concurrency Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Task Queuing and Static Concurrency Limits Leading to Tokio Worker Starvation

## System Constraints
You are a Principal Systems Architect enforcing Axum 0.8 Adaptive Load-Shedding and Tokio runtime safety invariants. Mandate dynamic concurrency limits derived from Little's Law (L = λW) using gradient-based RTT observation. Prohibit unbounded task queuing, static request limits, and un-instrumented background spawning within Axum handlers. Ensure all shed requests return HTTP 503 Service Unavailable or 429 Too Many Requests before queue delays degrade async runtime worker availability.

## Strict Guidelines
- Engineers MUST ALWAYS enforce adaptive concurrency limits at the Tower service/middleware layer rather than relying on static global semaphores or unbounded channel queues.
- Engineers MUST NOT spawn detached Tokio tasks (`tokio::spawn`) within request handlers without propagating backpressure from downstream services.
- Code SHOULD dynamically adjust allowed in-flight requests (limit = throughput * target_latency) via latency-gradient estimators (e.g., Vegas, Gradient2, or CoDel algorithms) per route group.
- Services MUST reject excess requests with HTTP 503 (Service Unavailable) or HTTP 429 (Too Many Requests) including a computed `Retry-After` header as soon as observed RTT exceeds the Little's Law threshold.
- Components NEVER buffer inbound request payloads in memory when the route's concurrency controller signals queue saturation.

## Bad vs Good Implementations
### Adaptive Concurrency Limiting Middleware vs Static Unbounded Spawn

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The handler relies on a hardcoded static permit pool and blindly spawns tasks on the Tokio runtime. When downstream dependencies slow down, queue time inflates dramatically, permits stay locked, and the runtime suffers catastrophic task queue buildup without any visibility into Little's Law latency thresholds.
```rust
use axum::{routing::post, Router, response::IntoResponse, http::StatusCode};
use std::sync::Arc;
use tokio::sync::Semaphore;

// ANTI-PATTERN: Static semaphore with unbounded async spawn
// Under tail-latency spikes, tasks accumulate in Tokio run-queues,
// exhausting memory and starving worker threads without adaptive feedback.
static GLOBAL_PERMIT: once_cell::sync::Lazy<Arc<Semaphore>> = 
    once_cell::sync::Lazy::new(|| Arc::new(Semaphore::new(1000)));

async fn handle_workload() -> impl IntoResponse {
    let permit = GLOBAL_PERMIT.clone().acquire_owned().await.unwrap();
    tokio::spawn(async move {
        // Heavy compute/IO without latency-based shedding
        tokio::time::sleep(std::time::Duration::from_millis(500)).await;
        drop(permit);
    });
    StatusCode::ACCEPTED
}

pub fn app() -> Router {
    Router::new().route("/process", post(handle_workload))
}
```

#### Production Standard (Correct)
> **Standard**: Implements an adaptive concurrency middleware that samples latency (W) and current in-flight load (L). When response latency exceeds baseline RTT, the dynamic limit contracts, immediately shedding excess load with HTTP 503 and a Retry-After header before task queues saturate the Tokio worker thread pool.
```rust
use axum::{
    body::Body,
    extract::Request,
    http::{header::RETRY_AFTER, HeaderValue, StatusCode},
    middleware::{self, Next},
    response::{IntoResponse, Response},
    routing::post,
    Router,
};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Instant;

// PRODUCTION INVARIANT: Dynamic gradient concurrency tracker based on Little's Law (L = λ * W)
#[derive(Clone)]
pub struct AdaptiveLimiter {
    in_flight: Arc<AtomicUsize>,
    limit: Arc<AtomicUsize>,
    min_rtt_ms: Arc<AtomicUsize>,
}

impl AdaptiveLimiter {
    pub fn new(initial_limit: usize) -> Self {
        Self {
            in_flight: Arc::new(AtomicUsize::new(0)),
            limit: Arc::new(AtomicUsize::new(initial_limit)),
            min_rtt_ms: Arc::new(AtomicUsize::new(50)), // 50ms baseline
        }
    }

    pub fn try_acquire(&self) -> Result<PermitGuard, StatusCode> {
        let current = self.in_flight.fetch_add(1, Ordering::SeqCst);
        let max_allowed = self.limit.load(Ordering::Relaxed);

        if current >= max_allowed {
            self.in_flight.fetch_sub(1, Ordering::SeqCst);
            return Err(StatusCode::SERVICE_UNAVAILABLE);
        }
        Ok(PermitGuard { limiter: self.clone(), start: Instant::now() })
    }

    fn record_completion(&self, duration_ms: usize) {
        self.in_flight.fetch_sub(1, Ordering::SeqCst);
        let min_rtt = self.min_rtt_ms.load(Ordering::Relaxed).max(1);
        let current_limit = self.limit.load(Ordering::Relaxed);

        // Gradient: target / actual RTT applied to Little's Law threshold
        let gradient = (min_rtt as f64) / (duration_ms as f64).max(min_rtt as f64);
        let new_limit = ((current_limit as f64 * gradient) + 1.0).clamp(5.0, 500.0) as usize;
        self.limit.store(new_limit, Ordering::Relaxed);
    }
}

pub struct PermitGuard {
    limiter: AdaptiveLimiter,
    start: Instant,
}

impl Drop for PermitGuard {
    fn drop(&mut self) {
        let elapsed = self.start.elapsed().as_millis() as usize;
        self.limiter.record_completion(elapsed);
    }
}

async fn load_shedding_middleware(
    limiter: AdaptiveLimiter,
    req: Request<Body>,
    next: Next,
) -> Response {
    match limiter.try_acquire() {
        Ok(_permit) => next.run(req).await,
        Err(status) => {
            let mut res = status.into_response();
            res.headers_mut().insert(RETRY_AFTER, HeaderValue::from_static("1"));
            res
        }
    }
}

async fn process_handler() -> impl IntoResponse {
    StatusCode::OK
}

pub fn app() -> Router {
    let limiter = AdaptiveLimiter::new(64);
    Router::new()
        .route("/process", post(process_handler))
        .layer(middleware::from_fn(move |req, next| {
            load_shedding_middleware(limiter.clone(), req, next)
        }))
}
```

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 Load-Shedding & Dynamic Concurrency Guardrail

Axum 0.8 Adaptive Load-Shedding & Dynamic Concurrency Guardrail defines an industrial-grade behavioral standard for backend engineering. Protects Tokio asynchronous runtime worker threads from catastrophic queue saturation and metastable latency collapse by enforcing dynamic Little's Law concurrency thresholds per route. Replaces static, unbounded channel buffers with adaptive latency-gradient load shedders that immediately reject excess traffic with HTTP 503 and accurate Retry-After directives. Guarantees that internal runtime task scheduling queues remain bounded under heavy p99 tail-latency 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: Unbounded Task Queuing and Static Concurrency Limits Leading to Tokio Worker 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 DriftUnbounded Task Queuing and Static Concurrency Limits Leading to Tokio Worker 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

Engineers MUST ALWAYS enforce adaptive concurrency limits at the Tower service/middleware layer rather than relying on static global semaphores or unbounded channel queues.

Engineers MUST ALWAYS enforce adaptive concurrency limits at the Tower service/middleware layer rather than relying on static global semaphores or unbounded channel queues.

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 spawn detached Tokio tasks (`tokio

Engineers MUST NOT spawn detached Tokio tasks (`tokio::spawn`) within request handlers without propagating backpressure from downstream 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.
3

Code SHOULD dynamically adjust allowed in-flight requests (limit = throughput * target_latency) via latency-gradient estimators (e.g., Vegas, Gradient2, or CoDel algorithms) per route group.

Code SHOULD dynamically adjust allowed in-flight requests (limit = throughput * target_latency) via latency-gradient estimators (e.g., Vegas, Gradient2, or CoDel algorithms) per route group.

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 reject excess requests with HTTP 503 (Service Unavailable) or HTTP 429 (Too Many Requests) including a computed `Retry-After` header as soon as observed RTT exceeds the Little's Law threshold.

Services MUST reject excess requests with HTTP 503 (Service Unavailable) or HTTP 429 (Too Many Requests) including a computed `Retry-After` header as soon as observed RTT exceeds the Little's Law threshold.

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 inbound request payloads in memory when the route's concurrency controller signals queue saturation.

Components NEVER buffer inbound request payloads in memory when the route's concurrency controller signals queue saturation.

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 Load-Shedding & Dynamic Concurrency Guardrail critical for AI coding assistants?

Protects Tokio asynchronous runtime worker threads from catastrophic queue saturation and metastable latency collapse by enforcing dynamic Little's Law concurrency thresholds per route. Replaces static, unbounded channel buffers with adaptive latency-gradient load shedders that immediately reject excess traffic with HTTP 503 and accurate Retry-After directives. Guarantees that internal runtime task scheduling queues remain bounded under heavy p99 tail-latency 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 (Unbounded Task Queuing and Static Concurrency Limits Leading to Tokio Worker 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