Why Do You Need Rate Limiting?

Has your login endpoint ever been hammered by a bot trying thousands of passwords in a single minute? Here is why rate limiting is mandatory, and how to wire it into FastAPI.

Read in Indonesian

You just shipped a login feature or a product search endpoint. At first everything runs calm. Then one night a bot tries thousands of passwords through brute-force, or a bored user refreshes the app a hundred times in a single minute.

Within seconds your server CPU spikes to 100%, database connections are drained, and other users who only wanted to pay get a Server Timeout message. Who would not be stressed?

This problem is a classic nightmare for backend developers. The good news is that one shield stops it better than anything else: Rate Limiting.

Key takeaways

  • Rate Limiting caps how many times one client may call your API within a given window.
  • Token Bucket is flexible and handles short bursts, Leaky Bucket releases requests at a constant rate, while Sliding Window is the most accurate at stopping attacks on minute boundaries.
  • Redis makes an ideal partner because its atomic operations check quota in sub-milliseconds without loading the primary database.
  • Authentication endpoints and heavy-resource endpoints are the first priority for a limiter.

What Is Rate Limiting, Really?

Put simply, Rate Limiting is a mechanism that caps how often one client may call your API endpoint within a set window. That client is identified by IP address, API Key, or User ID.

When a client sends more requests than the limit allows, say 60 requests per minute, the server rejects them and returns the standard HTTP 429 Too Many Requests status code.

To make it concrete, picture your API as a roller coaster that holds 20 people per ride. If 500 people rush the gate at once with no guard organizing the line, the door collapses and the ride stops running entirely. That guard is the rate limiter. Visitors enter in turns according to the per-minute quota, and the rest wait a moment.

Several strategies run behind the scenes, and each one has a different character.

1. Token Bucket

The Token Bucket algorithm works like a bucket that is refilled with tokens at a steady rate, for example 1 token per second. Every incoming request must take 1 token. When the bucket is empty, requests are rejected until fresh tokens arrive.

The upside: this algorithm is very flexible and can absorb short traffic bursts (burst).

2. Leaky Bucket

The opposite of that, this one is a bucket with a hole in the bottom. Incoming requests collect inside the bucket, then leave through the hole at a constant speed. If requests arrive faster than the hole drains them, the excess spills out and gets dropped.

3. Sliding Window Log / Counter

This one counts requests inside a rolling window, for example the last 60 seconds measured from right now. Its accuracy is very good at stopping attacks that deliberately wait at the minute boundary (boundary reset attack).

Why Is Redis the Ideal Partner for a Rate Limiter?

Storing request counts in a relational database like PostgreSQL actually adds load to that database. Just to check one quota number, it has to read and write millions of rows. Wasteful, right? 😅

This is where Redis earns its place. Because it runs in memory (RAM) and supports atomic operations like INCR and EXPIRE, Redis checks request quota in sub-milliseconds without disturbing the primary database.

A Simple Implementation in FastAPI

Here is a Redis-based rate limiter in Python with FastAPI:

PYTHON
import redis.asyncio as redis
from fastapi import FastAPI, HTTPException, Request, status

app = FastAPI()
r = redis.from_url("redis://localhost:6379", encoding="utf-8", decode_responses=True)

RATE_LIMIT = 5        # Maksimal 5 request
WINDOW_SECONDS = 60   # Per 60 detik

@app.middleware("http")
async def rate_limit_middleware(request: Request, call_next):
    client_ip = request.client.host
    key = f"rate_limit:{client_ip}"

    # INCR is atomic, so it is safe to call from many workers at once
    current = await r.incr(key)

    # Set the TTL only on the first request. If you set it on every request,
    # the window keeps resetting and the limit never runs out.
    if current == 1:
        await r.expire(key, WINDOW_SECONDS)

    if current > RATE_LIMIT:
        retry_after = await r.ttl(key)
        raise HTTPException(
            status_code=status.HTTP_429_TOO_MANY_REQUESTS,
            detail="Terlalu banyak request! Silakan coba lagi sebentar ya.",
            headers={"Retry-After": str(max(retry_after, 0))},
        )

    return await call_next(request)

@app.get("/api/data-publik")
async def get_data():
    return {"message": "Ini data aman yang terlindungi oleh Rate Limiter!"}

Now look at the order of INCR and then EXPIRE above. This is the simplest fixed window example, and it stays atomic because you only read what INCR returns. If you fetch the value first with GET, check it in your code, then INCR, the sequence is no longer atomic, and two concurrent requests can slip past the limit.

When Is Rate Limiting Mandatory, and When Should You Skip It?

Rate limiting is cheap and easy to install. That does not mean every endpoint needs a tight cap.

  • Install it on authentication endpoints like /login, /register, and /forgot-password, the favorite door for brute-force, plus search or export endpoints that eat heavy resources.
  • Leave room on public endpoints that only read light data, like article pages or feeds. A cap that is too tight there blocks real users, especially when they sit behind one office or campus NAT.
  • Send informative headers. Include Retry-After, X-RateLimit-Limit, and X-RateLimit-Remaining so the frontend knows when to retry without guessing.

If your scale is enterprise, application-level rate limiting can be paired with DNS/CDN protection like Cloudflare or AWS WAF.

Building a fast API is fun, but keeping it standing when thousands of wild requests hit is far more crucial. Installing rate limiting from day one is a cheap step that saves your server from downtime it never needed.

Happy rate limiting, and may your server sleep soundly tonight. 👋

Inva

Writer

Get better at shipping code.

Join the other developers getting tech updates, tips, and relaxed tutorials in their inbox every week.

Stay in the loop

Tech updates, coding tips, and relaxed insights straight to your inbox every week.

No spam. Unsubscribe anytime.

Read next

See all

For developers who learn by struggling

© 2026 Inva.dev.