Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 39 ยท API Security

Rate Limiting and Abuse Protection

Protect your API from abuse and overload: fixed window, sliding window and token bucket algorithms, 429 responses, rate limit headers and login throttling.

Without limits, one buggy script or one attacker can use all of your server's capacity, run up your cloud bill, or guess passwords by the million. Rate limiting caps how many requests a caller may make in a period of time. It protects availability, keeps costs predictable, makes brute-force attacks impractical, and is the basis for pricing tiers ("1,000 calls per hour on the free plan").

Rate limiting is one layer of abuse protection. This lesson covers the three main algorithms, how to communicate limits to clients with 429 Too Many Requests, and what else you should limit. Every demo uses a simulated clock so results are identical on each run.

What to limit by

API key or user Fairest, needs auth first.
IP address For anonymous routes.
Route Tighter on login and search.
Plan Free versus paid limits.

Combine several limits for best protection.

1 / 2

Fixed window counter

Count requests per caller per window (for example per minute). When the count passes the limit, reject until the next window. It needs one counter per caller and is trivial to implement in Redis with INCR and EXPIRE.

Its weakness is the boundary burst: with a limit of 100 per minute, a caller can send 100 requests at 12:00:59 and 100 more at 12:01:00, so 200 requests in two seconds are all allowed.

Sliding window

Instead of fixed minutes, look back exactly one window from now. A sliding window log stores each request's timestamp and counts those in the last window: exact, but memory grows with the limit. A sliding window counter blends the previous and current fixed windows (weighting the previous one by how much of it still overlaps) for a close approximation with two counters.

Token bucket

Picture a bucket that holds up to capacity tokens and refills at rate tokens per second. Each request removes one token. If the bucket is empty, the request is rejected. This allows short bursts (up to the capacity) while enforcing an average rate, which matches how real clients behave. It is the algorithm behind many cloud APIs.

Compare the fixed window and the token bucket under the boundary burst:

Node.js Fixed window versus token bucket

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The fixed window let all ten requests through because they straddled a boundary. The token bucket allowed the first five (the burst capacity) and denied the rest, because only half a token had refilled by t=10.

Telling the client: 429 and headers

When you reject a request, use 429 Too Many Requests and say when to try again:

HTTP
HTTP/1.1 429 Too Many Requests
Retry-After: 30
Content-Type: application/problem+json

{"type":"about:blank","title":"Too Many Requests","status":429,"detail":"Limit of 100 requests per minute exceeded."}

Retry-After holds seconds to wait (or an HTTP date). On every response, successful or not, it helps to tell clients where they stand. The common convention uses X-RateLimit-Limit, X-RateLimit-Remaining and X-RateLimit-Reset. An IETF draft standardizes RateLimit-Limit, RateLimit-Remaining and RateLimit-Reset; pick one family and document it.

Good clients read these headers, slow down, and use exponential backoff with jitter after a 429 or a 503: wait 1 s, 2 s, 4 s, ... plus a random amount, so thousands of clients do not retry at the same instant.

A rate-limited API

This server limits each API key to 3 requests per 10-second window and shows the headers. The clock is simulated through a variable so the output is stable.

Node.js 429, Retry-After and limit headers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Each key has its own budget, so one noisy client does not hurt the others.

What else to limit

Risk Defence
Password guessing Strict limits on login by account and IP, growing delays, optional lockout with care (lockouts can be used to harass users)
Credential stuffing Per-IP and per-device limits, breached-password checks, MFA
Expensive endpoints Lower limits for search, export and report generation
Scraping Quotas, pagination limits, detection of enumeration patterns
Large payloads Body size limits (Input Validation)
Slow clients Request and idle timeouts
Resource exhaustion Maximum page size, query cost limits, concurrency limits

Counters must be shared between servers (Redis is the usual choice), or each instance enforces its own limit and your effective limit multiplies. Enforce limits as early as possible: at the CDN or gateway first, then in the application for per-user rules. If you sit behind a proxy, read the client IP from a trusted forwarded header, or every user will look like the proxy.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which status code means "slow down"?

    429 Too Many Requests is the standard response to rate limit violations, usually with Retry-After.
  2. 2. What is the weakness of a fixed window counter?

    Requests at the end of one window and the start of the next both count against fresh windows.
  3. 3. What does a token bucket allow that a strict per-second limit does not?

    Unused tokens accumulate up to the capacity, so occasional bursts are fine while the long-term rate is capped.
  4. 4. Why should rate limit counters live in shared storage such as Redis?

    Each server would otherwise count separately, letting a client send the limit to every server.
  5. 5. What should a client do after receiving 429 with Retry-After: 30?

    Retry-After tells the client when it is allowed to try again. Backoff with jitter avoids synchronized retries.

Summary

  • Rate limit by key, user, IP and route; tighten limits on login and expensive endpoints.
  • Fixed windows are simple but allow boundary bursts; sliding windows are exact; token buckets allow controlled bursts.
  • Reject with 429 and Retry-After, and expose RateLimit-* headers so clients can adapt.
  • Clients should back off exponentially with jitter.
  • Keep counters in shared storage, enforce at the edge first, and combine with size, time and concurrency limits.

Next: CORS and Security Headers