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

Axum 0.8 Tower Workload-Shedding & Concurrency-Limit Guardrail

Mandates bounded concurrency limits and fail-fast load-shedding middleware across Axum 0.8 services to prevent Tokio worker thread starvation and connection saturation. Enforces explicit separation between critical orchestration probes and shed-sensitive business routes while mapping Tower Overloaded errors to deterministic 503 responses.

Prohibited Anti-Pattern: Unbounded Route Concurrency and Blanket Load-Shedding Leading to Worker Starvation and Orchestration Crash Loops

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 Tower Workload-Shedding & Concurrency-Limit Guardrail - Mandates bounded concurrency limits and fail-fast load-shedding middleware across Axum 0.8 services to prevent Tokio worker thread starvation and connection saturation. Enforces explicit separation between critical orchestration probes and shed-sensitive business routes while mapping Tower Overloaded errors to deterministic 503 responses.
globs: src/**/*.rs, crates/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-tower-workload-shedding-concurrency-guardrail"
---

# Axum 0.8 Tower Workload-Shedding & Concurrency-Limit Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Route Concurrency and Blanket Load-Shedding Leading to Worker Starvation and Orchestration Crash Loops

## System Constraints
You are a Principal Rust Architect enforcing Axum 0.8 Tower Workload-Shedding and Concurrency-Limit Guardrails. You must ensure that high-throughput web services apply explicit backpressure, prevent Tokio runtime thread pool starvation, isolate Kubernetes health endpoints from load-shedding layers, and properly translate Tower layer errors into standard HTTP 503 Service Unavailable responses with Retry-After headers.

## Strict Guidelines
- Engineers MUST ALWAYS isolate orchestrator liveness and readiness probe routes (e.g., '/healthz', '/ready') outside the Tower LoadShedLayer and ConcurrencyLimitLayer stack.
- Engineers MUST ALWAYS pair 'tower::load_shed::LoadShedLayer' with 'axum::error_handling::HandleErrorLayer' to map 'tower::load_shed::error::Overloaded' to HTTP 503 (Service Unavailable) before responses reach the client socket.
- Engineers MUST NOT configure unbounded in-flight requests on database- or CPU-bound subrouters; concurrency limits MUST be bounded via 'tower::limit::ConcurrencyLimitLayer' calibrated to backend pool capacity.
- Services MUST emit a 'Retry-After' HTTP header on shed requests to prevent immediate client retry storms from amplifying socket saturation.
- Components NEVER rely on default TCP connection dropping or unhandled Tower error propagation, which terminates connections abruptly or induces Axum hyper handler panics.

## Bad vs Good Implementations
### Unbounded In-Flight Requests with Indiscriminate Health Check Shedding

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Applying LoadShedLayer without an explicit HandleErrorLayer causes unhandled BoxError errors inside Axum, generating 500 Internal Server Errors or connection drops. Enclosing '/healthz' in the load-shedding stack causes orchestration probes to fail under load, prompting Kubernetes to restart saturated but functioning pods.
```rust
use axum::{routing::get, Router};
use tower::ServiceBuilder;
use tower::load_shed::LoadShedLayer;
use tower::limit::ConcurrencyLimitLayer;

// ANTI-PATTERN: Health probes are enclosed inside LoadShedLayer.
// Under high load, /healthz gets shed, causing Kubernetes to kill
// healthy but saturated pods, triggering a catastrophic cascading failure.
// Also lacks HandleErrorLayer, causing 500 Internal Errors or hyper drops.
pub fn build_router() -> Router {
    Router::new()
        .route("/healthz", get(|| async { "OK" }))
        .route("/api/compute", get(|| async { "computed" }))
        .layer(
            ServiceBuilder::new()
                .layer(ConcurrencyLimitLayer::new(1024))
                .layer(LoadShedLayer::new()) // Unhandled Overloaded error leads to internal crash
        )
}
```

#### Production Standard (Correct)
> **Standard**: Liveness/readiness probes bypass load-shedding layers, preventing orchestration death spirals. The API routes enforce a strict limit of 256 concurrent operations. Tower's Overloaded error is trapped by HandleErrorLayer and translated into an HTTP 503 response with a standard Retry-After header.
```rust
use axum::{
    error_handling::HandleErrorLayer,
    http::{header, HeaderMap, HeaderValue, StatusCode},
    response::IntoResponse,
    routing::get,
    BoxError,
    Router,
};
use std::time::Duration;
use tower::{
    limit::ConcurrencyLimitLayer,
    load_shed::{
        error::Overloaded,
        LoadShedLayer,
    },
    ServiceBuilder,
};

async fn handle_overloaded_or_timeout(err: BoxError) -> impl IntoResponse {
    if err.is::<Overloaded>() {
        let mut headers = HeaderMap::new();
        headers.insert(header::RETRY_AFTER, HeaderValue::from_static("2"));
        (
            StatusCode::SERVICE_UNAVAILABLE,
            headers,
            "Service is overloaded; please retry shortly.",
        )
            .into_response()
    } else {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("Unhandled middleware error: {err}"),
        )
            .into_response()
    }
}

pub fn build_production_router() -> Router {
    // Probe router: Exempt from load shedding to maintain kubelet liveness
    let probe_routes = Router::new().route("/healthz", get(|| async { "OK" }));

    // Business router: Protected with bounded concurrency and adaptive load shedding
    let api_routes = Router::new()
        .route("/api/compute", get(|| async { "computed" }))
        .layer(
            ServiceBuilder::new()
                .layer(HandleErrorLayer::new(handle_overloaded_or_timeout))
                .layer(LoadShedLayer::new())
                .layer(ConcurrencyLimitLayer::new(256)),
        );

    probe_routes.merge(api_routes)
}
```

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 Workload-Shedding & Concurrency-Limit Guardrail

Axum 0.8 Tower Workload-Shedding & Concurrency-Limit Guardrail defines an industrial-grade behavioral standard for backend engineering. Mandates bounded concurrency limits and fail-fast load-shedding middleware across Axum 0.8 services to prevent Tokio worker thread starvation and connection saturation. Enforces explicit separation between critical orchestration probes and shed-sensitive business routes while mapping Tower Overloaded errors to deterministic 503 responses. 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 Route Concurrency and Blanket Load-Shedding Leading to Worker Starvation and Orchestration Crash Loops 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 Route Concurrency and Blanket Load-Shedding Leading to Worker Starvation and Orchestration Crash LoopsCritical
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 isolate orchestrator liveness and readiness probe routes (e.g., '/healthz', '/ready') outside the Tower LoadShedLayer and ConcurrencyLimitLayer stack.

Engineers MUST ALWAYS isolate orchestrator liveness and readiness probe routes (e.g., '/healthz', '/ready') outside the Tower LoadShedLayer and ConcurrencyLimitLayer stack.

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 ALWAYS pair 'tower

Engineers MUST ALWAYS pair 'tower::load_shed::LoadShedLayer' with 'axum::error_handling::HandleErrorLayer' to map 'tower::load_shed::error::Overloaded' to HTTP 503 (Service Unavailable) before responses reach the client socket.

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 NOT configure unbounded in-flight requests on database- or CPU-bound subrouters

Engineers MUST NOT configure unbounded in-flight requests on database- or CPU-bound subrouters; concurrency limits MUST be bounded via 'tower::limit::ConcurrencyLimitLayer' calibrated to backend pool capacity.

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

Services MUST emit a 'Retry-After' HTTP header on shed requests to prevent immediate client retry storms from amplifying socket saturation.

Services MUST emit a 'Retry-After' HTTP header on shed requests to prevent immediate client retry storms from amplifying socket saturation.

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 rely on default TCP connection dropping or unhandled Tower error propagation, which terminates connections abruptly or induces Axum hyper handler panics.

Components NEVER rely on default TCP connection dropping or unhandled Tower error propagation, which terminates connections abruptly or induces Axum hyper handler panics.

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 Workload-Shedding & Concurrency-Limit Guardrail critical for AI coding assistants?

Mandates bounded concurrency limits and fail-fast load-shedding middleware across Axum 0.8 services to prevent Tokio worker thread starvation and connection saturation. Enforces explicit separation between critical orchestration probes and shed-sensitive business routes while mapping Tower Overloaded errors to deterministic 503 responses. 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 Route Concurrency and Blanket Load-Shedding Leading to Worker Starvation and Orchestration Crash Loops) 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