Skip to content

paceapi

Rate limiting, caching and traffic management for Starlette and FastAPI. Async-native, strictly typed, and built on one dependency: Starlette.

Alpha

Rate limiting for FastAPI is ready to try. Caching and traffic management are planned. The API may change without notice until 1.0.

A limit in a few lines

from fastapi import Depends, FastAPI

from paceapi import Limiter

limiter = Limiter()
app = FastAPI()


@app.get("/search", dependencies=[Depends(limiter.limit("5/minute"))])
async def search(q: str = "") -> dict[str, str]:
    return {"query": q}

Each caller gets five searches a minute. Every response carries headers that say how many are left, and the sixth request is answered with a 429 that says how long to wait.

First steps walks through it.

What works today

  • Limits where FastAPI takes dependencies: on a route, a router or the whole app.
  • Keys that know who is calling: the caller's address, a header, or any dependency of your own, such as the signed-in user. See Keys and Limiting by user.
  • A rate for each plan, several rates at once, one allowance shared across routes, and requests that count for more than one. See Several limits.
  • Three algorithms: sliding window, fixed window and token bucket. See Algorithms.
  • Counts shared by every process through Redis or Valkey, in one round trip for each limit. See Stores.
  • A store in trouble costs little: a timeout, a rest for a store that keeps failing, and a fallback store. See Failure modes.
  • Plain Starlette too: middleware for a limit on the whole app, a decorator for one endpoint, and rate limit headers on every response. See Without FastAPI.
  • Tests without sleeping. See Testing your app.

What is planned

  • WebSocket limits and OpenTelemetry.
  • Caching for functions and HTTP responses, organised by namespace so related entries are invalidated together.
  • Traffic management: concurrency limits, deadlines and load shedding.

Design goals

  • Async-native, with no blocking storage calls on the event loop.
  • Strictly typed, and checked with both pyright and mypy.
  • One required dependency: Starlette.
  • Small, readable modules with protocol-based extension points.