This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: axum-0-8-zero-copy-streaming-tower-pipeline

Axum 0.8 Zero-Copy Streaming and Backpressured Tower Service Invariants

Enforces zero-allocation and bounded-memory streaming pipelines in Axum 0.8 microservices handling multi-gigabit workloads. Mandates explicit frame-based backpressure across Tower layers and strictly forbids buffering unbounded bodies into contiguous memory allocations. Guarantees constant memory consumption regardless of payload size under heavy concurrent ingress.

Prohibited Anti-Pattern: Buffering Unbounded Request Payloads into Contiguous Buffers Without Flow Control

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 Zero-Copy Streaming and Backpressured Tower Service Invariants - Enforces zero-allocation and bounded-memory streaming pipelines in Axum 0.8 microservices handling multi-gigabit workloads. Mandates explicit frame-based backpressure across Tower layers and strictly forbids buffering unbounded bodies into contiguous memory allocations. Guarantees constant memory consumption regardless of payload size under heavy concurrent ingress.
globs: src/**/*.rs, crates/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-0-8-zero-copy-streaming-tower-pipeline"
---

# Axum 0.8 Zero-Copy Streaming and Backpressured Tower Service Invariants

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Buffering Unbounded Request Payloads into Contiguous Buffers Without Flow Control

## System Constraints
You are a Principal Rust Systems Architect specializing in high-throughput network engines using Axum 0.8 and Tower. Enforce non-blocking, zero-copy byte streaming with deterministic memory limits and strict backpressure propagation. Reject any code that accumulates arbitrary request bodies in memory or circumvents Tower's poll_ready backpressure protocol.

## Strict Guidelines
- Engineers MUST ALWAYS process high-throughput inbound bodies using `axum::body::Body` chunk streams via `http_body_util::BodyStream` or raw frame iteration without intermediate `Vec<u8>` allocation.
- Engineers MUST NOT invoke `axum::body::to_bytes` or read full payloads into memory buffers without explicit, hard-coded byte limits wrapped in `tower_http::limit::RequestBodyLimitLayer` or `http_body_util::Limited`.
- Services MUST preserve backpressure by awaiting downstream consumer readiness before polling subsequent `Frame<Bytes>` chunks from upstream sources.
- Tower middleware layers MUST propagate `poll_ready` signals directly from inner services and NEVER queue unbound items or drop unpolled poll futures across task boundaries.
- Components NEVER clone payload byte slices into owned buffers; engineers MUST leverage `bytes::Bytes` shallow slicing to achieve true zero-copy processing through the service graph.

## Bad vs Good Implementations
### Unbounded Body Buffering vs. Zero-Copy Backpressured Stream Ingestion

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Reading an entire unconstrained request payload into memory with `to_bytes` and cloning it into a `Vec<u8>` defeats Axum 0.8's streaming engine, breaks backpressure signals to the client, and causes Out-Of-Memory (OOM) panic during multi-gigabit bursts.
```rust
use axum::{body::to_bytes, extract::Request, http::StatusCode, response::IntoResponse};

pub async fn ingest_telemetry_bad(request: Request) -> Result<impl IntoResponse, StatusCode> {
    // ANTI-PATTERN: Ingests the entire multi-gigabit payload directly into contiguous memory.
    // Bypasses backpressure, induces high allocator churn, and creates immediate OOM risk.
    let body = request.into_body();
    let full_bytes = to_bytes(body, usize::MAX)
        .await
        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;

    // Inefficient owned copy instead of zero-copy chunk parsing
    let data_vec: Vec<u8> = full_bytes.to_vec();
    process_payload_sync(&data_vec);

    Ok(StatusCode::ACCEPTED)
}

fn process_payload_sync(_data: &[u8]) {}
```

#### Production Standard (Correct)
> **Standard**: Wraps the streaming body in `http_body_util::Limited` to guarantee a hard ceiling, processes frames incrementally with `BodyStream`, and feeds byte slices directly into an async sink via backpressure. Memory footprint is bounded to the size of the active chunk.
```rust
use axum::{body::Body, extract::Request, http::StatusCode, response::IntoResponse};
use bytes::Bytes;
use futures_util::StreamExt;
use http_body_util::{BodyStream, Limited};
use tokio::io::AsyncWriteExt;

const MAX_CHUNK_CEILING: usize = 64 * 1024; // 64 KiB chunk threshold
const MAX_TOTAL_BYTES: usize = 10 * 1024 * 1024 * 1024; // 10 GiB ceiling

pub async fn ingest_telemetry_good(request: Request<Body>) -> Result<impl IntoResponse, StatusCode> {
    // Enforce strict upper bound on the incoming stream
    let limited_body = Limited::new(request.into_body(), MAX_TOTAL_BYTES);
    let mut stream = BodyStream::new(limited_body);

    // Simulated backpressured asynchronous sink
    let mut sink = tokio::io::sink();

    while let Some(frame_result) = stream.next().await {
        let frame = frame_result.map_err(|_| StatusCode::PAYLOAD_TOO_LARGE)?;
        
        // Zero-copy extraction of the frame's data buffer
        if let Some(chunk) = frame.data_ref() {
            // Forward chunk directly with backpressure; memory footprint never exceeds frame size
            sink.write_all(chunk)
                .await
                .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
        }
    }

    sink.flush().await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
    Ok(StatusCode::ACCEPTED)
}
```

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 Backpressured Tower Service Invariants

Axum 0.8 Zero-Copy Streaming and Backpressured Tower Service Invariants defines an industrial-grade behavioral standard for backend engineering. Enforces zero-allocation and bounded-memory streaming pipelines in Axum 0.8 microservices handling multi-gigabit workloads. Mandates explicit frame-based backpressure across Tower layers and strictly forbids buffering unbounded bodies into contiguous memory allocations. Guarantees constant memory consumption regardless of payload size under heavy concurrent ingress. 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: Buffering Unbounded Request Payloads into Contiguous Buffers Without Flow Control 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 DriftBuffering Unbounded Request Payloads into Contiguous Buffers Without Flow ControlCritical
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 process high-throughput inbound bodies using `axum

Engineers MUST ALWAYS process high-throughput inbound bodies using `axum::body::Body` chunk streams via `http_body_util::BodyStream` or raw frame iteration without intermediate `Vec<u8>` allocation.

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

Engineers MUST NOT invoke `axum::body::to_bytes` or read full payloads into memory buffers without explicit, hard-coded byte limits wrapped in `tower_http::limit::RequestBodyLimitLayer` or `http_body_util::Limited`.

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 preserve backpressure by awaiting downstream consumer readiness before polling subsequent `Frame<Bytes>` chunks from upstream sources.

Services MUST preserve backpressure by awaiting downstream consumer readiness before polling subsequent `Frame<Bytes>` chunks from upstream sources.

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

Tower middleware layers MUST propagate `poll_ready` signals directly from inner services and NEVER queue unbound items or drop unpolled poll futures across task boundaries.

Tower middleware layers MUST propagate `poll_ready` signals directly from inner services and NEVER queue unbound items or drop unpolled poll futures across task 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

Components NEVER clone payload byte slices into owned buffers

Components NEVER clone payload byte slices into owned buffers; engineers MUST leverage `bytes::Bytes` shallow slicing to achieve true zero-copy processing through the service graph.

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 Backpressured Tower Service Invariants critical for AI coding assistants?

Enforces zero-allocation and bounded-memory streaming pipelines in Axum 0.8 microservices handling multi-gigabit workloads. Mandates explicit frame-based backpressure across Tower layers and strictly forbids buffering unbounded bodies into contiguous memory allocations. Guarantees constant memory consumption regardless of payload size under heavy concurrent ingress. 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 (Buffering Unbounded Request Payloads into Contiguous Buffers Without Flow Control) 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