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

Axum 0.8 Dynamic Gradient Concurrency Limiting and Adaptive Shedding

Mandates dynamic, gradient-based concurrency limits via Tower middleware in Axum 0.8 services to preemptively shed low-priority requests based on observed RTT drift. Prevents Tokio multi-threaded executor queue saturation and cascading tail latency collapse under burst traffic.

Prohibited Anti-Pattern: Static Concurrency Caps and Unbounded Tower Layers Leading to Tokio Worker Starvation

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 Dynamic Gradient Concurrency Limiting and Adaptive Shedding - Mandates dynamic, gradient-based concurrency limits via Tower middleware in Axum 0.8 services to preemptively shed low-priority requests based on observed RTT drift. Prevents Tokio multi-threaded executor queue saturation and cascading tail latency collapse under burst traffic.
globs: src/**/*.rs, crates/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-gradient-backpressure-shedding"
---

# Axum 0.8 Dynamic Gradient Concurrency Limiting and Adaptive Shedding

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Static Concurrency Caps and Unbounded Tower Layers Leading to Tokio Worker Starvation

## System Constraints
You are a Principal Systems Architect enforcing Axum 0.8 Gradient-Based Dynamic Backpressure Shedding. Mandate adaptive concurrency estimation (e.g., TCP Vegas/Gradient algorithms) in Tower middleware layers, ensuring low-priority requests are rejected with HTTP 503 or 429 before Tokio task queues suffer executor starvation.

## Strict Guidelines
- Engineers MUST ALWAYS compute moving minimum RTT (rtt_no_load) and short-term smoothed RTT to derive dynamic concurrency limits (gradient = rtt_min / rtt_current).
- Middleware MUST evaluate request priority (e.g., health probes vs. batch ingest) before attempting to acquire an execution permit.
- Engineers MUST NOT use static unbounded buffers or rigid global semaphores (e.g., `tokio::sync::Semaphore::new(10_000)`) as the primary load protection mechanism.
- Rejected requests MUST immediately return `StatusCode::SERVICE_UNAVAILABLE` (503) or `StatusCode::TOO_MANY_REQUESTS` (429) with a calibrated `Retry-After` header without reading the request body.
- Tower services MUST NEVER hold concurrency permits across downstream network I/O that is outside the application boundary unless explicitly bounded by strict timeouts.

## Bad vs Good Implementations
### Adaptive Gradient-Based Load Shedding vs. Unbounded Tower Layer

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: A static ConcurrencyLimitLayer does not monitor queue latency or response time degradation. When a downstream dependency slows down, Tokio's task queues fill up, causing catastrophic memory spikes and thread pool starvation.
```rust
use axum::{routing::get, Router};
use tower::limit::ConcurrencyLimitLayer;
use std::net::SocketAddr;

// ANTI-PATTERN: Fixed concurrency cap fails to react to downstream latency drift.
// Requests queue internally in Tokio worker queues, exhausting memory and starving worker threads.
pub fn app() -> Router {
    Router::new()
        .route("/api/v1/resource", get(|| async { "processed" }))
        // Rigid concurrency limit has no concept of gradient latency drift or priority
        .layer(ConcurrencyLimitLayer::new(10_000))
}
```

#### Production Standard (Correct)
> **Standard**: Implements adaptive gradient concurrency limit estimation directly inside the Tower middleware pipeline. Non-critical requests are shed instantaneously without touching executor threads or buffering payloads, maintaining steady throughput and low latency under high load.
```rust
use axum::{body::Body, http::{Request, Response, StatusCode, header}, response::IntoResponse, routing::get, Router};
use std::{sync::{atomic::{AtomicUsize, Ordering}, Arc}, time::{Duration, Instant}};
use tower::{Layer, Service};
use std::task::{Context, Poll};
use futures_util::future::BoxFuture;

#[derive(Clone)]
pub struct DynamicGradientLimiter {
    in_flight: Arc<AtomicUsize>,
    estimated_limit: Arc<AtomicUsize>,
    min_rtt_nanos: Arc<AtomicUsize>,
}

impl DynamicGradientLimiter {
    pub fn new(initial_limit: usize) -> Self {
        Self {
            in_flight: Arc::new(AtomicUsize::new(0)),
            estimated_limit: Arc::new(AtomicUsize::new(initial_limit)),
            min_rtt_nanos: Arc::new(AtomicUsize::new(1_000_000)), // 1ms base floor
        }
    }
}

#[derive(Clone)]
pub struct GradientMiddleware<S> {
    inner: S,
    limiter: DynamicGradientLimiter,
}

impl<S> Service<Request<Body>> for GradientMiddleware<S>
where
    S: Service<Request<Body>, Response = Response<Body>> + Clone + Send + 'static,
    S::Future: Send + 'static,
{
    type Response = Response<Body>;
    type Error = S::Error;
    type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, req: Request<Body>) -> Self::Future {
        let current = self.limiter.in_flight.load(Ordering::Relaxed);
        let limit = self.limiter.estimated_limit.load(Ordering::Relaxed);
        let is_critical = req.headers().contains_key("x-critical-priority");

        // Shed low-priority traffic early when nearing or exceeding gradient limit
        if !is_critical && current >= limit {
            let resp = Response::builder()
                .status(StatusCode::SERVICE_UNAVAILABLE)
                .header(header::RETRY_AFTER, "1")
                .body(Body::from("Load shed by dynamic backpressure"))
                .unwrap();
            return Box::pin(async move { Ok(resp) });
        }

        self.limiter.in_flight.fetch_add(1, Ordering::SeqCst);
        let limiter = self.limiter.clone();
        let mut inner = self.inner.clone();
        let start = Instant::now();

        Box::pin(async move {
            let res = inner.call(req).await;
            let elapsed = start.elapsed().as_nanos() as usize;
            limiter.in_flight.fetch_sub(1, Ordering::SeqCst);

            // Gradient calculation: limit = limit * (min_rtt / observed_rtt)
            let min_rtt = limiter.min_rtt_nanos.fetch_min(elapsed, Ordering::Relaxed).min(elapsed);
            let gradient = (min_rtt as f64) / (elapsed as f64).max(1.0);
            let new_limit = ((limit as f64 * gradient).round() as usize).clamp(5, 5_000);
            limiter.estimated_limit.store(new_limit, Ordering::Relaxed);

            res
        })
    }
}

pub fn app() -> Router {
    let limiter = DynamicGradientLimiter::new(100);
    Router::new()
        .route("/api/v1/resource", get(|| async { "processed safely" }))
        .layer(tower::layer::layer_fn(move |inner| GradientMiddleware { inner, limiter: limiter.clone() }))
}
```

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 Dynamic Gradient Concurrency Limiting and Adaptive Shedding

Axum 0.8 Dynamic Gradient Concurrency Limiting and Adaptive Shedding defines an industrial-grade behavioral standard for backend engineering. Mandates dynamic, gradient-based concurrency limits via Tower middleware in Axum 0.8 services to preemptively shed low-priority requests based on observed RTT drift. Prevents Tokio multi-threaded executor queue saturation and cascading tail latency collapse under burst traffic. 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: Static Concurrency Caps and Unbounded Tower Layers Leading to Tokio Worker Starvation 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 DriftStatic Concurrency Caps and Unbounded Tower Layers Leading to Tokio Worker StarvationCritical
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 compute moving minimum RTT (rtt_no_load) and short-term smoothed RTT to derive dynamic concurrency limits (gradient = rtt_min / rtt_current).

Engineers MUST ALWAYS compute moving minimum RTT (rtt_no_load) and short-term smoothed RTT to derive dynamic concurrency limits (gradient = rtt_min / rtt_current).

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

Middleware MUST evaluate request priority (e.g., health probes vs. batch ingest) before attempting to acquire an execution permit.

Middleware MUST evaluate request priority (e.g., health probes vs. batch ingest) before attempting to acquire an execution permit.

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 use static unbounded buffers or rigid global semaphores (e.g., `tokio

Engineers MUST NOT use static unbounded buffers or rigid global semaphores (e.g., `tokio::sync::Semaphore::new(10_000)`) as the primary load protection mechanism.

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

Rejected requests MUST immediately return `StatusCode

Rejected requests MUST immediately return `StatusCode::SERVICE_UNAVAILABLE` (503) or `StatusCode::TOO_MANY_REQUESTS` (429) with a calibrated `Retry-After` header without reading the request body.

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

Tower services MUST NEVER hold concurrency permits across downstream network I/O that is outside the application boundary unless explicitly bounded by strict timeouts.

Tower services MUST NEVER hold concurrency permits across downstream network I/O that is outside the application boundary unless explicitly bounded by strict timeouts.

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 Dynamic Gradient Concurrency Limiting and Adaptive Shedding critical for AI coding assistants?

Mandates dynamic, gradient-based concurrency limits via Tower middleware in Axum 0.8 services to preemptively shed low-priority requests based on observed RTT drift. Prevents Tokio multi-threaded executor queue saturation and cascading tail latency collapse under burst traffic. 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 (Static Concurrency Caps and Unbounded Tower Layers Leading to Tokio Worker Starvation) 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