Rate Limiting Implementation in Your App
How to actually bolt rate limiting onto a real service — the counting middleware, the 429 response, and in-process versus Redis-backed counters.
Rate Limiting Implementation in Your App
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Rate limiting middleware needs a key to count against — per-IP, per-user, or per-API-key — and picking the wrong key is the most common way a rate limiter ends up either useless or actively harmful to legitimate traffic.
- A `429 Too Many Requests` response is only actually useful to a well-behaved client if it comes with a `Retry-After` header telling that client exactly when it's safe to try again.
- A counter stored in your process's own memory works for exactly one running instance and silently stops limiting anything correctly the moment you run two.
- Moving the counter into Redis is what makes rate limiting hold up once you've scaled horizontally, because every instance is now checking and incrementing the same shared number instead of each keeping its own partial, wrong count.
Why “just add rate limiting” is a request-handling problem
An endpoint with no rate limit will do exactly as much work as anyone asks it to. That’s fine when “anyone” is a handful of well-behaved clients, and a real problem the moment one client — a buggy retry loop, a scraper, someone deliberately hammering your login endpoint to guess passwords, or just a popular feature that gets linked somewhere and floods you — starts sending far more requests than you expected. Rate limiting is the practice of capping how many requests a given source gets to make in a given window, and rejecting the rest with a response that tells them to slow down.
The theory of how to count those requests — token bucket, sliding window, and the rest — is its own topic, covered in depth in rate-limiting-algorithms. This lesson is about the part that actually shows up in your codebase: where the counting logic lives, what key it counts against, what you send back when someone goes over, and where that count is actually stored.
Middleware: counting before the real work happens
In almost every framework, a rate limiter is implemented as middleware — a piece of code that runs before your actual route handler, on every matching request. Its job is simple in shape even when the counting underneath gets subtle: look up how many requests this key has made recently, and either let the request through and increment the count, or reject it.
async function rateLimiter(req, res, next) {
const key = req.user ? `user:${req.user.id}` : `ip:${req.ip}`;
const count = await store.increment(key, { windowSeconds: 60 });
if (count > LIMIT_PER_MINUTE) {
res.set('Retry-After', '60');
return res.status(429).json({ error: 'Too many requests' });
}
next();
}
The key is doing more work here than it looks like. Rate limiting per IP address is the simplest option and the right default for unauthenticated endpoints — a login form, a public search box — because there’s no identity yet to key on besides the network address the request came from. But IP addresses are a leaky proxy for “one client”: a large office, a university, or a mobile carrier’s NAT can put thousands of real users behind a handful of IPs, and limiting too aggressively per-IP can throttle all of them for one bad actor’s behavior. Once a request is authenticated, keying by user ID or by API key is almost always better — it follows the actual identity you care about, regardless of how many people happen to share a network address, and it’s what lets you offer different limits to different tiers of API key (a free plan gets 100 requests a minute, a paid plan gets 10,000).
The 429 response is part of the contract, not an afterthought
Returning a bare 429 Too Many Requests status code is the minimum, but a rate limiter that stops there is only half-finished. A well-behaved client — your own frontend, a partner’s integration, a retry library — needs to know when it’s safe to try again, and that’s what the Retry-After header is for:
HTTP/1.1 429 Too Many Requests
Retry-After: 42
Content-Type: application/json
{"error": "Too many requests, retry after 42 seconds"}
Retry-After can be a number of seconds or an HTTP date, and most rate-limiting libraries compute it for you from however much time is left in the current window. Leaving it off doesn’t make the rate limit less effective, but it does mean every client integrating with your API either has to guess a backoff strategy or hardcode one, and a client that guesses wrong tends to retry immediately, hit the limit again, and generate exactly the kind of request storm the limiter was supposed to prevent in the first place.
Where the count actually lives
This is the part that quietly breaks the most rate limiters in production: the counter has to live somewhere, and where it lives determines whether the limit is actually correct once you’re running more than one instance of your service.
An in-process counter — a plain object or map inside your running application, incremented on each request — is the simplest possible implementation, and it’s genuinely correct as long as exactly one instance of your service is handling all the traffic. It needs no extra infrastructure and no network round trip, which makes it attractive for a side project or an early-stage app behind a single server.
It stops being correct the moment you run two instances behind a load balancer. If a client’s requests get split roughly evenly between instance A and instance B, each instance only ever sees about half that client’s actual traffic, and each one independently believes the client is well under the limit — because neither has any way to see what the other counted. A client could be making twice your intended limit and sail through undetected, not because the limiter has a bug exactly, but because it was never designed to see the whole picture.
A shared store — almost always Redis — fixes this by giving every instance the same place to check and increment before deciding. Now the count reflects the client’s total traffic across however many instances you’re running, because none of them were ever counting in isolation. This is also exactly the Redis-backed rate limiter the embedded video above walks through building for a real app that was getting hit by an unexpected traffic spike: the practical motivation isn’t abstract correctness, it’s “my single-instance limiter stopped being enough the moment the app needed more than one server to handle load.” The implementation detail that matters here is atomicity — the increment-and-compare has to happen as one operation (Redis’s INCR plus an expiry, or a small Lua script for anything fancier), so two instances checking at nearly the same instant can’t both read “still under the limit” and both let through a request that pushes the real count over it.
The practical rule mirrors the one from session storage: an in-memory counter is fine for a single instance and a reasonable starting point, but it’s a correctness bug waiting to surface the day you scale horizontally, not a design choice you can defer indefinitely. If you’re reaching for a library instead of hand-rolling this, pick one that supports a Redis store from the start — swapping the backing store later is more work than configuring it correctly the first time.
Check yourself
Why does a rate limiter need to pick a specific key to count against, and what are the common choices?
A rate limiter isn't counting 'requests in general' — it's counting requests from some specific identity, and the identity you choose determines what you're actually protecting against. Per-IP catches an anonymous flood from one source but falls apart behind a shared corporate NAT or a mobile carrier's IP pool; per-user or per-API-key is more precise once a request is authenticated, because it follows an identity rather than a network address that many unrelated people might share.
What should a 429 response actually include, and why does the Retry-After header matter?
A bare 429 status code tells a client it's being throttled but gives it no useful information about what to do next, so a naive client might retry immediately and get throttled again instantly, or give up and surface an error to a user needlessly. The Retry-After header gives a concrete number of seconds (or a timestamp) the client should wait, turning the response into something a well-written client can actually act on correctly.
Why does an in-process counter stop being correct once you run more than one instance of your service?
An in-process counter lives entirely in one running instance's memory, so if a load balancer splits a client's requests across three instances, each instance only sees roughly a third of that client's actual traffic and under-counts accordingly. A client could be making three times your intended limit and every single instance would independently believe it's still under quota, because none of them can see what the others counted.
What does moving the counter to Redis actually fix, mechanically?
Redis becomes the single shared place every instance reads from and writes to before deciding whether to allow a request, so the count reflects the client's total traffic across all instances rather than whatever slice happened to land on one of them. The operation typically needs to be atomic — an increment-and-check done as one step — so that two instances checking the counter at nearly the same moment can't both see 'one under the limit' and both let a request through that pushes the real total over it.
How does this lesson's scope differ from rate-limiting-algorithms in the system-design roadmap?
The system-design lesson is about the algorithm itself — token bucket, leaky bucket, sliding window, and how a distributed rate limiter behaves under contention at scale — as a piece of theory you'd reach for regardless of language or framework. This lesson is the narrower, practical question: given that you've already decided to rate limit something, how do you actually wire a counter into your framework's request pipeline today, store it somewhere real, and return a response a client can act on.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.