API Rate Limiting: From Token Bucket to Per-User Quotas

Last November, a forgotten retry loop on the server of an integration partner using our vehicle tracking API generated 3.2 million requests overnight. When I woke up at six in the morning, the database connection pool was exhausted, and the twelve other customers using the same API had taken their share of the slowdown. The painful part: we had rate limiting infrastructure. But we had set the limit with "come on, nobody's going to send that many requests" logic, at something like ten thousand per minute, a value that looked reasonable on paper. That night we learned that rate limiting isn't an on/off switch; it's a design problem you have to think through layer by layer.

In this post I'll describe the approach we settled on after that night. Not theory, but the way it runs in production right now, in systems handling an average of 40 million requests a day.

Why the fixed window misled us

Our first setup was a classic fixed window counter: a per-minute counter for each user, a 429 once the limit is exceeded. Simple, solved with a single INCR in Redis, easy to understand. The problem is that there's an ugly gap at the window boundaries: if a client sends 10,000 requests in the last second of a minute and another 10,000 in the first second of the next minute, you're hit with 20,000 requests in two seconds, and technically no limit has been violated. A large part of the load we saw that night came from exactly this pattern; the retry loop had happened to sync up with the window transitions.

That's why we moved to a token bucket. I like the logic because it respects real-world usage patterns: every client has a bucket, the bucket fills at a constant rate (say, 50 tokens per second), and every request spends one token. A client can make a short burst — up to as many tokens as have accumulated in the bucket — but it can't sustain a high pace. This is critical for mobile apps; when a user opens the app, the screen fires off several requests at once, and then there's silence for minutes. A fixed window punishes this pattern; a token bucket tolerates it.

Fixed window counter versus token bucket: the fixed window lets 20,000 requests through at the boundary, while the token bucket tolerates a short burst.
Instead of a counter that can let 20,000 requests through at the boundary, a bucket that respects bursts.

One limit isn't enough: the quota pyramid

Real maturity came with accepting that a single limit is never enough. Today we stack the limits in our APIs like a pyramid:

  • Per-second limit per IP: the first line of defense against bots and misconfigured clients, running at the edge without ever touching the application server.
  • Token bucket per API key: this is where the real workload control happens; it allows bursts and cuts off sustained abuse.
  • Daily and monthly quota per user/tenant: the technical counterpart of the commercial contract; a "1 million requests per month" plan is managed here.
  • Per-endpoint weighting: an endpoint that generates a report and a simple status query shouldn't spend the same token; expensive endpoints eat 5-10 tokens.

Endpoint weighting is usually skipped, but for us it was a game changer. A query that generates a fleet report does roughly two hundred times as much work in the database as a single vehicle location query. Keeping the two under the same limit is unfair to cheap requests and an open invitation to expensive ones.

A four-layer quota pyramid: IP limit, token bucket per API key, tenant quota, and endpoint weighting.
A single limit is never enough; limits are stacked like a pyramid.

Returning a 429 isn't enough; you have to return it properly

Just as important as the limit itself is what you tell the client that hits it. Returning a bare 429 status and going silent leaves the developer on the other side flying blind; since they don't know when to try again, they either retry aggressively (making the problem worse) or abandon the integration (and you lose the customer).

Our standard: on every response, headers for the remaining token count, the limit, and the reset time; and on a 429 response, always a Retry-After. When the other side respects that header, retry storms die down on their own. After that November night, we sat down with our integration partner and had them add exponential backoff and jitter to their retry logic; when we replayed the same scenario in staging, 3.2 million requests dropped to 40 thousand. The change on the code side was a grand total of twenty lines.

The 429 flow: response headers, Retry-After, exponential backoff and jitter on the client, and finally the retry storm dying down.
The change on the code side was a grand total of twenty lines.

One more detail: put a human-readable explanation in the 429 body. The difference between a body that says "Rate limit exceeded" and one that says "You've reached your per-minute limit; you can try again in 42 seconds or upgrade your plan from the dashboard" is measured in the number of tickets landing in your support inbox. After we switched to the second format, emails titled "Your API is broken, it's throwing errors" all but disappeared; developers started reading the error not as a problem but as a predictable response from the system.

There's also this: before you tell the client about a limit breach, tell yourself. Rate limit violations are a first-class metric for us: which key, which endpoint, what time. Because more often than not, that chart doesn't show an attack; it shows a customer growing — and bringing a growing customer an upgrade offer before showing them a limit error is a much sweeter conversation for both sides.

The pain of keeping counters in a distributed system

Rate limiting on a single server is as easy as a student assignment. On an API running on fifteen instances across three regions, though, the question "how many requests did this user send?" turns into a philosophical matter. With a central Redis you get full consistency but add a network round trip to every request; with local counters you get speed but your limits inflate by the number of instances.

Our balance is a hybrid: per-second limits are local and approximate on each instance (if they overflow a little, it's not the end of the world), while daily and monthly quotas are exact in Redis. We accept an overflow margin of 10 percent of the limit for approximate counters, and we state this honestly in our customer contracts as well. Adding 2 ms to every request for the sake of perfect consistency means killing the very performance you're trying to protect with the limit itself.

Meanwhile, here's the new reality of 2026: a growing share of your API consumers are no longer software written by humans but agents. AI agents connecting to our API through our MCP server arrive with far more variable patterns than classic clients; an agent can fire a hundred requests in ten seconds for one task and then vanish for hours. The token bucket has become even more valuable in this world, because bursty usage is now the norm, not the exception. We defined a separate key class and separate bucket sizes for agent traffic; lumping human users and agents together makes both sides unhappy.

One last warning, this time about your own internal services. Thinking of rate limiting only as a shield against the outside world is a common mistake; one of the two most expensive outages we've ever had was a buggy release of our own mobile app hammering the API until it went down. We give internal clients keys too, and they have buckets as well. Trusting your own code is nice; but an old app version released a year ago that you can't remove from the store is also "your own code," and you no longer have any control over what that version does.

Trying to bolt on rate limiting after the fact is like adding a fire escape to a crowded building after it's built; possible, but expensive and ugly. If you're designing a new API, think about quotas from the first sprint. If you don't know where to start with your existing API, let's talk over a cup of tea; with a half-day analysis we can roughly work out which layers fit your traffic.

Since the phone call that woke me up that night, not one endpoint in our APIs has gone without a quota. Not a single one.