Rate Limiting Explained: Protecting Your API from Abuse

One client in a retry loop can take down an API that serves thousands of happy users. One script guessing passwords can burn through your login endpoint in minutes. Rate limiting explained simply is this: you decide how many requests a client may make in a window of time, and you refuse the rest with a clear answer. It is the cheapest protection an API has, and it doubles as a guard against your own clients’ bugs.

Rate limiting explained: the core idea

A rate limit has three parts: a key that identifies the client, a window of time, and a maximum count. The key is usually an API key, a user ID, or an IP address. The window might be a minute, an hour, or a day. When the count passes the maximum, the API answers 429 Too Many Requests and stops doing work for that client until the window resets. The response should include a Retry-After header so a well-behaved client knows when to try again. The limit itself is a policy decision, not a technical one: how much traffic is normal for this endpoint, and how much is abuse?

Fixed window counting

Fixed window counting is the simplest algorithm. You divide time into blocks, say one minute each, and count requests per key in the current block. At the start of a new block the counter resets to zero. It is easy to build with a cache or a database counter, and easy to explain to anyone who reads your code. Its weakness is the boundary: a client can send the full limit at the end of one window and the full limit again at the start of the next, doubling the burst. For most internal APIs that is acceptable. For a public login endpoint, it is not.

Token bucket algorithm

The token bucket algorithm smooths out bursts. Imagine a bucket that holds a fixed number of tokens and refills at a steady rate, say ten tokens per second. Each request takes one token. If the bucket is empty, the request is refused. A client that has been quiet can spend its saved tokens in a short burst, which is good for real usage patterns, but a sustained flood still hits the refill rate and stops. Token buckets are a good default for public APIs because they forgive bursts while capping the average. They also map neatly onto pricing tiers: a free plan gets a small bucket and a slow refill, a paid plan gets a bigger one.

Sliding window log

A sliding window log is the most precise option. You record the timestamp of every request and count how many fall inside the last N seconds, with the window sliding forward as time passes. There is no boundary burst, because the window never resets all at once. The cost is memory: you store a timestamp per request, which adds up on a busy endpoint. In practice teams use a sliding window counter instead, which keeps a few counters per window and weights them, getting most of the precision for a fraction of the storage. Choose the log when precision matters more than scale, and the counter when scale matters more.

Limits per user, not just per IP

Set limits per user, not just per IP address. A single IP can hide a whole office behind NAT, or a whole botnet behind a proxy. Key the limit to the authenticated user or API key whenever you can, and use the IP only as a second layer for unauthenticated routes like login. Apply different limits to different endpoints: a search endpoint that runs an expensive query gets a tight limit, a static read endpoint gets a loose one. Expensive endpoints deserve tight limits even for paying users, because one heavy query can cost more than a hundred cheap ones. If you are building a REST API with Node.js and Express, add the limiter as middleware on the routes that need it, not as a global blanket that punishes your cheapest endpoints the same as your most expensive ones.

What a good 429 response looks like

A refused request should still be a helpful one. Return status 429, a short message, and headers that say when to retry and what the limit is. Some APIs also include how many requests remain in the current window on every response, so clients can slow down before they hit the wall. Never return a 500 or a timeout for a rate-limited request; that hides the cause and makes clients retry harder, which is the opposite of what you want. Log rate-limit hits with the key and the endpoint, so you can tell a misconfigured client apart from an attack. The first is a support ticket. The second is a block.

Where to store the counters

Counters must live somewhere fast and shared across your servers. An in-memory map works for a single server and falls apart the moment you run two, because each server counts on its own and the limit doubles. Redis is the usual choice: atomic increment operations, keys that expire on their own, and speed that keeps the limiter from becoming a bottleneck. Keep the counter logic separate from your business logic so you can change the algorithm without touching every route. And put the limiter in front of your expensive work. If the rate check runs after you have already parsed a large body and opened a database connection, you have limited nothing.

Choosing numbers that do not annoy users

Start from real traffic, not from a guess. Look at your logs for the busiest legitimate client and set the limit a few times above its peak. Watch the 429 rate for a week. If real users hit the limit, raise it or split it by endpoint. If nobody ever hits it, it may be too loose to matter. Document the limits in your API docs so clients can design around them instead of discovering them in production. Limits that surprise people create support tickets; limits that are published create better clients. When your API runs on your own server, benchmarking your VPS tells you what the hardware can actually sustain, which is a useful ceiling for the numbers you choose.

Rate limiting is one layer, not the whole wall

Rate limiting stops floods. It does not stop a slow, patient attacker who stays under the limit, and it does not replace authentication, validation, or monitoring. Treat it as one layer among several: auth decides who may call, validation decides what they may send, rate limiting decides how often, and logging tells you when the pattern looks wrong. If you are deciding how your API should behave under load, reading REST vs GraphQL helps, because the two styles expose different endpoints to abuse and need different limit strategies. And when the API lives behind a web server, deploying a Node.js app to a VPS with PM2 and Nginx is where you can add a second layer of limiting at the proxy, before requests ever reach your code.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.