Skip to content

Rate limiting

Introduction

Set a cap such as "at most N requests per minute" for each user (or IP), and reject anything beyond it.

✗ Without it
Legitimate user
Crawler/attacker: thousands of requests per second
Server resources exhausted
Nobody can get through
✓ With it
Legitimate user
Crawler/attacker: thousands of requests per second
Rate limiter blocks excess requests
Legitimate users are unaffected

Why use it

  • Abuse prevention: without limits on login, an attacker can try thousands of passwords per second.
  • Resource protection: metered endpoints such as SMS or AI calls can rack up a huge bill overnight if scraped.
  • Fairness: stop one large client from consuming all capacity.

Common algorithms (overview only)

Algorithm Traits
Fixed window At most N per minute; simplest, but can allow a double burst at window boundaries
Sliding window Smoother; a bit more complex to implement
Token bucket Allows short bursts while keeping a long-term rate; the most common choice

How to do it

  1. Choose the rate-limit dimension: by IP, by user, or by API key.
  2. Give sensitive endpoints (login, OTP, payment) stricter limits.
  3. On exceed, return 429 Too Many Requests with a Retry-After header so clients know when to retry.
  4. With multiple servers, keep counters in shared storage (e.g. Redis), or each instance counts on its own.

Component comparison

ASP.NET Core built-in rate-limiting middleware

Built into the frameworkOpsLow

Rate limiting built into .NET: declare in the program "each user may make at most N requests per minute", and requests beyond that are rejected.

Best for.NET APIs on a single machine or a few instances that want simple abuse protection.

Avoid whenThere are multiple servers and you need accurate global limits, or you must withstand high-volume attacks.

Pros

Built into the framework, so you do not install a separate limiter

Rules can be set per user or per endpoint

Rules live in code and move with the version

Cons

Counts sit in one machine's memory, so each server counts on its own

Requests are blocked only after they enter the program, so heavy traffic still hurts

It applies only to projects on the .NET side

slowapi

Self-hosted open sourceOpen sourceOpsLow

A rate-limiting library for Python FastAPI/Starlette apps: add one decorator line to an endpoint to limit its request frequency.

Best forAPIs written with FastAPI that need abuse protection quickly.

Avoid whenThe project is not Python FastAPI/Starlette, or you need to block heavy traffic at the entry layer.

Pros

Simple to add, and each endpoint can have its own limit

Counts can use the client IP or a custom identifier

Counts can live in Redis so multiple instances share them

Cons

It fits Python projects such as FastAPI

Counts are in memory by default, so multiple instances need Redis

Requests are blocked only after they reach the app, so heavy traffic still gets through

Official docs ↗

Django rate limiting (DRF Throttling / django-ratelimit)

Self-hosted open sourceOpen sourceOpsLow

Rate limiting for Django projects: APIs use Django REST Framework's built-in throttling, and plain views use the django-ratelimit decorator.

Best forBack offices or APIs built with Django that want simple abuse protection.

Avoid whenThe project is not Django, or you need to block heavy traffic at the entry layer.

Pros

DRF throttling is built into the framework; per-user or per-IP rates take just a few lines of config

Different quotas can be set per logged-in user and per endpoint

Counts can live in the Django cache (such as Redis), shared across multiple instances

Cons

Uses the local-memory cache by default; multi-instance deployments must switch to a shared cache, otherwise each counts on its own

Requests are blocked only after they enter Django, so it cannot stop high-volume attacks

Only for Django projects

Official docs ↗

Custom Redis counters

Hand-rolledOpen sourceOpsMedium

Use Redis to record how many requests each user makes within a time window, and write your own code to decide whether the limit is exceeded.

Best forMulti-instance deployments where limiting rules are tightly tied to the business (such as different quotas per membership tier).

Avoid whenA single server with simple needs, where the framework's built-in option is easier.

Pros

Every machine shares one count, so the global limit is accurate

Rules can be customized by user, plan, or endpoint

Counter commands are fast, and Redis itself performs well

Cons

You write and test the limiting algorithm, and a mistake lets traffic through

You maintain Redis, and must decide whether a failure allows or denies

Every request adds one network round trip to Redis

rate-limiter-flexible

Open-source libraryOpen sourceOpsLow

A framework-agnostic rate limiting library supporting in-memory and Redis backends, with shared counters across multiple instances.

Best forMulti-instance Node services that need per-user or per-endpoint rate limiting.

Avoid whenThere is only a single instance and the rate limiting needs are simple (use the framework's built-in plugin).

Pros

Redis support keeps counts consistent across multiple instances

Configurable algorithms and granularity (per IP, user, or endpoint)

Not tied to any specific framework

Cons

You must write the middleware that plugs it into your framework

You must decide the fallback strategy when Redis fails

Many configuration options

Official docs ↗

governor

Open-source libraryOpen sourceOpsLow

A GCRA rate limiter for Rust, suited to per-key limits inside one process.

Best forRust services that need rate limiting per user / IP / endpoint.

Avoid whenRate limiting is already implemented uniformly at the gateway layer.

Pros

A mature, fast implementation

Fine-grained per-key limiting

Not tied to a specific web framework

Cons

Counts inside one process by default and is not shared across instances

A distributed setup needs your own store, or the gateway

The response for limited requests must be unified by you

Official docs ↗

x/time/rate

Open-source libraryOpen sourceOpsLow

The token-bucket limiter from Go's official extended libraries, for use within a single process.

Best forSingle-instance services, or setups where the gateway does global limiting and the application layer only provides a safety net.

Avoid whenMultiple instances that need globally consistent rate limiting (use a Redis-based solution).

Pros

Maintained in the official extension library, with almost no extra dependencies

The API is simple: wrap it in middleware and it is ready

The implementation is light, and single-process limiting performs well

Cons

Counts stay in this process and are not shared across instances

Per-IP or per-user limits require you to clean up expired entries

There is no ready-made backend for counts shared across instances

Official docs ↗

Checklist

  • A global default limit is in place
  • Login, signup, OTP, and payment endpoints have stricter limits
  • Exceeding the limit returns 429 + Retry-After
  • Multi-instance deployments store counters in shared storage