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

Axum 0.8 Adaptive Load-Shedding & Concurrency Guardrail

Protects async Tokio worker runtimes from saturation-driven tail-latency collapse using Little's Law latency-gradient backpressure middleware. Under saturation, queuing requests internally causes latency inflation and thread starvation; systems must reject excess traffic at the ingress boundary using dynamically adjusted inflight limits and immediate HTTP 503 Service Unavailable responses with Retry-After headers.

Prohibited Anti-Pattern: Static Concurrency Caps and Unbounded Queue Buffering Inducing 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 & Concurrency Guardrail - Protects async Tokio worker runtimes from saturation-driven tail-latency collapse using Little's Law latency-gradient backpressure middleware. Under saturation, queuing requests internally causes latency inflation and thread starvation; systems must reject excess traffic at the ingress boundary using dynamically adjusted inflight limits and immediate HTTP 503 Service Unavailable responses with Retry-After headers.
globs: src/**/*.rs, crates/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-08-adaptive-load-shedding-concurrency-guardrail"
---

# Axum 0.8 Adaptive Load-Shedding & Concurrency Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Static Concurrency Caps and Unbounded Queue Buffering Inducing Tokio Worker Starvation

## System Constraints
You are a Principal Systems Architect enforcing resilience standards in Axum 0.8 and Tokio applications. Mandate Little's Law dynamic backpressure and adaptive load-shedding to prevent cascading queue buildup and tail-latency collapse. Reject unconstrained asynchronous request intake, unbounded channels, and static concurrency limits that fail to account for downstream latency gradients.

## Strict Guidelines
- Engineers MUST ALWAYS shed load early using HTTP 503 (Service Unavailable) with a calibrated 'Retry-After' header when inflight execution reaches dynamic latency thresholds.
- Engineers MUST NOT rely on static semaphore limits or unbound Tower buffers that queue incoming requests when downstream dependencies degrade.
- Middleware MUST measure end-to-end service time (latency gradient) and adjust the inflight concurrency ceiling using Little's Law (L = λ * W) or TCP Vegas-style latency trackers.
- Services MUST execute adaptive shed logic before parsing or deserializing large HTTP request bodies to save Tokio CPU time and allocation overhead.
- Components NEVER block cooperative Tokio execution threads with synchronous, CPU-intensive, or unbounded polling operations inside handler tasks.

## Bad vs Good Implementations
### Naive Static Concurrency Limit vs Adaptive Inflight Guard with Load Shedding

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: A static limit of 10,000 does not adapt to downstream latency changes. If DB latency increases from 5ms to 500ms, 10,000 concurrently held requests will exhaust system memory, saturate connection pools, and starve Tokio worker threads.
```rust
use axum::{routing::get, Router};
use tower::limit::ConcurrencyLimitLayer;

pub fn app() -> Router {
    // ANTI-PATTERN: A static concurrency limit paired with implicit unbounded queueing.
    // If downstream dependencies slow down, requests pile up inside the runtime,
    // inflating p999 tail latency and exhausting Tokio scheduler worker threads.
    Router::new()
        .route("/api/resource", get(|| async { "ok" }))
        .layer(ConcurrencyLimitLayer::new(10_000))
}
```

#### Production Standard (Correct)
> **Standard**: Tracks inflight tasks against a dynamic ceiling adjusted via latency feedback. Once the ceiling is reached, incoming requests are shed immediately with HTTP 503 and Retry-After without queuing, preventing Tokio runtime saturation.
```rust
use axum::{
    extract::Request,
    http::{header::RETRY_AFTER, HeaderValue, StatusCode},
    middleware::{self, Next},
    response::{IntoResponse, Response},
    routing::get, Router,
};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Instant;

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

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

pub async fn adaptive_load_shed_middleware(
    limiter: axum::extract::Extension<AdaptiveLimiter>,
    req: Request,
    next: Next,
) -> Response {
    let current = limiter.inflight.fetch_add(1, Ordering::SeqCst);
    let limit = limiter.max_concurrency.load(Ordering::Relaxed);

    if current >= limit {
        limiter.inflight.fetch_sub(1, Ordering::SeqCst);
        let mut res = (StatusCode::SERVICE_UNAVAILABLE, "System Overloaded").into_response();
        res.headers_mut().insert(RETRY_AFTER, HeaderValue::from_static("1"));
        return res;
    }

    let start = Instant::now();
    let response = next.run(req).await;
    let elapsed = start.elapsed();

    limiter.inflight.fetch_sub(1, Ordering::SeqCst);

    // Latency-gradient feedback: dynamically shed by contracting limit if RTT surges
    if elapsed.as_millis() > 200 && limit > 10 {
        limiter.max_concurrency.fetch_sub(1, Ordering::Relaxed);
    } else if elapsed.as_millis() < 50 {
        limiter.max_concurrency.fetch_add(1, Ordering::Relaxed);
    }

    response
}

pub fn app() -> Router {
    let limiter = AdaptiveLimiter::new(256);
    Router::new()
        .route("/api/resource", get(|| async { "ok" }))
        .layer(middleware::from_fn(move |req, next| {
            adaptive_load_shed_middleware(axum::extract::Extension(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 & Concurrency Guardrail

Axum 0.8 Adaptive Load-Shedding & Concurrency Guardrail defines an industrial-grade behavioral standard for backend engineering. Protects async Tokio worker runtimes from saturation-driven tail-latency collapse using Little's Law latency-gradient backpressure middleware. Under saturation, queuing requests internally causes latency inflation and thread starvation; systems must reject excess traffic at the ingress boundary using dynamically adjusted inflight limits and immediate HTTP 503 Service Unavailable responses with Retry-After headers. 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 Caps and Unbounded Queue Buffering Inducing 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 DriftStatic Concurrency Caps and Unbounded Queue Buffering Inducing 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 shed load early using HTTP 503 (Service Unavailable) with a calibrated 'Retry-After' header when inflight execution reaches dynamic latency thresholds.

Engineers MUST ALWAYS shed load early using HTTP 503 (Service Unavailable) with a calibrated 'Retry-After' header when inflight execution reaches dynamic latency thresholds.

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 semaphore limits or unbound Tower buffers that queue incoming requests when downstream dependencies degrade.

Engineers MUST NOT rely on static semaphore limits or unbound Tower buffers that queue incoming requests when downstream dependencies degrade.

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

Middleware MUST measure end-to-end service time (latency gradient) and adjust the inflight concurrency ceiling using Little's Law (L = λ * W) or TCP Vegas-style latency trackers.

Middleware MUST measure end-to-end service time (latency gradient) and adjust the inflight concurrency ceiling using Little's Law (L = λ * W) or TCP Vegas-style latency trackers.

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 execute adaptive shed logic before parsing or deserializing large HTTP request bodies to save Tokio CPU time and allocation overhead.

Services MUST execute adaptive shed logic before parsing or deserializing large HTTP request bodies to save Tokio CPU time and allocation overhead.

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 block cooperative Tokio execution threads with synchronous, CPU-intensive, or unbounded polling operations inside handler tasks.

Components NEVER block cooperative Tokio execution threads with synchronous, CPU-intensive, or unbounded polling operations inside handler tasks.

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

Protects async Tokio worker runtimes from saturation-driven tail-latency collapse using Little's Law latency-gradient backpressure middleware. Under saturation, queuing requests internally causes latency inflation and thread starvation; systems must reject excess traffic at the ingress boundary using dynamically adjusted inflight limits and immediate HTTP 503 Service Unavailable responses with Retry-After headers. 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 Caps and Unbounded Queue Buffering Inducing 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