LOAD-BEARINGfree
High-level designTier 9 · unit 3 of 10 · 13 min read

Rate limiting: four algorithms

A rate limiter answers a blunt question — "has this caller had enough?" — and it protects you from three distinct villains: deliberate abuse, a buggy client stuck in a retry loop (far more common than attackers, and just as destructive), and the noisy-neighbour problem — one big tenant consuming everything while everyone else starves. There are four standard algorithms; the differences all reduce to how each one defines the edges of "the window."

Fixed window — simple, with a seam

Count requests per calendar minute; over the limit, reject; at :00, reset to zero. Two counters' worth of memory, trivially simple — and there's a seam at the boundary: with a limit of 100/minute, a client sends 100 at 11:59:59 and 100 more at 12:00:01. Both windows are satisfied; you just absorbed 200 requests in two seconds — double your intended rate, fully "within limits." If bursts at that scale genuinely hurt you, the seam matters; for rough abuse-capping, it often doesn't.

Sliding window log — exact, at a price

Store a timestamp for every request; on each new one, count timestamps inside the trailing 60 seconds. Perfectly precise, no boundary games — and memory grows with request volume: at scale, you're storing millions of timestamps per busy key just to say no. The accountant who keeps every receipt: correct, and heavy.

Sliding window counter — the production compromise

Keep just two counters (current and previous fixed window) and blend them by overlap: 30 seconds into the current minute, estimate = current + 0.5 × previous. It approximates the sliding log with two integers instead of a million timestamps, assuming requests spread evenly within windows — slightly wrong, never badly. This is what most production limiters actually run.

Token bucket — the right default for APIs

A bucket holds up to B tokens, refilling at r per second. Each request takes one; empty bucket, rejected. The elegant consequence: an idle client accumulates tokens, so it can legitimately burst up to B before settling to the sustained rate r. That matches how real clients behave — a user opens your app and fires 20 requests at once, then goes quiet. Token bucket says: burst is fine, sustained excess is not. Two dials (B = burst allowance, r = long-run rate) let you tune each independently. When an interviewer says "allow bursts but cap the sustained rate," this is the phrase they're fishing for. (Its sibling, the leaky bucket, drains at a constant rate and smooths bursts out — right when downstream needs steady flow, wrong when bursts are legitimate.)

Distributed limiting, and being polite about no

One server counting in local memory stops working behind a load balancer — ten servers each allowing 100/min is a 1000/min limit nobody chose (Tier 2's synchronized trap, wearing a new hat). The counters move to shared storage — Redis, with the check-and-decrement done atomically (a Lua script), or it's a check-then-act race (Tier 2 again). At serious scale, exact global counting costs a network hop per request, so systems accept approximation: local counters syncing to a global tally periodically, trading a little precision for a lot of latency.

And when you reject: HTTP 429, plus a Retry-After header. That one header converts a client that would hammer you in a tight loop into one that waits politely — it's the difference between a limiter that sheds load and a limiter that generates it.

The trap

Rate limiting only at the front door. The gateway limits external callers — and then one internal batch job, inside the perimeter, floods a downstream service at 50× normal and takes it down. No malice, no attacker: your own cron job. Internal callers need budgets too — every service should defend itself, not just trust the moat.

The tradeoff sheet

Problem solved
One caller — buggy, abusive or merely successful — consuming capacity the rest of your users needed.
Rejected, and why
Autoscaling absorbs the load and pays for the abuse. Quotas enforced at the contract level work socially and not at 3am. Neither protects a shared downstream the way a limiter does.
The trade
Legitimate bursts get rejected, the limit becomes an API contract you cannot tighten later, and distributed counting means the enforced limit is approximate.
First to break at 10x
Counter coordination. At 10x, per-instance counting drifts far from the intended global limit, and a shared counter becomes a hot key of its own.
What it does to the bill
Small directly, large indirectly — it is the mechanism that stops one tenant from setting everyone else’s capacity bill.
Day-2 operations
Emit which limit was hit and by whom on every rejection. A 429 with no attribution turns every support conversation into an investigation.