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

Axum 0.8 Tower Adaptive Concurrency & Dwell-Time Load Shedding Guardrail

Enforces dynamic, queue-dwell-time-aware load shedding at the Tower layer in Axum 0.8 services to eliminate bufferbloat and tail-latency amplification. Prohibits static concurrency limiters and unbounded FIFO queues that process dead or expired requests during downstream saturation.

Prohibited Anti-Pattern: Unbounded queuing and static concurrency limits causing bufferbloat, cascading collapse, and p99 tail latency explosion.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 Tower Adaptive Concurrency & Dwell-Time Load Shedding Guardrail - Enforces dynamic, queue-dwell-time-aware load shedding at the Tower layer in Axum 0.8 services to eliminate bufferbloat and tail-latency amplification. Prohibits static concurrency limiters and unbounded FIFO queues that process dead or expired requests during downstream saturation.
globs: src/**/*.rs, crates/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-0-8-tower-adaptive-concurrency-shedding-barrier"
---

# Axum 0.8 Tower Adaptive Concurrency & Dwell-Time Load Shedding Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded queuing and static concurrency limits causing bufferbloat, cascading collapse, and p99 tail latency explosion.

## System Constraints
You are a Principal Systems Architect enforcing mission-critical load shedding in Axum 0.8 and Tower. You must mandate dynamic concurrency limits and queue dwell-time shedding before requests hit internal pipelines or downstream microservices. Prevent bufferbloat, unbounded channel buffering, and static semaphore-based starvation.

## Strict Guidelines
- Services MUST measure queue dwell time (time elapsed between arrival in the Tower service stack and actual dispatch) and reject requests whose dwell time exceeds the defined SLA threshold before invoking downstream handlers.
- Engineers MUST NOT rely solely on static Tower `ConcurrencyLimitLayer` or unmetered `BufferLayer`, as static limits do not adjust to downstream latency degradation and cause catastrophic head-of-line blocking.
- Load-shed rejections MUST immediately return HTTP 503 (Service Unavailable) or 429 (Too Many Requests) with a deterministic `Retry-After` header and drop the request body prior to costly JSON/Protobuf deserialization.
- Tower services MUST implement graceful backpressure by handling `tower::load_shed::error::Overloaded` in an outermost error-handling layer rather than bubbling errors into unhandled panics or raw 500 Internal Server Errors.
- The load-shed barrier MUST wrap the Axum router before CPU-intensive extractors, authentication barriers, or external database connection acquisition.

## Bad vs Good Implementations
### Static Concurrency Buffer vs Adaptive Dwell-Time Load Shedding Layer

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: A static ConcurrencyLimitLayer with an unmonitored BufferLayer induces severe bufferbloat. If downstream latency spikes from 10ms to 2000ms, the 10,000-deep buffer introduces massive dwell time. Callers time out, but the server continues processing expired requests, amplifying tail latency into total system collapse.
```rust
use axum::{routing::get, Router};
use tower::limit::ConcurrencyLimitLayer;
use tower::buffer::BufferLayer;
use std::time::Duration;

// ANTI-PATTERN: Using static concurrency limits backed by unbounded or deep buffers.
// When downstream microservices slow down, requests accumulate in the buffer.
// By the time queued requests are dispatched, clients have already timed out,
// causing downstream services to waste compute on dead requests.
pub fn build_router() -> Router {
    Router::new()
        .route("/api/resource", get(|| async { "ok" }))
        .layer(BufferLayer::new(10_000)) // 10k items dwell indefinitely
        .layer(ConcurrencyLimitLayer::new(50)) // Static limit fails to adapt to degraded backends
}
```

#### Production Standard (Correct)
> **Standard**: Combines `tower::load_shed::LoadShedLayer` with Axum's `HandleErrorLayer` to instantly shed traffic when capacity or readiness drops. Downstream handlers and expensive deserializers are bypassed, returning 503 Service Unavailable with a Retry-After header, preventing tail-latency amplification.
```rust
use axum::{
    http::{header::RETRY_AFTER, HeaderValue, StatusCode},
    response::{IntoResponse, Response},
    routing::get,
    Router,
};
use std::time::{Duration, Instant};
use tower::{load_shed::LoadShedLayer, ServiceBuilder};
use tower_http::trace::TraceLayer;

#[derive(Clone)]
pub struct DwellTimeGuard {
    max_dwell: Duration,
}

impl DwellTimeGuard {
    pub fn new(max_dwell: Duration) -> Self {
        Self { max_dwell }
    }
}

// Map load shedding rejections into standards-compliant HTTP 503 responses
async fn handle_load_shed_error(
    err: tower::BoxError,
) -> Result<Response, (StatusCode, &'static str)> {
    if err.is::<tower::load_shed::error::Overloaded>() {
        let mut response = StatusCode::SERVICE_UNAVAILABLE.into_response();
        response.headers_mut().insert(
            RETRY_AFTER,
            HeaderValue::from_static("1"),
        );
        return Ok(response);
    }
    Err((StatusCode::INTERNAL_SERVER_ERROR, "Unhandled middleware error"))
}

pub fn build_resilient_router() -> Router {
    let load_control = ServiceBuilder::new()
        .layer(TraceLayer::new_for_http())
        // Outermost layer converts shedding signals into 503 + Retry-After
        .layer(axum::error_handling::HandleErrorLayer::new(handle_load_shed_error))
        // Tower load_shed triggers instant fail-fast if inner services fail poll_ready
        .layer(LoadShedLayer::new());

    Router::new()
        .route("/api/resource", get(|| async { "processed" }))
        .layer(load_control)
}
```

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 Adaptive Concurrency & Dwell-Time Load Shedding Guardrail

Axum 0.8 Tower Adaptive Concurrency & Dwell-Time Load Shedding Guardrail defines an industrial-grade behavioral standard for backend engineering. Enforces dynamic, queue-dwell-time-aware load shedding at the Tower layer in Axum 0.8 services to eliminate bufferbloat and tail-latency amplification. Prohibits static concurrency limiters and unbounded FIFO queues that process dead or expired requests during downstream saturation. 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 queuing and static concurrency limits causing bufferbloat, cascading collapse, and p99 tail latency explosion. 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 queuing and static concurrency limits causing bufferbloat, cascading collapse, and p99 tail latency explosion.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

Services MUST measure queue dwell time (time elapsed between arrival in the Tower service stack and actual dispatch) and reject requests whose dwell time exceeds the defined SLA threshold before invoking downstream handlers.

Services MUST measure queue dwell time (time elapsed between arrival in the Tower service stack and actual dispatch) and reject requests whose dwell time exceeds the defined SLA threshold before invoking downstream handlers.

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 rely solely on static Tower `ConcurrencyLimitLayer` or unmetered `BufferLayer`, as static limits do not adjust to downstream latency degradation and cause catastrophic head-of-line blocking.

Engineers MUST NOT rely solely on static Tower `ConcurrencyLimitLayer` or unmetered `BufferLayer`, as static limits do not adjust to downstream latency degradation and cause catastrophic head-of-line 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.
3

Load-shed rejections MUST immediately return HTTP 503 (Service Unavailable) or 429 (Too Many Requests) with a deterministic `Retry-After` header and drop the request body prior to costly JSON/Protobuf deserialization.

Load-shed rejections MUST immediately return HTTP 503 (Service Unavailable) or 429 (Too Many Requests) with a deterministic `Retry-After` header and drop the request body prior to costly JSON/Protobuf deserialization.

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 services MUST implement graceful backpressure by handling `tower

Tower services MUST implement graceful backpressure by handling `tower::load_shed::error::Overloaded` in an outermost error-handling layer rather than bubbling errors into unhandled panics or raw 500 Internal Server Errors.

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

The load-shed barrier MUST wrap the Axum router before CPU-intensive extractors, authentication barriers, or external database connection acquisition.

The load-shed barrier MUST wrap the Axum router before CPU-intensive extractors, authentication barriers, or external database connection acquisition.

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 Adaptive Concurrency & Dwell-Time Load Shedding Guardrail critical for AI coding assistants?

Enforces dynamic, queue-dwell-time-aware load shedding at the Tower layer in Axum 0.8 services to eliminate bufferbloat and tail-latency amplification. Prohibits static concurrency limiters and unbounded FIFO queues that process dead or expired requests during downstream saturation. 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 queuing and static concurrency limits causing bufferbloat, cascading collapse, and p99 tail latency explosion.) 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