tower-rate-limiter
Keyed, fixed-window HTTP rate limiting middleware for Tower. Limit repeated requests by account, API client, peer address, or any other application-defined identity.
- Tower-first core with optional Axum integration
- Fixed or request-derived quotas
- Process-local memory and shared Redis Stores
- Custom keys, responses, bypass rules, and Store failure behavior
- Optional structured tracing for Store failures
- IETF
RateLimitandRateLimit-Policyresponse fields
Guide · API documentation · Examples
Quick start
The default feature includes the process-local MemoryStore. The crate requires Rust 1.96 or
newer.
[]
= "0.1"
use Duration;
use Layer;
use ;
let limiter = builder
.policy_name
.limit
.window
.with_store
.build
.expect;
let service = limiter.layer;
The Store is always explicit through .with_store(...). IpKeyExtractor reads only the peer
SocketAddr from request extensions; Axum applications must provide ConnectInfo. Behind a trusted
proxy, ClientIpKeyExtractor can check supported client-IP headers first and fall back to that peer.
Only use its header-derived identity when the proxy removes or overwrites every accepted header. See the
quick-start guide for a
complete runnable example.
Stores
| Store | Use when |
|---|---|
MemoryStore |
One process owns the quota, or for local development |
RedisStore |
Multiple processes must share one quota |
Custom Store |
The application needs another counter backend |
For Redis with Tokio and the default transaction implementation:
[]
= {
version = "0.1",
= false,
= ["redis", "runtime-tokio"],
}
Use redis-lua instead of redis for the Lua implementation, or runtime-smol instead of
runtime-tokio for Smol. The application provides an established Redis connection and owns its
lifecycle.
Documentation
| Topic | Guide |
|---|---|
| Installation and first Layer | Quick start |
| Charging and fixed-window semantics | How it works |
| Builder options and failure behavior | Configuration |
| Axum, Redis, and proxy considerations | Adapters |
| Custom keys, quotas, Stores, and responses | Custom components |
| Response field formats | Rate Limit fields |
| Deployment checklist | Production guide |
Runnable Tower, Axum, dynamic-quota, proxy, and Redis integrations are collected in
examples/.
Contributing
Bug reports, feature requests, and contributions are welcome through GitHub Issues.
License
Licensed under either
- Apache License, Version 2.0 (LICENSE-APACHE); or
- MIT License (LICENSE-MIT).