The in-memory per-customer limiter for Hono keeps each customer's count in a Map. Each process has its own map, so three instances can let a customer through up to three times their limit. I wanted to see what happens with Django REST framework's built-in throttles, which look like they handle this for you.
With four workers and eight requests in flight, Django's default cache let 39 of 40 requests through a 10/min limit. With Redis, exactly 10 got through when I sent them one at a time, and 16 to 19 when I sent eight at a time. A per-customer quota middleware that runs in front of DRF needs a shared counter for the same reason. In an LLM API, the same race shows up when two requests reserve tokens at once, for example when counting LiteLLM tokens per customer against a monthly quota.
The setup
One DRF view behind TokenAuthentication, with UserRateThrottle set to 10/min. One authenticated DRF user with one token stands in for a customer. 40 requests per run, cache cleared before each run. (If several users share a customer account, key the throttle on the account ID, not request.user.pk, so they share one limit.) Python 3.13, Django 6.1.3, DRF 3.18.3, gunicorn 27.0.1, and Redis 7.4 in a local container for the shared-cache runs.
REST_FRAMEWORK = { "DEFAULT_AUTHENTICATION_CLASSES": ["rest_framework.authentication.TokenAuthentication"], "DEFAULT_PERMISSION_CLASSES": ["rest_framework.permissions.IsAuthenticated"], "DEFAULT_THROTTLE_CLASSES": ["rest_framework.throttling.UserRateThrottle"], "DEFAULT_THROTTLE_RATES": {"user": "10/min"}, } Each worker keeps its own request history
If you don't configure CACHES, Django uses LocMemCache, which lives inside each worker process. DRF's throttle stores its request history there.
| Workers | Requests sent | Let through (limit 10) |
|---|---|---|
| 1, one at a time | 40 | 10 |
| 4, one at a time | 40 | 23 to 28 |
| 4, 8 at a time | 40 | 39 |
With one worker and one request at a time, exactly 10 got through. With four, each worker keeps its own history and allows its own 10. Sending one at a time, I got 28, 27, 25 and 23 through across four runs, depending on how gunicorn spread the requests: in three runs, two workers admitted 10 each and the other two saw only a few. In the fourth, the workers let through 3, 6, 9 and 10. Eight at a time, all four workers got traffic, and 39 of 40 got through.
So with four sync workers, LocMemCache gives each worker its own 10-request allowance, and a customer whose requests reach all four can get up to 40 through in a minute. That's the Hono Map problem again, just harder to spot because the setting says 10/min.
Concurrent writes to a shared Redis cache
Pointing CACHES at Redis fixes the per-process split:
CACHES = { "default": { "BACKEND": "django.core.cache.backends.redis.RedisCache", "LOCATION": "redis://127.0.0.1:6379", } } One request at a time against four workers, exactly 10 got through. With 8 concurrent requests, 16 to 19 got through across five runs, and 15 with 16 concurrent.
The reason is in SimpleRateThrottle.allow_request. It reads the customer's history with cache.get, checks its length, adds a timestamp, and writes the whole list back with cache.set. Two requests that read the history before either writes back both see room for one more, and the later write overwrites the earlier one.
For a limit that exists to protect your servers, being off by a few requests may be fine. For a limit printed on a pricing page, I'd rather count in one step.
Counting requests with Redis INCR
Redis can increment a counter and return the new value in one command, so there's no gap between reading and writing. The throttle below creates each minute's counter with cache.add(), then decides from the value cache.incr() returns. A fixed one-minute window per customer:
import time from django.core.cache import cache from rest_framework.throttling import BaseThrottle class CustomerMinuteThrottle(BaseThrottle): limit = 10 def allow_request(self, request, view): if not request.user.is_authenticated: return True window = int(time.time() // 60) key = f"rl:{request.user.pk}:{window}" cache.add(key, 0, timeout=120) # create the counter if it isn't there yet count = cache.incr(key) # one INCR, no read-then-write gap self.retry_after = 60 - time.time() % 60 return count <= self.limit def wait(self): return self.retry_after Four workers, 8 concurrent requests, three runs: exactly 10 got through each time. With 16 concurrent, also 10.
What this gives up: a fixed window lets a customer send 10 at the end of one minute and 10 more at the start of the next, which DRF's sliding history doesn't. If that matters, a token bucket in a small Lua script keeps the single atomic step and smooths out the edge. The other thing to know is that cache.incr is only as safe as the backend. Django's Redis backend checks that the key exists and then sends one INCR, and Memcached also increments atomically. LocMemCache keeps a separate counter per process, and the database backend reads and writes in separate steps.
Token quotas have the same problem, with bigger numbers
For a per-customer token quota, you have to decide whether to admit a request before you know how many tokens the model will generate. In that LiteLLM example, the middleware reserves room for the largest input the API accepts plus an output cap such as max_tokens, then settles against actual usage after the call. Concurrent requests have to reserve against the same shared balance, and how they take that reservation decides whether the quota holds.
I simulated it directly against Redis: a quota of 10,000 tokens, each request reserving 3,000, eight requests arriving together.
# Check, then add: what a "do they have room?" helper usually does used = int(r.get(key) or 0) if used + RESERVE > QUOTA: refuse() # ... the model call starts here ... r.incrby(key, RESERVE) # Reserve first, then check used = r.incrby(key, RESERVE) if used > QUOTA: r.decrby(key, RESERVE) # give the reservation back refuse() In all three runs, with a 10 ms pause between the check and the write, check-then-add let all eight requests through and left 24,000 tokens reserved against a 10,000-token quota. Reserve-first admitted three and left 9,000 reserved. Two requests can both pass the check if they read the same balance before either records its reservation.
Reserve-first keeps admitted reservations within the quota. A call that uses more than it reserved can still take the customer over, which is why the cap on output matters. A request that would fit can also be refused while a failed reservation is still on the counter. If that matters, a short Redis Lua script can check for room and reserve in one step.
What I'd check in an existing API
- Look at
CACHES. If there isn't one, or it'sLocMemCache, every worker has its own copy of each customer's limit. Count how many workers, across all your servers, can receive the same customer's traffic. - If the counter is shared, find the line that changes it. A
getfollowed by asetdrifts under concurrency. Check that the backend really increments in one step: getting the new value back doesn't tell you that on its own. - For token or credit quotas, take the reservation before the expensive call, in a way that can't race with the check, and settle against the actual count afterwards.
These numbers are from one machine with the versions above. Worker counts, cache backends and DRF releases all change the details, so run the same 40-request burst against your own setup.
