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

Axum 0.8 Adaptive Concurrency Shedding via Dynamic Latency Telemetry

Enforces dynamic, TCP/RTT-telemetry-driven concurrency limits within Axum 0.8 microservices to replace rigid static concurrency caps. Prevents catastrophic cascading failures and thread starvation caused by distributed tail-latency spikes and queuing delays governed by Little's Law. Ensures backpressure manifests as instantaneous HTTP 503 load-shed responses with zero-allocation drops rather than unbounded heap growth or socket pileups.

Prohibited Anti-Pattern: Static Concurrency Caps and Unbounded Tower Buffering Inducing Latency Cascades

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 Adaptive Concurrency Shedding via Dynamic Latency Telemetry - Enforces dynamic, TCP/RTT-telemetry-driven concurrency limits within Axum 0.8 microservices to replace rigid static concurrency caps. Prevents catastrophic cascading failures and thread starvation caused by distributed tail-latency spikes and queuing delays governed by Little's Law. Ensures backpressure manifests as instantaneous HTTP 503 load-shed responses with zero-allocation drops rather than unbounded heap growth or socket pileups.
globs: src/**/*.rs, crates/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-0-8-adaptive-concurrency-shedding-invariant"
---

# Axum 0.8 Adaptive Concurrency Shedding via Dynamic Latency Telemetry

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Static Concurrency Caps and Unbounded Tower Buffering Inducing Latency Cascades

## System Constraints
You are a Principal Rust Distributed Systems Engineer enforcing Axum 0.8 service resilience invariants. Mandate adaptive concurrency limits driven by request latency or round-trip telemetry (e.g., Vegas, gradient, or AIMD estimators) instead of static buffer or concurrency limits. Ensure tower-based load shedding rejects excess load immediately with HTTP 503 Service Unavailable, preserving p99 latencies under heavy upstream distress.

## Strict Guidelines
- Engineers MUST ALWAYS enforce adaptive concurrency shedding driven by observed latency (RTT) versus baseline latency (RTT_min) rather than static numerical ceilings (e.g., fixed `tower::limit::ConcurrencyLimitLayer`).
- Engineers MUST NOT utilize unbounded in-memory queues (`tower::buffer::BufferLayer` with large or unbounded capacity) ahead of downstream I/O bounds in Axum services.
- Code MUST return an immediate `503 Service Unavailable` with `Retry-After: 0` headers upon adaptive limit saturation to allow upstream meshes/load balancers to reroute.
- Services MUST continuously track sample minimum round-trip time (`min_rtt`) over rolling time windows to distinguish between true system saturation and natural workload variance.
- Components NEVER hold open stateful streaming connections or parse large request bodies prior to passing through the load-shedding middleware boundary.

## Bad vs Good Implementations
### Adaptive Concurrency Shedding vs. Static Queue Saturation in Axum 0.8

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: A static ConcurrencyLimitLayer does not account for downstream latency degradation. If request processing latency increases from 10ms to 2000ms, the static concurrency limit leads to severe queuing, memory consumption, and eventual cascading timeouts across all upstream clients.
```rust
use axum::{routing::get, Router};
use tower::limit::ConcurrencyLimitLayer;
use std::time::Duration;

pub fn create_service() -> Router {
    // ANTI-PATTERN: Fixed static concurrency limits cause head-of-line blocking,
    // queue inflation under tail latency spikes (Little's Law), and server resource exhaustion.
    Router::new()
        .route("/api/v1/resource", get(|| async { 
            tokio::time::sleep(Duration::from_millis(50)).await;
            "ok"
        }))
        // Statically allowing 1000 requests. When downstream dependencies crawl,
        // all 1000 slots hold open sockets and saturate OS file descriptors.
        .layer(ConcurrencyLimitLayer::new(1000))
}
```

#### Production Standard (Correct)
> **Standard**: Implements adaptive concurrency limiting evaluating individual request latency against observed baseline round-trip telemetry (Vegas estimation). Saturated or high-latency states trigger non-allocating, instantaneous HTTP 503 responses, preventing internal queue build-up and preserving system throughput.
```rust
use axum::{
    body::Body,
    http::{Request, Response, StatusCode},
    middleware::{self, Next},
    response::IntoResponse,
    routing::get,
    Router,
};
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Instant;

#[derive(Clone)]
pub struct AdaptiveLimiter {
    in_flight: Arc<AtomicUsize>,
    limit: Arc<AtomicUsize>,
    min_rtt_nanos: Arc<AtomicU64>,
}

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_nanos: Arc::new(AtomicU64::new(u64::MAX)),
        }
    }
}

pub async fn shed_middleware(
    limiter: axum::extract::State<AdaptiveLimiter>,
    req: Request<Body>,
    next: Next,
) -> Response<Body> {
    let current_in_flight = limiter.in_flight.fetch_add(1, Ordering::SeqCst);
    let current_limit = limiter.limit.load(Ordering::Relaxed);

    if current_in_flight >= current_limit {
        limiter.in_flight.fetch_sub(1, Ordering::SeqCst);
        return Response::builder()
            .status(StatusCode::SERVICE_UNAVAILABLE)
            .header("Retry-After", "0")
            .body(Body::from("Load shed: Concurrency threshold exceeded"))
            .unwrap();
    }

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

    limiter.in_flight.fetch_sub(1, Ordering::SeqCst);
    let min_rtt = limiter.min_rtt_nanos.fetch_min(rtt, Ordering::Relaxed).min(rtt);

    // Vegas-style dynamic modulation: Adjust concurrency boundary based on queue inflation
    if rtt > (min_rtt * 3 / 2) && current_limit > 5 {
        limiter.limit.fetch_sub(1, Ordering::Relaxed);
    } else if current_in_flight + 1 >= current_limit {
        limiter.limit.fetch_add(1, Ordering::Relaxed);
    }

    response
}

pub fn create_service() -> Router {
    let limiter = AdaptiveLimiter::new(128);
    Router::new()
        .route("/api/v1/resource", get(|| async { "ok" }))
        .route_layer(middleware::from_fn_with_state(limiter.clone(), shed_middleware))
        .with_state(limiter)
}
```

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 Concurrency Shedding via Dynamic Latency Telemetry

Axum 0.8 Adaptive Concurrency Shedding via Dynamic Latency Telemetry defines an industrial-grade behavioral standard for backend engineering. Enforces dynamic, TCP/RTT-telemetry-driven concurrency limits within Axum 0.8 microservices to replace rigid static concurrency caps. Prevents catastrophic cascading failures and thread starvation caused by distributed tail-latency spikes and queuing delays governed by Little's Law. Ensures backpressure manifests as instantaneous HTTP 503 load-shed responses with zero-allocation drops rather than unbounded heap growth or socket pileups. 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 Tower Buffering Inducing Latency Cascades 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 Tower Buffering Inducing Latency CascadesCritical
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 shedding driven by observed latency (RTT) versus baseline latency (RTT_min) rather than static numerical ceilings (e.g., fixed `tower

Engineers MUST ALWAYS enforce adaptive concurrency shedding driven by observed latency (RTT) versus baseline latency (RTT_min) rather than static numerical ceilings (e.g., fixed `tower::limit::ConcurrencyLimitLayer`).

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 utilize unbounded in-memory queues (`tower

Engineers MUST NOT utilize unbounded in-memory queues (`tower::buffer::BufferLayer` with large or unbounded capacity) ahead of downstream I/O bounds in Axum 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 MUST return an immediate `503 Service Unavailable` with `Retry-After

Code MUST return an immediate `503 Service Unavailable` with `Retry-After: 0` headers upon adaptive limit saturation to allow upstream meshes/load balancers to reroute.

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 continuously track sample minimum round-trip time (`min_rtt`) over rolling time windows to distinguish between true system saturation and natural workload variance.

Services MUST continuously track sample minimum round-trip time (`min_rtt`) over rolling time windows to distinguish between true system saturation and natural workload variance.

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 hold open stateful streaming connections or parse large request bodies prior to passing through the load-shedding middleware boundary.

Components NEVER hold open stateful streaming connections or parse large request bodies prior to passing through the load-shedding middleware boundary.

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 Concurrency Shedding via Dynamic Latency Telemetry critical for AI coding assistants?

Enforces dynamic, TCP/RTT-telemetry-driven concurrency limits within Axum 0.8 microservices to replace rigid static concurrency caps. Prevents catastrophic cascading failures and thread starvation caused by distributed tail-latency spikes and queuing delays governed by Little's Law. Ensures backpressure manifests as instantaneous HTTP 503 load-shed responses with zero-allocation drops rather than unbounded heap growth or socket pileups. 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 Tower Buffering Inducing Latency Cascades) 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