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

Axum 0.8 Tower Service Load-Shedding Backpressure Invariant

Protects Tokio work-stealing thread pools from executor saturation by enforcing bounded buffer drop policies before request deserialization. Mandates placement of Tower load shedding and concurrency limit layers at the outermost edge of Axum 0.8 routers with explicit 503 Service Unavailable mapping.

Prohibited Anti-Pattern: Deserializing Payloads or Buffering In-Flight Requests Without Upstream Edge Load-Shedding

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 Tower Service Load-Shedding Backpressure Invariant - Protects Tokio work-stealing thread pools from executor saturation by enforcing bounded buffer drop policies before request deserialization. Mandates placement of Tower load shedding and concurrency limit layers at the outermost edge of Axum 0.8 routers with explicit 503 Service Unavailable mapping.
globs: src/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-0-8-tower-load-shedding-backpressure-invariant"
---

# Axum 0.8 Tower Service Load-Shedding Backpressure Invariant

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Deserializing Payloads or Buffering In-Flight Requests Without Upstream Edge Load-Shedding

## System Constraints
You are a Principal Rust Architect enforcing Axum 0.8 and Tower load-shedding backpressure invariants. Mandate bounded execution and edge rejection before request body deserialization to protect Tokio worker threads.

## Strict Guidelines
- Engineers MUST ALWAYS place Tower `LoadShedLayer` and `ConcurrencyLimitLayer` upstream of request body extractors to drop excessive traffic prior to allocating memory or executing deserialization logic.
- Engineers MUST wrap `LoadShedLayer` with Axum's `HandleErrorLayer` to translate `tower::load_shed::error::Overloaded` into an explicit `503 Service Unavailable` HTTP response.
- Code MUST NEVER use unbounded `tower::buffer::BufferLayer` or unbound Tokio `mpsc` channels within the HTTP request pipeline, as they lead to silent memory exhaustion under sustained ingress spikes.
- Services MUST enforce strict body size limits (`axum::extract::DefaultBodyLimit`) in conjunction with load shedding to prevent memory starvation from slow-drain attacks.
- Components NEVER execute blocking operations (e.g., synchronous file I/O, heavy crypto, or synchronous database drivers) within Axum handlers without delegating via `tokio::task::spawn_blocking`.

## Bad vs Good Implementations
### Edge Load Shedding vs Unbounded Deserialization

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The router has no concurrency barrier or load-shedding layer. When high traffic arrives, Tokio workers concurrently read and deserialize incoming JSON bodies into heap memory, exhausting allocator resources and stalling cooperative scheduling before any request can be shed.
```rust
use axum::{routing::post, Json, Router};
use serde::Deserialize;

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

// ANTI-PATTERN: Heavy body parsing occurs before concurrency/load-shed evaluation.
// An ingress spike will exhaust Tokio worker memory and starve the executor pool.
async fn compute(Json(payload): Json<HeavyPayload>) -> &'static str {
    // Simulated CPU-intensive or state-bound operation
    let _ = payload.data.len();
    "ok"
}

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

#### Production Standard (Correct)
> **Standard**: The middleware stack enforces bounded concurrency and fast drops at the outermost edge using Tower's `LoadShedLayer` and `ConcurrencyLimitLayer`. Requests exceeding the threshold are immediately failed with HTTP 503 before stream extraction or JSON deserialization can saturate the Tokio executor.
```rust
use axum::{
    error_handling::HandleErrorLayer,
    extract::DefaultBodyLimit,
    http::StatusCode,
    routing::post,
    BoxError, Json, Router,
};
use serde::Deserialize;
use tower::{limit::ConcurrencyLimitLayer, load_shed::LoadShedLayer, ServiceBuilder};

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

async fn compute(Json(payload): Json<HeavyPayload>) -> &'static str {
    let _ = payload.data.len();
    "ok"
}

async fn handle_shed_error(err: BoxError) -> (StatusCode, &'static str) {
    if err.is::<tower::load_shed::error::Overloaded>() {
        (StatusCode::SERVICE_UNAVAILABLE, "Server overloaded: request shed")
    } else {
        (StatusCode::INTERNAL_SERVER_ERROR, "Internal service error")
    }
}

pub fn app() -> Router {
    let middleware = ServiceBuilder::new()
        .layer(HandleErrorLayer::new(handle_shed_error))
        .layer(LoadShedLayer::new())
        .layer(ConcurrencyLimitLayer::new(1024))
        .layer(DefaultBodyLimit::max(2 * 1024 * 1024));

    Router::new()
        .route("/compute", post(compute))
        .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 Tower Service Load-Shedding Backpressure Invariant

Axum 0.8 Tower Service Load-Shedding Backpressure Invariant defines an industrial-grade behavioral standard for backend engineering. Protects Tokio work-stealing thread pools from executor saturation by enforcing bounded buffer drop policies before request deserialization. Mandates placement of Tower load shedding and concurrency limit layers at the outermost edge of Axum 0.8 routers with explicit 503 Service Unavailable mapping. 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: Deserializing Payloads or Buffering In-Flight Requests Without Upstream Edge Load-Shedding 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 DriftDeserializing Payloads or Buffering In-Flight Requests Without Upstream Edge Load-SheddingCritical
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 Tower `LoadShedLayer` and `ConcurrencyLimitLayer` upstream of request body extractors to drop excessive traffic prior to allocating memory or executing deserialization logic.

Engineers MUST ALWAYS place Tower `LoadShedLayer` and `ConcurrencyLimitLayer` upstream of request body extractors to drop excessive traffic prior to allocating memory or executing deserialization logic.

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 wrap `LoadShedLayer` with Axum's `HandleErrorLayer` to translate `tower

Engineers MUST wrap `LoadShedLayer` with Axum's `HandleErrorLayer` to translate `tower::load_shed::error::Overloaded` into an explicit `503 Service Unavailable` HTTP response.

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 NEVER use unbounded `tower

Code MUST NEVER use unbounded `tower::buffer::BufferLayer` or unbound Tokio `mpsc` channels within the HTTP request pipeline, as they lead to silent memory exhaustion under sustained ingress spikes.

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 enforce strict body size limits (`axum

Services MUST enforce strict body size limits (`axum::extract::DefaultBodyLimit`) in conjunction with load shedding to prevent memory starvation from slow-drain attacks.

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 execute blocking operations (e.g., synchronous file I/O, heavy crypto, or synchronous database drivers) within Axum handlers without delegating via `tokio

Components NEVER execute blocking operations (e.g., synchronous file I/O, heavy crypto, or synchronous database drivers) within Axum handlers without delegating via `tokio::task::spawn_blocking`.

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 Tower Service Load-Shedding Backpressure Invariant critical for AI coding assistants?

Protects Tokio work-stealing thread pools from executor saturation by enforcing bounded buffer drop policies before request deserialization. Mandates placement of Tower load shedding and concurrency limit layers at the outermost edge of Axum 0.8 routers with explicit 503 Service Unavailable mapping. 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 (Deserializing Payloads or Buffering In-Flight Requests Without Upstream Edge Load-Shedding) 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