Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Quick start

This guide builds a process-local limiter around a plain Tower service. It is the shortest path to the first working layer; the same layer can later be applied to an Axum router.

1. Add the dependency

The default feature enables the in-memory FixedWindowStore:

[dependencies]
tower-rate-limiter = "0.2"

The crate requires Rust 1.96 or newer.

2. Choose a client key

Define how a request becomes an application-owned client key and compose the Layer:

use std::{convert::Infallible, error::Error, future::ready, time::Duration};

use http::{Request, Response};
use tower::{Layer, service_fn};
use tower_rate_limiter::{FixedWindow, KeyExtractor, MemoryStore, RateLimitLayer};

#[derive(Clone, Copy)]
struct StaticClient;

impl KeyExtractor for StaticClient {
    type Key = String;

    fn extract<B>(&self, _request: &Request<B>) -> Result<Self::Key, tower_rate_limiter::RateLimitError> {
        Ok(String::from("example-client"))
    }
}

fn main() -> Result<(), Box<dyn Error>> {
    dotenvy::dotenv().ok();

    let limiter = RateLimitLayer::builder(StaticClient)
        .policy_name("tower-example")
        .with_policy(FixedWindow::new(MemoryStore::new(), 100, Duration::from_secs(60))?)
        .build()?;

    let _service = limiter.layer(service_fn(|_request: Request<()>| {
        ready(Ok::<Response<()>, Infallible>(Response::new(())))
    }));

    println!("constructed a Tower MemoryStore rate limiter");
    Ok(())
}

Run the complete example from the repository root:

cargo run --example tower_memory --features memory

The example uses one static key so its construction is easy to see. In a real service, extract a validated account ID, API-client ID, peer address, or another stable identity. Different callers must produce different keys; repeated requests from one caller must produce the same key.

3. Configure the policy deliberately

The minimal production-shaped builder is:

#![allow(unused)]
fn main() {
use std::time::Duration;
use tower_rate_limiter::{ConfigError, FixedWindow, IpKeyExtractor, MemoryStore, RateLimitLayer};

fn configure() -> Result<RateLimitLayer<IpKeyExtractor, FixedWindow<MemoryStore>>, ConfigError> {
    let layer = RateLimitLayer::builder(IpKeyExtractor::new())
        .policy_name("public-api")
        .with_policy(FixedWindow::new(MemoryStore::new(), 100, Duration::from_secs(60))?)
        .build()?;
    Ok(layer)
}
}

build() validates layer configuration, including a non-empty policy name. FixedWindow::new(...) validates its window, which must be at least one millisecond. The typed builder also prevents build() until a policy has been supplied.

Builder defaults

SettingDefault
Policy namedefault-policy
Policy errorsReject with 503 Service Unavailable
Response fieldsIETF draft 11

A real application should set a stable policy name and select its policy deliberately. Layers that share a policy instance or backend state, policy name, and extracted key intentionally share usage.

What happens on each request

The middleware first extracts a key, scopes it with the policy name, then asks the selected policy to charge it. A policy failure consumes no quota metadata, while a charged request is not refunded based on the downstream response.

The first limit requests are allowed. Request limit + 1 is rate limited, and rejected requests continue to increment usage without extending the active fixed window.

For a limit of 2, the sequence is:

RequestFixedWindowUsage::usedRemainingResult
111inner service called
220inner service called
330429 Too Many Requests

The response from the second request has zero remaining quota but is still allowed. Exhaustion is enforced only when used > limit.

Next steps

  • Read How it works for the public extension points.
  • Browse the complete examples for Tower, Axum, proxy handling, and Redis.
  • Review every builder option in Configuration.
  • Use Axum and Redis when the service needs framework or shared-store integration.
  • Browse the repository’s examples/ for nested policies, proxy handling, and custom error responses.