Installation¶
Getting Traffik installed takes about thirty seconds. Let's make sure you get the right extras for your setup.
Requirements¶
Before you install, make sure you have:
- Python 3.9+ - Traffik uses modern async features and type hints throughout.
- FastAPI or Starlette - Traffik integrates with Starlette's
HTTPConnectionmodel, which FastAPI is built on.
That's it. No mandatory external services, no heavyweight dependencies. The in-memory backend works with no additional setup.
Install Options¶
Traffik ships with optional extras for each storage backend. Install only what you need.
What Each Extra Installs¶
| Extra | What it adds | When to use it |
|---|---|---|
| (none) | Core package + InMemoryBackend |
Local development, tests, single-process apps |
[redis] |
redis-py, pottery |
Multi-instance production deployments |
[memcached] |
aiomcache |
High-throughput environments where you already run Memcached |
[all] |
Everything above | When you want to try all backends |
[dev] |
Testing tools, linters, type checkers | Contributing to Traffik |
Choosing a Backend¶
Start with InMemory, graduate to Redis
During local development and in CI, the InMemoryBackend is perfect. It requires zero configuration and zero external services. When you're ready to deploy, swap it for RedisBackend, the rest of your code stays the same.
InMemory is not shared across processes
The InMemoryBackend lives in a single Python process. If you run your app with multiple workers (e.g., uvicorn --workers 4), each worker has its own independent counter. Use RedisBackend or MemcachedBackend for any multi-process or multi-host setup.
Memcached caveats
Memcached doesn't support atomic increment-and-expire in a single operation the way Redis does, so the MemcachedBackend uses best-effort distributed locks. This is fine for most workloads, but if you need strong consistency guarantees, Redis is the safer choice.
Verifying the Installation¶
Or from the command line:
Fully Typed¶
Traffik is a PEP 561 compliant package. It ships with a py.typed marker, which means type checkers like mypy and pyright pick up its type annotations automatically, no stub packages required.
Pyright / Pylance users
Traffik's generics (Throttle[Request], ThrottleBackend[..., Request]) resolve correctly with strict mode. If you see false positives, make sure you're on the latest version of your type checker.
Next Steps¶
You're all set. Head over to the Quick Start to write your first throttled endpoint.