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

tower-rate-limiter

Keyed HTTP rate limiting middleware for Tower.

tower-rate-limiter lets an application decide who a request belongs to and which policy charges it. The middleware owns the HTTP response flow without coupling the core to Axum, Tokio, Redis, or a built-in identity policy.

Request
  -> optional bypass
  -> KeyExtractor
  -> RateLimitPolicy::check
  -> allow the ready inner service or return a response

When to use it

Use this crate when you need per-caller HTTP enforcement in a Tower service, for example:

  • one quota per authenticated account or API client;
  • an IP-based limit at an application boundary;
  • route-specific policies composed as Tower Layers;
  • fixed-window counters or GCRA quotas with process-local, Redis, or PostgreSQL state.

This is not a global concurrency limiter or a backpressure mechanism. The included policy is fixed window; other algorithms are expressed as explicit policies.

Design at a glance

ConcernApplication-facing seamIncluded option
Caller identityKeyExtractorIpKeyExtractor, ClientIpKeyExtractor, TrustedProxyClientIpKeyExtractor
Charging algorithmRateLimitPolicyFixedWindow<S>, MemoryGcra, RedisGcra, PostgresGcra
Fixed-window storageFixedWindowStoreMemoryStore, optional RedisStore
Smooth quota configurationGcraQuotaoptional Memory, Redis, and PostgreSQL GCRA policies
Error and rejection responsesResponseFactoryDefaultResponseFactory

The policy is always explicit. This makes the charging algorithm and its state-ownership boundary visible at layer construction instead of hiding process-local state behind a default singleton.

Cargo features

FeatureDefaultAdds
memoryyesRuntime-independent MemoryStore for FixedWindow
axumnoAxum ConnectInfo<SocketAddr> support in the built-in IP extractors
redisnoRedisStore backed by an existing multiplexed connection for FixedWindow
redis-luanoRedis increment through Lua instead of MULTI/EXEC
memory-gcranoMemoryGcra: process-local, governor-backed one-request GCRA
redis-gcranoRedisGcra: shared one-request GCRA through an attributed Redis Lua artifact
postgres-gcranoPostgresGcra: shared one-request GCRA through caller-installed PostgreSQL SQL; SQLx Tokio runtime
runtime-tokionoTokio-compatible Redis async runtime
runtime-smolnoSmol-compatible Redis async runtime
tracingnoStructured events for policy failures

RedisStore needs an increment implementation (redis or redis-lua) together with one async runtime (runtime-tokio or runtime-smol).

MemoryGcra needs memory-gcra only. RedisGcra needs redis-gcra together with one async runtime and uses ordinary Redis, not a Redis module. PostgresGcra needs postgres-gcra; its SQLx adapter is Tokio-only and requires an application-applied migration. See GCRA backends for the shared contract and backend-specific deployment guides.

With default-features = false, the core remains usable with application-provided implementations and does not pull in Axum, Redis, or an async runtime.

Start here

  1. Follow the Quick start to construct a Tower layer.
  2. Browse the complete examples for progressively richer integrations.
  3. Review Configuration before choosing policy names and failure mode.
  4. Read How it works for exact charging semantics.
  5. Use Axum and Redis, choose a GCRA backend, or implement Custom components.
  6. Check the Production guide before deploying behind a proxy or across replicas.

The API documentation is the complete type-level reference. The GitHub repository contains runnable examples.