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

Axum 0.8 Deterministic Tower Backpressure and Adaptive Concurrency Limiting

Enforces deterministic tail latency and graceful connection shedding in Axum 0.8 workloads under saturated HTTP/3 and WebSocket conditions. Mandates the correct ordering of Tower middleware to isolate unbuffered TCP/QUIC streams and prevent unbounded task allocation during load spikes. Requires integration of adaptive concurrency limiting algorithms with fast-reject 503 responses before connection handshakes occur.

Prohibited Anti-Pattern: Unbounded Task Spawning and Unordered Tower Layers Inducing Memory Blowup and Latency Cascades

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 Deterministic Tower Backpressure and Adaptive Concurrency Limiting - Enforces deterministic tail latency and graceful connection shedding in Axum 0.8 workloads under saturated HTTP/3 and WebSocket conditions. Mandates the correct ordering of Tower middleware to isolate unbuffered TCP/QUIC streams and prevent unbounded task allocation during load spikes. Requires integration of adaptive concurrency limiting algorithms with fast-reject 503 responses before connection handshakes occur.
globs: src/**/*.rs, crates/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-08-tower-backpressure-adaptive-concurrency"
---

# Axum 0.8 Deterministic Tower Backpressure and Adaptive Concurrency Limiting

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Task Spawning and Unordered Tower Layers Inducing Memory Blowup and Latency Cascades

## System Constraints
You are a Principal Rust Architect enforcing rock-solid resilience guardrails on Axum 0.8 services. Your goal is to eliminate unbounded task growth, buffer bloat, and cascading timeouts under heavy HTTP/3 (QUIC) and WebSocket pressure. Enforce correct Tower layer ordering, load shedding, and adaptive concurrency control.

## Strict Guidelines
- Engineers MUST ALWAYS place backpressure-shedding middleware (e.g., LoadShedLayer, ConcurrencyLimitLayer) at the outermost edge of the Tower stack before state extraction or body parsing.
- Engineers MUST NOT allow WebSocket upgrade endpoints to consume permit quotas indefinitely; connection upgrades MUST transfer permit responsibility to dedicated session-tracking permits rather than holding the underlying HTTP request permit.
- Engineers MUST configure Tower's `HandleErrorLayer` to map load-shed rejections directly to RFC 7807/RFC 9457 HTTP 503 (Service Unavailable) responses with a deterministic `Retry-After` header.
- Services MUST bound the incoming body size via `DefaultBodyLimit` and QUIC stream flow control window to prevent allocation attacks that bypass CPU-bound concurrency limiters.
- Components NEVER spawn unmonitored `tokio::spawn` loops inside WebSocket stream adapters without a bounded channel and explicit backpressure propagation to the peer.

## Bad vs Good Implementations
### Applying Outermost Adaptive Load Shedding and Bounded Upgrades in Axum 0.8

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Placing ConcurrencyLimitLayer without LoadShedLayer or HandleErrorLayer leads to unbounded pending futures in the Tower service buffer. Upgrading a WebSocket inside a raw concurrency limit starves standard HTTP requests because the permit is retained for the lifetime of the upgrade handler. Spawning unbounded tasks per incoming WS frame bypasses Tower backpressure entirely.
```rust
use axum::{Router, routing::get, extract::ws::WebSocketUpgrade};
use tower::limit::ConcurrencyLimitLayer;

// ANTI-PATTERN: Inner concurrency limit layer causes queue build-up,
// does not handle sheds gracefully, and holds permits across long-lived WS connections.
pub fn bad_router() -> Router {
    Router::new()
        .route("/ws", get(|ws: WebSocketUpgrade| async move {
            ws.on_upgrade(|socket| async move {
                // WebSocket lives forever holding request-level concurrency slot
                let (mut sender, mut receiver) = socket.split();
                while let Some(Ok(msg)) = receiver.next().await {
                    tokio::spawn(async move {
                        // Unbounded task spawning per frame
                    });
                }
            })
        }))
        .layer(ConcurrencyLimitLayer::new(1024))
}
```

#### Production Standard (Correct)
> **Standard**: Combines `HandleErrorLayer` and `LoadShedLayer` with explicit `GlobalConcurrencyLimitLayer` at the boundary. Rejections return HTTP 503 instantly with backoff guidance. WebSockets complete the initial HTTP handshake and release the HTTP-tier permit, delegating stateful connection streaming to an isolated bounded channel mechanism.
```rust
use axum::{
    Router, routing::get, extract::ws::WebSocketUpgrade, response::{Response, IntoResponse},
    http::StatusCode, error_handling::HandleErrorLayer, BoxError,
};
use tower::{ServiceBuilder, load_shed::LoadShedLayer, limit::GlobalConcurrencyLimitLayer};
use std::time::Duration;

async fn handle_shed_error(err: BoxError) -> Response {
    if err.is::<tower::load_shed::error::Overloaded>() {
        (StatusCode::SERVICE_UNAVAILABLE, [("retry-after", "2")], "Server Overloaded").into_response()
    } else {
        (StatusCode::INTERNAL_SERVER_ERROR, "Internal Server Error").into_response()
    }
}

pub fn production_router() -> Router {
    let middleware = ServiceBuilder::new()
        .layer(HandleErrorLayer::new(handle_shed_error))
        .layer(LoadShedLayer::new())
        .layer(GlobalConcurrencyLimitLayer::new(1024))
        .timeout(Duration::from_secs(5));

    Router::new()
        .route("/ws", get(|ws: WebSocketUpgrade| async move {
            // Handshake completes and releases the load-shedded HTTP permit
            ws.on_upgrade(|socket| async move {
                // Bounded channel prevents unbounded memory growth
                let (mut tx, mut rx) = tokio::sync::mpsc::channel(32);
                // Frame handling uses explicit permit acquisition and bounded buffers
            })
        }))
        .layer(middleware)
}
```

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 Deterministic Tower Backpressure and Adaptive Concurrency Limiting

Axum 0.8 Deterministic Tower Backpressure and Adaptive Concurrency Limiting defines an industrial-grade behavioral standard for backend engineering. Enforces deterministic tail latency and graceful connection shedding in Axum 0.8 workloads under saturated HTTP/3 and WebSocket conditions. Mandates the correct ordering of Tower middleware to isolate unbuffered TCP/QUIC streams and prevent unbounded task allocation during load spikes. Requires integration of adaptive concurrency limiting algorithms with fast-reject 503 responses before connection handshakes occur. 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 Spawning and Unordered Tower Layers Inducing Memory Blowup and 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 DriftUnbounded Task Spawning and Unordered Tower Layers Inducing Memory Blowup and 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 place backpressure-shedding middleware (e.g., LoadShedLayer, ConcurrencyLimitLayer) at the outermost edge of the Tower stack before state extraction or body parsing.

Engineers MUST ALWAYS place backpressure-shedding middleware (e.g., LoadShedLayer, ConcurrencyLimitLayer) at the outermost edge of the Tower stack before state extraction or body parsing.

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 allow WebSocket upgrade endpoints to consume permit quotas indefinitely

Engineers MUST NOT allow WebSocket upgrade endpoints to consume permit quotas indefinitely; connection upgrades MUST transfer permit responsibility to dedicated session-tracking permits rather than holding the underlying HTTP request permit.

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 configure Tower's `HandleErrorLayer` to map load-shed rejections directly to RFC 7807/RFC 9457 HTTP 503 (Service Unavailable) responses with a deterministic `Retry-After` header.

Engineers MUST configure Tower's `HandleErrorLayer` to map load-shed rejections directly to RFC 7807/RFC 9457 HTTP 503 (Service Unavailable) responses 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

Services MUST bound the incoming body size via `DefaultBodyLimit` and QUIC stream flow control window to prevent allocation attacks that bypass CPU-bound concurrency limiters.

Services MUST bound the incoming body size via `DefaultBodyLimit` and QUIC stream flow control window to prevent allocation attacks that bypass CPU-bound concurrency limiters.

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 spawn unmonitored `tokio

Components NEVER spawn unmonitored `tokio::spawn` loops inside WebSocket stream adapters without a bounded channel and explicit backpressure propagation to the peer.

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 Deterministic Tower Backpressure and Adaptive Concurrency Limiting critical for AI coding assistants?

Enforces deterministic tail latency and graceful connection shedding in Axum 0.8 workloads under saturated HTTP/3 and WebSocket conditions. Mandates the correct ordering of Tower middleware to isolate unbuffered TCP/QUIC streams and prevent unbounded task allocation during load spikes. Requires integration of adaptive concurrency limiting algorithms with fast-reject 503 responses before connection handshakes occur. 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 Spawning and Unordered Tower Layers Inducing Memory Blowup and 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