Menu

Per-customer rate limits in DRF: what the default cache and Redis actually let through
📰

Per-customer rate limits in DRF: what the default cache and Redis actually let through

DEV Community: redis·Jack Ma·1 day ago
#xP6QgLFX
#devtoredis#devto#dev#cache#time
Long article (1368 words) — no AI summary yet.
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.