This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: axum-tower-graceful-drain-loadshed-invariant

Axum 0.8 & Tower Deterministic Worker Drain and Load-Shedding Invariant

Enforces deterministic termination of in-flight requests and immediate upstream backpressure propagation during Kubernetes rolling updates under extreme concurrency. Mandates readiness probe degradation before socket close, Tower load-shedding layers with explicit 503 Service Unavailable responses, and bounded graceful drain timeouts to prevent TCP RST drops.

Prohibited Anti-Pattern: Naive axum::serve Graceful Shutdown Lacking Readiness De-registration, Pre-Stop Delay, and Tower Load-Shedding

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 & Tower Deterministic Worker Drain and Load-Shedding Invariant - Enforces deterministic termination of in-flight requests and immediate upstream backpressure propagation during Kubernetes rolling updates under extreme concurrency. Mandates readiness probe degradation before socket close, Tower load-shedding layers with explicit 503 Service Unavailable responses, and bounded graceful drain timeouts to prevent TCP RST drops.
globs: src/**/*.rs, crates/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-tower-graceful-drain-loadshed-invariant"
---

# Axum 0.8 & Tower Deterministic Worker Drain and Load-Shedding Invariant

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Naive axum::serve Graceful Shutdown Lacking Readiness De-registration, Pre-Stop Delay, and Tower Load-Shedding

## System Constraints
You are a Principal Distributed Systems Engineer enforcing Axum 0.8 and Tower service architecture standards. You mandate deterministic worker draining, proactive readiness probe tripping, load-shedding middleware with explicit HTTP 503 backpressure, and bounded execution timeouts during Kubernetes pod termination.

## Strict Guidelines
- Engineers MUST ALWAYS decouple the readiness probe state from liveness probes using an atomic flag or watch channel, marking readiness as false immediately upon receiving SIGTERM.
- Engineers MUST ALWAYS insert a configurable sleep delay (e.g., 5-15s) between readiness degradation and listener socket shutdown to allow Kubernetes iptables/IPVS endpoints to drain upstream traffic cleanly.
- Engineers MUST wrap request pipelines in Tower's `LoadShedLayer` combined with `ConcurrencyLimitLayer` or `RateLimitLayer` to reject excess requests with explicit HTTP 503 (Service Unavailable) and `Retry-After: 0` headers rather than dropping raw sockets.
- Engineers MUST NOT block Axum's `.with_graceful_shutdown()` indefinitely; in-flight request draining MUST be bounded by a hard deadline timeout using `tokio::time::timeout` or `tokio::select!`.
- Code MUST set `Connection: close` headers (or initiate HTTP/2 `GOAWAY` frame sequences) on responses initiated during the draining phase to instruct load balancers to terminate keep-alive connections cleanly.
- Components NEVER rely on naive SIGINT-only termination handlers; both SIGTERM (Kubernetes default) and SIGINT MUST be captured and coordinated with asynchronous cancellation tokens.

## Bad vs Good Implementations
### Naive Shutdown vs. Deterministic Draining with Tower Load-Shedding

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: This pattern causes 502 Bad Gateway and connection reset errors in Kubernetes. When SIGTERM arrives, the process continues serving or dies abruptly without notifying kube-proxy. Unbounded requests hang until Kubernetes issues SIGKILL, terminating customer requests mid-flight.
```rust
use axum::{routing::get, Router};
use tokio::net::TcpListener;

#[tokio::main]
async fn main() {
    let app = Router::new().route("/work", get(|| async { "done" }));
    let listener = TcpListener::bind("0.0.0.0:8080").await.unwrap();

    // ANTI-PATTERN: Immedate termination on Ctrl+C only.
    // Ignores SIGTERM, ignores K8s iptables propagation latency, lacks Tower load-shedding,
    // and permits unbounded in-flight requests that get violently SIGKILL'd after terminationGracePeriodSeconds.
    axum::serve(listener, app)
        .with_graceful_shutdown(async {
            tokio::signal::ctrl_c().await.unwrap();
        })
        .await
        .unwrap();
}
```

#### Production Standard (Correct)
> **Standard**: De-registers the pod from Kubernetes service endpoints before initiating server shutdown, sheds load deterministically via Tower with standard 503 and Retry-After headers, and coordinates both SIGTERM and SIGINT under bounded conditions.
```rust
use axum::{
    error_handling::HandleErrorLayer,
    http::{header, StatusCode},
    response::IntoResponse,
    routing::get,
    BoxError, Router,
};
use std::{sync::{atomic::{AtomicBool, Ordering}, Arc}, time::Duration};
use tokio::{net::TcpListener, signal::unix::{signal, SignalKind}, sync::watch};
use tower::{load_shed::LoadShedLayer, limit::ConcurrencyLimitLayer, ServiceBuilder};

#[derive(Clone)]
struct AppState {
    is_ready: Arc<AtomicBool>,
}

async fn handle_shed_error(err: BoxError) -> impl IntoResponse {
    if err.is::<tower::load_shed::error::Overloaded>() {
        (
            StatusCode::SERVICE_UNAVAILABLE,
            [(header::RETRY_AFTER, "0")],
            "Service Overloaded / Shedding Load",
        )
    } else {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            [(header::RETRY_AFTER, "5")],
            "Internal Server Error",
        )
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let is_ready = Arc::new(AtomicBool::new(true));
    let (drain_tx, mut drain_rx) = watch::channel(false);

    let app_state = AppState { is_ready: is_ready.clone() };

    let middleware_stack = ServiceBuilder::new()
        .layer(HandleErrorLayer::new(handle_shed_error))
        .layer(LoadShedLayer::new())
        .layer(ConcurrencyLimitLayer::new(1024));

    let app = Router::new()
        .route("/readyz", get(|state: axum::extract::State<AppState>| async move {
            if state.is_ready.load(Ordering::Acquire) {
                StatusCode::OK
            } else {
                StatusCode::SERVICE_UNAVAILABLE
            }
        }))
        .route("/work", get(|| async { "done" }))
        .layer(middleware_stack)
        .with_state(app_state);

    let listener = TcpListener::bind("0.0.0.0:8080").await?;

    // Spawn signal orchestrator
    tokio::spawn(async move {
        let mut sigterm = signal(SignalKind::terminate()).expect("failed to install SIGTERM handler");
        let mut sigint = signal(SignalKind::interrupt()).expect("failed to install SIGINT handler");

        tokio::select! {
            _ = sigterm.recv() => tracing::info!("Received SIGTERM"),
            _ = sigint.recv() => tracing::info!("Received SIGINT"),
        }

        // Step 1: Immediately trip readiness check to 503
        is_ready.store(false, Ordering::Release);

        // Step 2: Allow Kube-proxy / Ingress endpoint convergence window
        tokio::time::sleep(Duration::from_secs(10)).await;

        // Step 3: Trigger Axum graceful worker drain
        let _ = drain_tx.send(true);
    });

    axum::serve(listener, app)
        .with_graceful_shutdown(async move {
            let _ = drain_rx.wait_for(|&draining| draining).await;
        })
        .await?;

    Ok(())
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Axum 0.8 & Tower Deterministic Worker Drain and Load-Shedding Invariant

Axum 0.8 & Tower Deterministic Worker Drain and Load-Shedding Invariant defines an industrial-grade behavioral standard for backend engineering. Enforces deterministic termination of in-flight requests and immediate upstream backpressure propagation during Kubernetes rolling updates under extreme concurrency. Mandates readiness probe degradation before socket close, Tower load-shedding layers with explicit 503 Service Unavailable responses, and bounded graceful drain timeouts to prevent TCP RST drops. 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: Naive axum::serve Graceful Shutdown Lacking Readiness De-registration, Pre-Stop Delay, and Tower Load-Shedding 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 DriftNaive axum::serve Graceful Shutdown Lacking Readiness De-registration, Pre-Stop Delay, and Tower Load-SheddingCritical
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 decouple the readiness probe state from liveness probes using an atomic flag or watch channel, marking readiness as false immediately upon receiving SIGTERM.

Engineers MUST ALWAYS decouple the readiness probe state from liveness probes using an atomic flag or watch channel, marking readiness as false immediately upon receiving SIGTERM.

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 insert a configurable sleep delay (e.g., 5-15s) between readiness degradation and listener socket shutdown to allow Kubernetes iptables/IPVS endpoints to drain upstream traffic cleanly.

Engineers MUST ALWAYS insert a configurable sleep delay (e.g., 5-15s) between readiness degradation and listener socket shutdown to allow Kubernetes iptables/IPVS endpoints to drain upstream traffic cleanly.

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 wrap request pipelines in Tower's `LoadShedLayer` combined with `ConcurrencyLimitLayer` or `RateLimitLayer` to reject excess requests with explicit HTTP 503 (Service Unavailable) and `Retry-After

Engineers MUST wrap request pipelines in Tower's `LoadShedLayer` combined with `ConcurrencyLimitLayer` or `RateLimitLayer` to reject excess requests with explicit HTTP 503 (Service Unavailable) and `Retry-After: 0` headers rather than dropping raw sockets.

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

Engineers MUST NOT block Axum's `.with_graceful_shutdown()` indefinitely

Engineers MUST NOT block Axum's `.with_graceful_shutdown()` indefinitely; in-flight request draining MUST be bounded by a hard deadline timeout using `tokio::time::timeout` or `tokio::select!`.

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

Code MUST set `Connection

Code MUST set `Connection: close` headers (or initiate HTTP/2 `GOAWAY` frame sequences) on responses initiated during the draining phase to instruct load balancers to terminate keep-alive connections cleanly.

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

Components NEVER rely on naive SIGINT-only termination handlers

Components NEVER rely on naive SIGINT-only termination handlers; both SIGTERM (Kubernetes default) and SIGINT MUST be captured and coordinated with asynchronous cancellation tokens.

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 Deterministic Worker Drain and Load-Shedding Invariant critical for AI coding assistants?

Enforces deterministic termination of in-flight requests and immediate upstream backpressure propagation during Kubernetes rolling updates under extreme concurrency. Mandates readiness probe degradation before socket close, Tower load-shedding layers with explicit 503 Service Unavailable responses, and bounded graceful drain timeouts to prevent TCP RST drops. 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 (Naive axum::serve Graceful Shutdown Lacking Readiness De-registration, Pre-Stop Delay, and Tower Load-Shedding) 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