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

Axum 0.8 Tower Buffer-Bounded Concurrency and Fail-Fast Load Shedding Protocol

Enforces deterministic backpressure and fail-fast load shedding in Axum 0.8 microservices by pairing Tower BufferLayer with LoadShedLayer and HandleErrorLayer. Guarantees saturating traffic spikes reject excess requests with HTTP 503 before streaming or buffering memory-intensive payloads.

Prohibited Anti-Pattern: Unbounded task queuing and memory pre-allocation leading to OOM cascades under heavy load.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 Tower Buffer-Bounded Concurrency and Fail-Fast Load Shedding Protocol - Enforces deterministic backpressure and fail-fast load shedding in Axum 0.8 microservices by pairing Tower BufferLayer with LoadShedLayer and HandleErrorLayer. Guarantees saturating traffic spikes reject excess requests with HTTP 503 before streaming or buffering memory-intensive payloads.
globs: src/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-tower-buffer-bounded-concurrency-load-shedding"
---

# Axum 0.8 Tower Buffer-Bounded Concurrency and Fail-Fast Load Shedding Protocol

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded task queuing and memory pre-allocation leading to OOM cascades under heavy load.

## System Constraints
You are a Principal Rust Systems Architect enforcing strict Tower backpressure and concurrency boundaries in Axum 0.8 services. Mandate bounded queue channels, fail-fast load shedding, and early HTTP 503 error translation before request body memory is allocated.

## Strict Guidelines
- Engineers MUST wrap downstream services with tower::load_shed::LoadShedLayer immediately outside tower::buffer::BufferLayer to fail fast when channel capacity is exhausted.
- Engineers MUST specify an explicit, bounded queue capacity in tower::buffer::BufferLayer calculated from max heap limits.
- Services MUST map tower::load_shed::error::Overloaded to StatusCode::SERVICE_UNAVAILABLE using axum::error_handling::HandleErrorLayer at the Tower layer boundary.
- Engineers MUST NOT place payload extractors, body-buffering middleware, or decompression layers above the LoadShedLayer in the Tower execution order.
- Components NEVER use unbounded channels or reliance on default Tokio worker queue expansion to buffer incoming HTTP requests under backpressure.

## Bad vs Good Implementations
### Tower Middleware Ordering for Bounded Buffering and Fail-Fast Load Shedding

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: ConcurrencyLimitLayer without a bounded BufferLayer and LoadShedLayer allows pending requests to queue indefinitely in the Tokio runtime, fully allocating request bodies in memory before shedding, precipitating out-of-memory crashes.
```rust
use axum::{routing::post, Router};
use tower::limit::ConcurrencyLimitLayer;

async fn ingest_payload(body: String) -> &'static str {
    // Anti-pattern: Request body memory is already buffered and allocated by the
    // time concurrency is checked. Under heavy load, unbounded tasks queue in
    // memory, causing high latency, thread contention, and eventual process OOM.
    "ok"
}

pub fn create_router() -> Router {
    Router::new()
        .route("/api/v1/ingest", post(ingest_payload))
        .layer(ConcurrencyLimitLayer::new(256))
}
```

#### Production Standard (Correct)
> **Standard**: HandleErrorLayer catches Overloaded errors from LoadShedLayer when the 1024-capacity BufferLayer is saturated, instantly returning HTTP 503 before the request body extractor allocates heap memory.
```rust
use axum::{
    error_handling::HandleErrorLayer,
    http::StatusCode,
    routing::post,
    BoxError,
    Router,
};
use tower::{
    buffer::BufferLayer,
    limit::ConcurrencyLimitLayer,
    load_shed::LoadShedLayer,
    ServiceBuilder,
};
use std::time::Duration;

async fn ingest_payload(body: String) -> &'static str {
    "ok"
}

async fn handle_service_error(err: BoxError) -> (StatusCode, &'static str) {
    if err.is::<tower::load_shed::error::Overloaded>() {
        (StatusCode::SERVICE_UNAVAILABLE, "Server overloaded; queue capacity exceeded")
    } else if err.is::<tower::timeout::error::Elapsed>() {
        (StatusCode::REQUEST_TIMEOUT, "Request processing timed out")
    } else {
        (StatusCode::INTERNAL_SERVER_ERROR, "Unhandled middleware error")
    }
}

pub fn create_router() -> Router {
    // Tower layers execute top-to-bottom: 
    // Incoming -> HandleErrorLayer -> LoadShedLayer -> BufferLayer -> ConcurrencyLimitLayer -> Handler
    let middleware_stack = ServiceBuilder::new()
        .layer(HandleErrorLayer::new(handle_service_error))
        .layer(LoadShedLayer::new())
        .layer(BufferLayer::new(1024)) // Strictly bound pending requests
        .layer(ConcurrencyLimitLayer::new(128)); // Bound concurrent in-flight executions

    Router::new()
        .route("/api/v1/ingest", post(ingest_payload))
        .layer(middleware_stack)
}
```

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 Buffer-Bounded Concurrency and Fail-Fast Load Shedding Protocol

Axum 0.8 Tower Buffer-Bounded Concurrency and Fail-Fast Load Shedding Protocol defines an industrial-grade behavioral standard for backend engineering. Enforces deterministic backpressure and fail-fast load shedding in Axum 0.8 microservices by pairing Tower BufferLayer with LoadShedLayer and HandleErrorLayer. Guarantees saturating traffic spikes reject excess requests with HTTP 503 before streaming or buffering memory-intensive payloads. 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 memory pre-allocation leading to OOM cascades under heavy load. 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 memory pre-allocation leading to OOM cascades under heavy load.Critical
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 wrap downstream services with tower

Engineers MUST wrap downstream services with tower::load_shed::LoadShedLayer immediately outside tower::buffer::BufferLayer to fail fast when channel capacity is exhausted.

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 specify an explicit, bounded queue capacity in tower

Engineers MUST specify an explicit, bounded queue capacity in tower::buffer::BufferLayer calculated from max heap limits.

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

Services MUST map tower

Services MUST map tower::load_shed::error::Overloaded to StatusCode::SERVICE_UNAVAILABLE using axum::error_handling::HandleErrorLayer at the Tower layer 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.
4

Engineers MUST NOT place payload extractors, body-buffering middleware, or decompression layers above the LoadShedLayer in the Tower execution order.

Engineers MUST NOT place payload extractors, body-buffering middleware, or decompression layers above the LoadShedLayer in the Tower execution order.

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 use unbounded channels or reliance on default Tokio worker queue expansion to buffer incoming HTTP requests under backpressure.

Components NEVER use unbounded channels or reliance on default Tokio worker queue expansion to buffer incoming HTTP requests under backpressure.

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 Buffer-Bounded Concurrency and Fail-Fast Load Shedding Protocol critical for AI coding assistants?

Enforces deterministic backpressure and fail-fast load shedding in Axum 0.8 microservices by pairing Tower BufferLayer with LoadShedLayer and HandleErrorLayer. Guarantees saturating traffic spikes reject excess requests with HTTP 503 before streaming or buffering memory-intensive payloads. 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 memory pre-allocation leading to OOM cascades under heavy load.) 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