← Back to the blog

Rust • 2026-11-26 • 3 min read

Running axum in Production: Timeouts, Backpressure and Graceful Shutdown

A default axum service is fast and almost entirely undefended. The five layers we add before anything faces real traffic, and why each one earns its place.

Running axum in Production: Timeouts, Backpressure and Graceful Shutdown — Rust

A fresh axum service will serve a benchmark beautifully and fall over in a way that is hard to diagnose the first time a client holds a connection open and sends one byte a minute. The framework is deliberately unopinionated about that. Here is the stack we put on every service before it leaves staging.

Timeouts, in both directions

A handler with no deadline is a handler that can be held forever. We apply a request timeout on the router and a separate, shorter one on every outbound call the handler makes.

// rust
use std::time::Duration;
use axum::{Router, error_handling::HandleErrorLayer};
use tower::ServiceBuilder;

let app = Router::new()
    .route("/v1/verify", post(verify))
    .layer(
        ServiceBuilder::new()
            .layer(HandleErrorLayer::new(handle_middleware_error))
            .load_shed()
            .concurrency_limit(512)
            .timeout(Duration::from_secs(5))
            .layer(RequestBodyLimitLayer::new(64 * 1024)),
    );

The order reads outside-in: load shedding sees the request first, the body limit last.

Backpressure is a feature

concurrency_limit plus load_shed is the pair that matters. Without load_shed, a concurrency limit queues excess requests, and a queue under sustained overload is just latency you cannot see. With it, the service returns 503 immediately once it is saturated. That is a far better signal to a load balancer, and it keeps p99 honest for the requests you did accept.

Pick the limit from measurement, not from a round number: run the service at increasing concurrency until p99 latency rises faster than throughput, and set the cap below that knee.

Body limits before parsing

RequestBodyLimitLayer rejects on Content-Length and while streaming, so an oversized upload never reaches your deserialiser. A JSON bomb that is refused at 64 kB costs nothing; the same payload parsed into a serde_json::Value first costs whatever the allocator gives it.

Shutdown that actually drains

A rolling deploy that cuts in-flight requests shows up as a thin band of 502s on every release — the kind everyone learns to ignore. axum::serve takes a shutdown signal; give it one that fires on both SIGTERM and SIGINT, and make the container's stop grace period longer than your request timeout. Otherwise the orchestrator kills the process before the drain finishes and the layer is decorative.

Tracing with a request id

Every response carries an id, and every log line inside the request carries the same one. It turns "a user reports a 500" into one search. tower-http's TraceLayer plus a span with the id is enough; what matters is that the id is in the response headers so support can quote it.

What we do not add

No retry middleware on the server. A retry belongs to the caller, which knows whether the operation is idempotent; a server that retries its own handler just multiplies the load during an incident. We also keep blocking work off the async runtime, which is the other half of keeping these numbers true.

Read next

COOKIES // MEASUREMENT

We count page views through Google Tag Manager. Say yes and it may store a cookie in your browser; say no and it still counts the visit, without storing anything and without an identifier. Nothing here is used for advertising.

Cookie Policy — you can change this at any time from the footer of any page.