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

Axum 0.8 Tower Service Buffering and Adaptive Backpressure Standard

Enforces strict bounds on Tower Service Buffers and mandates proactive load shedding and concurrency limits in Axum 0.8 middleware stacks. Prevents unbounded request queuing, memory exhaustion, and async runtime thread starvation by ensuring downstream saturation sheds load via cooperative backpressure and explicit HTTP 503/429 status codes.

Prohibited Anti-Pattern: Unbounded Tower buffer layers combined with unconstrained task spawning that exhausts Tokio runtime worker threads during downstream saturation.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 Tower Service Buffering and Adaptive Backpressure Standard - Enforces strict bounds on Tower Service Buffers and mandates proactive load shedding and concurrency limits in Axum 0.8 middleware stacks. Prevents unbounded request queuing, memory exhaustion, and async runtime thread starvation by ensuring downstream saturation sheds load via cooperative backpressure and explicit HTTP 503/429 status codes.
globs: src/**/*.rs, crates/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-tower-buffering-backpressure-guardrail"
---

# Axum 0.8 Tower Service Buffering and Adaptive Backpressure Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Tower buffer layers combined with unconstrained task spawning that exhausts Tokio runtime worker threads during downstream saturation.

## System Constraints
You are a Principal Rust Systems Architect enforcing strict backpressure and queue bounding standards across Axum 0.8 and Tower service middleware. Never permit unbounded service buffering (`BufferLayer` without limits or excessively deep queues) or fire-and-forget `tokio::spawn` calls without semaphore permits or rate limits. Mandate that all Tower service layers implement `tower::load_shed::LoadShedLayer`, `tower::limit::ConcurrencyLimitLayer`, or bounded `BufferLayer` with explicit error handling mapped to HTTP 429 or 503 responses, preventing runtime starvation under tail-latency spikes.

## Strict Guidelines
- Engineers MUST ALWAYS bound any `tower::buffer::BufferLayer` with an explicit, statically validated queue capacity derived from p99 latency SLOs and available RAM.
- Engineers MUST wrap bounded `BufferLayer` instances or downstream services with `tower::load_shed::LoadShedLayer` to fail fast when downstream dependencies saturate instead of blocking upstream Tokio workers.
- Engineers MUST map Tower backpressure and overload errors (`tower::load_shed::error::Overloaded`, `tower::buffer::error::ServiceError`, etc.) to explicit `StatusCode::SERVICE_UNAVAILABLE` (503) or `StatusCode::TOO_MANY_REQUESTS` (429) using `HandleErrorLayer`.
- Engineers MUST NOT introduce unbounded `mpsc::unbounded_channel` or unconstrained `tokio::spawn` invocations within HTTP route handlers to offload request processing.
- Engineers MUST place `ConcurrencyLimitLayer` upstream of `BufferLayer` to ensure downstream task queues do not accumulate stale, timed-out HTTP requests.
- Code SHOULD configure `tower_http::timeout::TimeoutLayer` outside the buffer stack so timed-out client connections are evicted before consuming buffer queue slots.

## Bad vs Good Implementations
### Unbounded Buffer vs. Bounded Backpressure Stack with Load Shedding in Axum 0.8

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Configuring massive or unbounded buffer queues without load-shedding causes queue bloat (bufferbloat). Under downstream latency spikes, Tokio worker threads stall waiting for buffer slots, consuming unbounded heap memory until the OS terminates the process via OOM killer.
```rust
use axum::{routing::get, Router};
use tower::ServiceBuilder;
use tower::buffer::BufferLayer;
use std::time::Duration;

pub fn make_app() -> Router {
    // ANTI-PATTERN: Unbounded buffer size (usize::MAX or arbitrary huge queue),
    // missing ConcurrencyLimitLayer and LoadShedLayer.
    // Incoming surges will allocate infinitely, causing Tokio executor starvation and OOM.
    let middleware = ServiceBuilder::new()
        .layer(BufferLayer::new(1_000_000)); // Queueing 1M requests consumes huge memory

    Router::new()
        .route("/api/v1/work", get(|| async { "ok" }))
        .layer(middleware)
}
```

#### Production Standard (Correct)
> **Standard**: The stack enforces a strict concurrency ceiling (1024 in-flight requests), a bounded intermediate buffer (256 slots), and an immediate `LoadShedLayer`. Surges beyond capacity fail fast with HTTP 503 instead of exhausting Tokio runtime worker threads or heap memory.
```rust
use axum::{error_handling::HandleErrorLayer, http::StatusCode, routing::get, BoxError, Router};
use std::time::Duration;
use tower::{load_shed::LoadShedLayer, ServiceBuilder};
use tower_http::timeout::TimeoutLayer;

async fn handle_service_error(err: BoxError) -> (StatusCode, String) {
    if err.is::<tower::load_shed::error::Overloaded>() {
        (StatusCode::SERVICE_UNAVAILABLE, "Server overloaded; please retry with backoff".into())
    } else if err.is::<tower_http::timeout::ResponseBodyTimeout>() || err.is::<tower::timeout::error::Elapsed>() {
        (StatusCode::GATEWAY_TIMEOUT, "Request processing timed out".into())
    } else {
        (StatusCode::INTERNAL_SERVER_ERROR, format!("Unhandled internal error: {err}"))
    }
}

pub fn make_app() -> Router {
    // PRODUCTION INVARIANT: Bounded concurrency, early timeout, and fast load-shedding.
    // HandleErrorLayer catches overload errors before bubbling into Axum root handler.
    let middleware = ServiceBuilder::new()
        .layer(HandleErrorLayer::new(handle_service_error))
        .layer(TimeoutLayer::new(Duration::from_secs(5)))
        .layer(LoadShedLayer::new())
        .layer(tower::limit::ConcurrencyLimitLayer::new(1024))
        .layer(tower::buffer::BufferLayer::new(256));

    Router::new()
        .route("/api/v1/work", get(|| async { "ok" }))
        .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 Buffering and Adaptive Backpressure Standard

Axum 0.8 Tower Service Buffering and Adaptive Backpressure Standard defines an industrial-grade behavioral standard for backend engineering. Enforces strict bounds on Tower Service Buffers and mandates proactive load shedding and concurrency limits in Axum 0.8 middleware stacks. Prevents unbounded request queuing, memory exhaustion, and async runtime thread starvation by ensuring downstream saturation sheds load via cooperative backpressure and explicit HTTP 503/429 status codes. 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 Tower buffer layers combined with unconstrained task spawning that exhausts Tokio runtime worker threads during downstream saturation. 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 Tower buffer layers combined with unconstrained task spawning that exhausts Tokio runtime worker threads during downstream saturation.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 ALWAYS bound any `tower

Engineers MUST ALWAYS bound any `tower::buffer::BufferLayer` with an explicit, statically validated queue capacity derived from p99 latency SLOs and available RAM.

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 bounded `BufferLayer` instances or downstream services with `tower

Engineers MUST wrap bounded `BufferLayer` instances or downstream services with `tower::load_shed::LoadShedLayer` to fail fast when downstream dependencies saturate instead of blocking upstream Tokio workers.

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 map Tower backpressure and overload errors (`tower

Engineers MUST map Tower backpressure and overload errors (`tower::load_shed::error::Overloaded`, `tower::buffer::error::ServiceError`, etc.) to explicit `StatusCode::SERVICE_UNAVAILABLE` (503) or `StatusCode::TOO_MANY_REQUESTS` (429) using `HandleErrorLayer`.

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 introduce unbounded `mpsc

Engineers MUST NOT introduce unbounded `mpsc::unbounded_channel` or unconstrained `tokio::spawn` invocations within HTTP route handlers to offload request processing.

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

Engineers MUST place `ConcurrencyLimitLayer` upstream of `BufferLayer` to ensure downstream task queues do not accumulate stale, timed-out HTTP requests.

Engineers MUST place `ConcurrencyLimitLayer` upstream of `BufferLayer` to ensure downstream task queues do not accumulate stale, timed-out HTTP requests.

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.
6

Code SHOULD configure `tower_http

Code SHOULD configure `tower_http::timeout::TimeoutLayer` outside the buffer stack so timed-out client connections are evicted before consuming buffer queue slots.

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 Buffering and Adaptive Backpressure Standard critical for AI coding assistants?

Enforces strict bounds on Tower Service Buffers and mandates proactive load shedding and concurrency limits in Axum 0.8 middleware stacks. Prevents unbounded request queuing, memory exhaustion, and async runtime thread starvation by ensuring downstream saturation sheds load via cooperative backpressure and explicit HTTP 503/429 status codes. 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 Tower buffer layers combined with unconstrained task spawning that exhausts Tokio runtime worker threads during downstream saturation.) 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