Skip to content

Performance

Single-call microbenchmarks measured with the scripts in benchmarks/ (limits.py, cache.py, and traffic.py).

Rate limiting

Benchmark Median (µs/call, 3 runs) Range across all rounds (µs)
MemoryStore — FixedWindow 4.4 4.3 – 4.7
MemoryStore — SlidingWindow 4.5 4.4 – 4.6
MemoryStore — TokenBucket 4.0 3.9 – 4.0
RedisStore — FixedWindow 153 150 – 171
RedisStore — SlidingWindow 154 152 – 164
RedisStore — TokenBucket 152 147 – 158
FastAPI route (baseline, no limit) 106 102 – 111
FastAPI route + 1 in-memory limit 136 132 – 140
  • In-memory check: 4–5 µs per call across all three algorithms.
  • FastAPI route overhead: ~30 µs on top of the baseline route. About 10 µs is FastAPI resolving two extra dependencies (the limit and its key); the rest is key lookup, route scoping, the check itself, and header formatting, which run 2–3x slower between requests than in a hot loop.
  • Redis check: 1 round trip (~150 µs against local Docker Redis). Over a network, cost is dominated by network RTT rather than algorithm choice.

Caching

Benchmark Median (µs/call, 3 runs) Range across all rounds (µs)
MemoryStore — hit 9.7 9.4 – 10.6
MemoryStore — miss (load + store) 69 68 – 71
RedisStore — hit 182 172 – 189
RedisStore — miss (load + store) 404 393 – 419
FastAPI route (baseline, uncached) 112 111 – 113
FastAPI route + @ns.response 146 144 – 149
FastAPI route + ResponseCacheMiddleware 133 130 – 142
  • Hit: ~10 µs in memory; 1 round trip in Redis.
  • Miss: ~7x a hit in memory and ~2x in Redis (a read followed by a write: 2 round trips), excluding the loader itself, which is a no-op here. Misses run through single-flight and serialize the result.
  • Response caching: The benchmarked route does no work, so these rows show overhead rather than savings: ~35 µs for @ns.response, and ~20 µs for ResponseCacheMiddleware (its overhead net of skipping FastAPI routing). Caching pays off when an endpoint costs more than that and requests repeat: a miss still runs the endpoint and adds a read and a write.

Traffic management

Benchmark Median (µs/call, 3 runs) Range across all rounds (µs)
FastAPI route (baseline, no middleware) 118 117 – 120
+ ConcurrencyLimitMiddleware 121 119 – 126
+ DeadlineMiddleware 126 123 – 152
+ LoadSheddingMiddleware 123 122 – 125
+ CoalescingMiddleware 127 126 – 129
+ all four 139 136 – 141
  • Uncontended overhead: 3–10 µs each (no queueing, timeouts, shedding, or coalescing triggered); ~20 µs for all four.
  • Higher baseline: This benchmark yields to the event loop after every request, as a server does. Without that, LoadSheddingMiddleware correctly reads a never-yielding loop as sustained lag and sheds.

Scope of these numbers

These measure sequential single-call latency on an otherwise idle event loop (with ASGI calls executed in-process, no HTTP server or TCP stack). They do not measure multi-worker throughput, contention, or Redis under load.

Each table comes from a separate run on a machine in ordinary use (the bare route measured 106 µs in one and 112 µs in another), so compare rows against the baseline in the same table.

Environment and reproduction

Recorded 2 December 2026 on Apple Silicon (macOS 26.7, arm64) with Python 3.12.9, FastAPI 0.165.0, Starlette 1.10.0, redis-py 8.2.0, and local containerized Redis 8.12.0. Each run executes 7 rounds (20,000 calls/round for in-memory checks and values; 2,000 for Redis and FastAPI routes). Tables report the median of three runs' medians and the min/max across all rounds.

Reproduce locally:

just services
just bench --redis redis://127.0.0.1:6391

Omit --redis to skip the Redis benchmarks. Each script exits non-zero if a benchmarked request is rejected or a cache "hit" turns out to be a miss.