Stores¶
A store is where the counts are kept. paceapi comes with two, and the choice between them is whether your app runs as one process or several.
In memory¶
With no store given, a limiter keeps its counts in the memory of the process. There is nothing to set up, which makes it right for development, for tests and for an app that runs as a single process.
It cannot be shared. With four workers each keeps its own counts, so a caller can get up to four times the limit, and the counts are gone when a process restarts. It is bounded: the store holds up to 100,000 buckets and drops the ones written longest ago beyond that, so a flood of made-up keys cannot use up your memory.
Redis¶
Give the limiter a RedisStore and every
process shares one count:
# The client is yours: create it as you would for anything else, and close it
# when your app shuts down. Nothing connects until the first request.
redis = from_url("redis://localhost:6379")
limiter = Limiter(RedisStore(redis))
It needs redis-py 8 or newer, which comes with the redis extra:
The whole app is in
examples/redis_store.py.
Nothing else about your limits changes.
- The client is yours. The store uses the client you give it and never closes it. It can be the same one the rest of your app uses.
- One round trip for each limit. A limit is checked and counted by one script inside Redis, however many rates it has. That also makes it exact: two requests arriving at the same moment can never both take the last place.
- One clock. The time comes from the Redis server, so your processes agree even when their own clocks do not.
- Nothing left behind. A bucket's key expires when its count no longer matters, so callers who stop calling cost nothing.
- A burst waits its turn. redis-py's connection pool raises when it runs out of connections, and a limiter that treated that as a failure would let the burst through. The store never has more than half of the client's pool in use at once, and the rest of a burst waits in line. A limiter does not count that wait against its timeout. Each store takes half of its client's pool, so limiters that share a client should share the store as well.
- Valkey works too. The store is tested against Redis 7, Redis 8 and Valkey 8.
Sharing a server¶
Every key starts with pace. If two apps share a server and must not share
counts, give each limiter its own prefix: Limiter(store, prefix="billing").
Clusters¶
Pass a redis.asyncio.RedisCluster client and the store works the same way. A
cluster only runs a script whose keys all hash to one slot, so the keys of one
limit are written to land in the same one. You do not need to do anything.
Sharing a client¶
The store leaves half of the client's connection pool to the rest of your app.
If the app can use more than that half at the same moment, give the limiter a
client of its own, or give the shared client a pool that waits when it is
empty (redis.asyncio.BlockingConnectionPool).
With access control lists, the user the limiter connects as needs EVAL,
EVALSHA, GET, MGET, SET, DEL and TIME.
When Redis is short of memory¶
A key is small and expires on its own, so what Redis holds at any moment is
roughly the callers of the last period. The store sets no limit of its own
beyond that, as the memory store does, so a flood of made-up keys takes memory
until those keys expire. When the server is full, what happens depends on its
maxmemory-policy:
- With an eviction policy, Redis may drop a counter early. That caller's count starts again, so the limit is briefly more generous than you set it.
- With
noeviction, which is Redis's default, writes fail. The limiter treats that as it treats any other failure of the store.
For limits that must hold, give Redis room to spare or an instance of its own.
When Redis cannot be reached¶
A slow or unreachable Redis does not hold up every request, and limits can carry on from memory in the meantime. Failure modes explains how.
An algorithm of your own¶
The three built-in algorithms run inside Redis. One you write yourself works
too, without any Lua: its step runs in your process, between a read and a
write that only goes through if nothing changed in the meantime.
That costs two round trips for each limit. Requests for one caller that arrive in the same process take turns. Across processes a write can lose to another and has to be tried again; one that keeps losing is in the end answered as over the limit, never let through uncounted, and a warning is logged at most once a minute. A forced cost keeps being tried for as long as its caller waits. For a limit that is busy across many processes, the built-in algorithms are the better choice.
A store of your own¶
A store is anything with the apply method of
LimiterStore. The rules it has to
keep are written there, and evaluate does
the part that is the same for every store.
If your store makes calls wait in line, as one with a connection pool does,
give it the turn method of
QueueingStore as well. A limiter
then waits there before it starts its timeout, so that a long line is not
taken for a slow server. A store that wraps another has to pass turn on to
it for the same reason.