How to protect your APIs from traffic spikes, bad actors, and the noisy neighbor problem

By The Coding Gopher

Published: 2026-06-27

URL: https://thecodinggopher.substack.com/p/rate-limiting-explained


At its core, rate limiting is a defensive mechanism used to control the amount of network traffic sent to or from a system. Think of it as a bouncer at a crowded club: the bouncer ensures that only a certain number of people enter at a time so the venue doesn’t violate fire codes or run out of drinks.Designing a rate limiter | Iván OvejeroA rate limiter is a security and traffic management tool that controls the number of requests a user or service can make to an API or server within a specific timeframe. It acts as a gatekeeper to prevent server overload, mitigate Denial of Service (DoS) attacks, and manage costs by rejecting excess traffic.

In software, it restricts how many requests a user, device, or IP address can make to an API or server within a specific timeframe. When a user exceeds their allowed limit, the server rejects the request, typically returning an HTTP 429 Too Many Requests status code.

Subscribe now

Why Do Systems Need Rate Limiting?Passthrough with priority and limits in rate limiting is an advanced traffic management strategy that categorizes incoming requests to determine which should be immediately admitted (passed through), which should be subjected to strict, lower limits, and which should be blocked. It moves beyond simple "allow/block" rules to ensure high-value traffic (like paying customers or critical APIs) is prioritized over lower-value traffic (like bots or free-tier users) during high load.

-

Preventing DDoS Attacks: It stops malicious actors from intentionally flooding your servers with millions of fake requests designed to crash your application.

-

Resource Management & Cost Control: Cloud infrastructure costs money. Rate limiting prevents a single user (or a runaway script) from accidentally consuming 100% of your CPU or database I/O, ensuring you don’t wake up to a massive server bill.

-

Fairness (The Noisy Neighbor Problem): If you are running a multi-tenant application, you want to ensure that one massive enterprise customer doesn’t hog all the bandwidth, slowing down the experience for your smaller customers.

-

Preventing Brute Force: It stops hackers from rapidly guessing thousands of passwords per minute on a login page.


How It Works: The Algorithms

There are a few standard algorithms engineers use to enforce these limits.

1. The Token Bucket

This is the most popular algorithm (used by companies like Stripe and Amazon).Token Bucket Algorithm (Rate Limiting) | by Suraj Shende | Medium

Imagine a bucket that holds a maximum of N tokens. Every time you make an API request, you must take one token out of the bucket. Meanwhile, the server automatically drops new tokens into the bucket at a constant rate (e.g., 5 tokens per second).

-

If the bucket has tokens, your request goes through.

-

If you make requests faster than the bucket refills, it empties out. Once empty, your requests are blocked until a new token drops in.

-

Advantage: It allows for brief spikes or “bursts” of traffic (if the bucket is full, you can fire off 10 requests instantly).

A Guide to Rate Limiting Strategies - ByteByteGo Newsletter

𝐋𝐞𝐚𝐫𝐧 𝐭𝐨 𝐛𝐮𝐢𝐥𝐝 𝐆𝐢𝐭, 𝐃𝐨𝐜𝐤𝐞𝐫, 𝐑𝐞𝐝𝐢𝐬, 𝐇𝐓𝐓𝐏 𝐬𝐞𝐫𝐯𝐞𝐫𝐬, 𝐚𝐧𝐝 𝐜𝐨𝐦𝐩𝐢𝐥𝐞𝐫𝐬, 𝐟𝐫𝐨𝐦 𝐬𝐜𝐫𝐚𝐭𝐜𝐡. Get 40% OFF CodeCrafters: https://app.codecrafters.io/join?via=the-coding-gopher

This Substack is reader-supported. To receive new posts and support my work, consider becoming a free or paid subscriber.

2. The Leaky Bucket

Imagine a bucket with a hole in the bottom. Requests are poured into the top of the bucket and “leak” out the bottom to be processed by the server at a strict, constant rate.The Leaky Bucket rate limiter | Mike Perham

-

If a massive wave of requests pours in, they just sit in the bucket waiting their turn.

-

If the bucket fills up entirely, any new requests poured on top simply spill over the sides and are discarded.

-

Advantage: It provides an incredibly smooth, predictable load on your database, but it completely punishes sudden bursts of traffic.

A Guide to Rate Limiting Strategies - ByteByteGo Newsletter

3. Fixed Window Counter

The system tracks requests based on rigid time windows (e.g., 12:00:00 to 12:01:00). If your limit is 100 requests per minute, the server keeps a counter. At exactly 12:01:00, the counter resets to zero.A Guide to Rate Limiting Strategies - ByteByteGo Newsletter

-

Advantage: Very easy to implement.

-

Disadvantage: The “Boundary Problem.” A user could send 100 requests at 12:00:59, and another 100 requests at 12:01:01. The server just processed 200 requests in two seconds, completely defeating the purpose of the limit.

Creating a simple rate limiter | IPinfo.io

This Substack is reader-supported. To receive new posts and support my work, consider becoming a free or paid subscriber.

Thanks for reading! This post is public so feel free to share it.

Share