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

Axum 0.8 Zero-Copy Streaming and Tower Layer Backpressure Invariants

Enforces strict backpressure propagation and zero-copy byte slicing across Axum 0.8 routes and Tower middleware layers. Prevents catastrophic memory amplification under high-concurrency I/O by prohibiting unbounded body materialization into memory. Guarantees deterministic O(1) allocation overhead during asynchronous pipeline forwarding.

Prohibited Anti-Pattern: Unbounded Eager Body Materialization and Backpressure Dropping in Tower Middleware

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 Zero-Copy Streaming and Tower Layer Backpressure Invariants - Enforces strict backpressure propagation and zero-copy byte slicing across Axum 0.8 routes and Tower middleware layers. Prevents catastrophic memory amplification under high-concurrency I/O by prohibiting unbounded body materialization into memory. Guarantees deterministic O(1) allocation overhead during asynchronous pipeline forwarding.
globs: crates/**/src/**/*.rs, src/**/*.rs, **/middleware/**/*.rs, **/handlers/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-08-streaming-tower-backpressure-invariants"
---

# Axum 0.8 Zero-Copy Streaming and Tower Layer Backpressure Invariants

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Eager Body Materialization and Backpressure Dropping in Tower Middleware

## System Constraints
You are a Principal Systems Architect enforcing Axum 0.8 Zero-Copy Streaming and Tower Layer Invariants. Mandate strict backpressure propagation, explicit byte frame budgets, and stream composition without eager heap accumulation. Reject any code that loads arbitrary payloads into memory using unbounded buffers or circumvents the `http_body::Body` frame polling contract.

## Strict Guidelines
- Engineers MUST ALWAYS enforce explicit byte limits via `http_body_util::Limited` or `axum::extract::DefaultBodyLimit` before any stream aggregation occurs.
- Engineers MUST NOT call `axum::body::to_bytes` with unbounded limits (`usize::MAX`) or collect entire bodies into `Vec<u8>` inside Tower middleware.
- Middleware transformations MUST preserve backpressure by wrapping `http_body::Body` implementations directly rather than buffering data through unbounded `mpsc::unbounded_channel` buffers.
- Stream payload transformations MUST leverage reference-counted zero-copy slicing (`bytes::Bytes::slice`) instead of deep-cloning byte payloads across layer boundaries.
- Handlers handling continuous I/O MUST consume request bodies via `Body::into_data_stream` or `http_body_util::BodyStream` and emit responses using `axum::body::Body::from_stream`.
- Custom Tower `Layer` and `Service` implementations MUST propagate `poll_ready` state faithfully to the inner service and not poll downstreams until inner readiness is confirmed.

## Bad vs Good Implementations
### Tower Middleware Body Transformation: Eager In-Memory Buffer vs. Zero-Copy Streaming Wrapper

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using `axum::body::to_bytes(body, usize::MAX)` loads the entire request body into heap memory before forwarding it to the inner service. This drops HTTP/2 stream-level backpressure and exposes the server to unbounded memory amplification when concurrent clients upload large payloads.
```rust
use axum::{body::Body, extract::Request, response::Response, middleware::Next};
use http_body_util::BodyExt;

// ANTI-PATTERN: Eagerly collects entire request body into memory,
// breaking backpressure and allowing O(N) memory amplification DOS.
pub async fn bad_audit_middleware(request: Request, next: Next) -> Response {
    let (parts, body) = request.into_parts();
    
    // Materializes unbounded stream into contiguous RAM
    let bytes = match axum::body::to_bytes(body, usize::MAX).await {
        Ok(b) => b,
        Err(_) => return Response::builder().status(400).body(Body::empty()).unwrap(),
    };
    
    println!("Payload size: {}", bytes.len());
    
    // Reconstructs body from collected bytes, allocating new buffer
    let new_request = Request::from_parts(parts, Body::from(bytes));
    next.run(new_request).await
}
```

#### Production Standard (Correct)
> **Standard**: Wraps the request body in a zero-allocation `HttpBody` adapter that observes chunk metrics dynamically during stream execution. Downstream backpressure is strictly maintained via `poll_frame` passthrough, chunks are observed without cloning using `frame.data_ref()`, and excessive memory allocation is structurally impossible.
```rust
use std::task::{Context, Poll};
use std::pin::Pin;
use axum::{body::Body, extract::Request, response::Response, middleware::Next};
use http_body::{Body as HttpBody, Frame};
use bytes::Bytes;
use pin_project_lite::pin_project;

pin_project! {
    pub struct InspectStreamBody<B> {
        #[pin]
        inner: B,
        bytes_observed: usize,
        max_limit: usize,
    }
}

impl<B: HttpBody<Data = Bytes>> HttpBody for InspectStreamBody<B> {
    type Data = Bytes;
    type Error = B::Error;

    fn poll_frame(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
        let this = self.project();
        match this.inner.poll_frame(cx) {
            Poll::Ready(Some(Ok(frame))) => {
                if let Some(data) = frame.data_ref() {
                    *this.bytes_observed += data.len();
                    // Backpressure invariant: Enforce hard limit during stream traversal
                    if *this.bytes_observed > *this.max_limit {
                        return Poll::Ready(None);
                    }
                }
                Poll::Ready(Some(Ok(frame)))
            }
            other => other,
        }
    }
}

// PRODUCTION INVARIANT: Zero-copy wrapping preserves downstream backpressure and limits heap usage
pub async fn good_audit_middleware(request: Request, next: Next) -> Response {
    let (parts, body) = request.into_parts();
    let wrapped_body = InspectStreamBody {
        inner: body,
        bytes_observed: 0,
        max_limit: 10 * 1024 * 1024, // Strict 10MB budget
    };
    let new_request = Request::from_parts(parts, Body::new(wrapped_body));
    next.run(new_request).await
}
```

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 Zero-Copy Streaming and Tower Layer Backpressure Invariants

Axum 0.8 Zero-Copy Streaming and Tower Layer Backpressure Invariants defines an industrial-grade behavioral standard for backend engineering. Enforces strict backpressure propagation and zero-copy byte slicing across Axum 0.8 routes and Tower middleware layers. Prevents catastrophic memory amplification under high-concurrency I/O by prohibiting unbounded body materialization into memory. Guarantees deterministic O(1) allocation overhead during asynchronous pipeline forwarding. 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 Eager Body Materialization and Backpressure Dropping in Tower Middleware 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 Eager Body Materialization and Backpressure Dropping in Tower MiddlewareCritical
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 explicit byte limits via `http_body_util

Engineers MUST ALWAYS enforce explicit byte limits via `http_body_util::Limited` or `axum::extract::DefaultBodyLimit` before any stream aggregation occurs.

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 call `axum

Engineers MUST NOT call `axum::body::to_bytes` with unbounded limits (`usize::MAX`) or collect entire bodies into `Vec<u8>` inside Tower middleware.

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

Middleware transformations MUST preserve backpressure by wrapping `http_body

Middleware transformations MUST preserve backpressure by wrapping `http_body::Body` implementations directly rather than buffering data through unbounded `mpsc::unbounded_channel` buffers.

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

Stream payload transformations MUST leverage reference-counted zero-copy slicing (`bytes

Stream payload transformations MUST leverage reference-counted zero-copy slicing (`bytes::Bytes::slice`) instead of deep-cloning byte payloads across layer boundaries.

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

Handlers handling continuous I/O MUST consume request bodies via `Body

Handlers handling continuous I/O MUST consume request bodies via `Body::into_data_stream` or `http_body_util::BodyStream` and emit responses using `axum::body::Body::from_stream`.

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

Custom Tower `Layer` and `Service` implementations MUST propagate `poll_ready` state faithfully to the inner service and not poll downstreams until inner readiness is confirmed.

Custom Tower `Layer` and `Service` implementations MUST propagate `poll_ready` state faithfully to the inner service and not poll downstreams until inner readiness is confirmed.

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 Zero-Copy Streaming and Tower Layer Backpressure Invariants critical for AI coding assistants?

Enforces strict backpressure propagation and zero-copy byte slicing across Axum 0.8 routes and Tower middleware layers. Prevents catastrophic memory amplification under high-concurrency I/O by prohibiting unbounded body materialization into memory. Guarantees deterministic O(1) allocation overhead during asynchronous pipeline forwarding. 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 Eager Body Materialization and Backpressure Dropping in Tower Middleware) 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