Skip to content
Velloreth
Cybersecurity 6 min read

Rate Limiting vs Throttling: What the Difference Is

Rate limiting rejects excess requests; throttling slows them down. Learn the difference, the algorithms, and how to respond well.

By

The difference

Rate limiting sets a maximum number of requests in a window and rejects anything over it. The client receives a 429 and must wait.

Throttling slows requests down. The request is not rejected; it is delayed, or the client’s quota is reduced gradually, or the response is served at a lower priority.

In practice the two are used together. A public API usually rate limits the request count and throttles the bandwidth or the expensive operations. The terms are often used interchangeably, and the important thing is the behaviour rather than the label.

The algorithms

Fixed window counts requests in a fixed period, such as a minute, and resets at the boundary. It is the simplest to implement and the cheapest to store.

Limit: 100 requests per minute
Window: 12:00:00 to 12:00:59

The problem: a client can send 100 requests at 12:00:59 and 100 more at 12:01:00, which is 200 requests in two seconds. The boundary allows a burst of twice the limit.

Sliding window counts requests in the last N seconds, evaluated at each request. It removes the boundary burst and costs more to store, because it needs a timestamp per request or a set of counters.

Token bucket holds a bucket of tokens that refills at a fixed rate. Each request takes a token, and a request with no token is rejected. The bucket size is the maximum burst.

Bucket size: 100 tokens
Refill rate: 10 tokens per second

This is the most common choice because it allows a legitimate burst up to the bucket size while enforcing an average rate. A client that has been idle can burst; a client that hammers is limited to the refill rate.

Leaky bucket queues requests and processes them at a fixed rate. It smooths the output rather than rejecting the input, which is a throttling behaviour.

Implementing a token bucket

async function allowRequest(key, capacity, refillPerSecond) {
  const now = Date.now();
  const script = `
    local key = KEYS[1]
    local now = tonumber(ARGV[1])
    local capacity = tonumber(ARGV[2])
    local refill = tonumber(ARGV[3])

    local bucket = redis.call('HMGET', key, 'tokens', 'ts')
    local tokens = tonumber(bucket[1]) or capacity
    local ts = tonumber(bucket[2]) or now

    local elapsed = (now - ts) / 1000
    tokens = math.min(capacity, tokens + elapsed * refill)

    local allowed = 0
    if tokens >= 1 then
      tokens = tokens - 1
      allowed = 1
    end

    redis.call('HMSET', key, 'tokens', tokens, 'ts', now)
    redis.call('EXPIRE', key, 3600)
    return { allowed, math.floor(tokens) }
  `;
  const [allowed, remaining] = await redis.eval(script, 1, key, now, capacity, refillPerSecond);
  return { allowed: allowed === 1, remaining };
}

The Lua script makes the operation atomic. A read-then-write in application code has a race, where two concurrent requests both read the same token count and both take the last token.

The key is what the limit applies to. It might be ratelimit:user:123, ratelimit:ip:203.0.113.5, or ratelimit:apikey:abc. The choice determines the fairness.

Choosing the key

By IP is the only option before authentication, and it protects against an unauthenticated flood. It is unfair behind a shared proxy, where many users share one address, and it is easy to evade with a pool of addresses.

By user or API key is fairer and is the right key for an authenticated API. It requires the request to be authenticated first, so it does not protect the login endpoint.

By endpoint is useful when one operation is much more expensive than the others. A search that scans a large table should have a tighter limit than a lookup by ID.

A combination is the practical answer: a strict IP limit on the unauthenticated endpoints, and a per-user limit on the authenticated ones.

Responding to a limited client

Return 429 Too Many Requests. It is the correct status, and a client library can recognise it.

Include Retry-After. The value is the number of seconds to wait, and it lets a client back off correctly rather than retrying immediately.

HTTP/1.1 429 Too Many Requests
Retry-After: 30
RateLimit-Limit: 100
RateLimit-Remaining: 0
RateLimit-Reset: 30

Include the limit and the remaining count. A well-behaved client paces itself from these headers and rarely hits the limit at all.

Do not return 403. A 403 says the request is forbidden, which is a different meaning and confuses a client. A 429 says the request is fine but too frequent.

Choosing a limit

The number is the hard part, and it should come from a measurement rather than a guess. The first input is the legitimate usage: a client that makes ten requests a minute should not be limited to five. The second is the capacity of the system: a limit that is higher than the system can serve does not protect it. The third is the cost of a false positive: a limit that breaks a real integration causes an outage, which is worse than the abuse it prevents. The practical approach is to measure the real usage over a week, set the limit at several times the observed peak, and add a separate, tighter limit for the expensive endpoints. A limit that is too generous is a limit that does not protect; a limit that is too tight is a limit that causes an incident, and the second failure is the more visible one.

Common mistakes

A local counter per server. The effective limit is multiplied by the number of servers. Use a shared store.

A non-atomic increment. A read-then-write has a race that lets a burst through. Use an atomic operation or a script.

A limit that is too tight for a legitimate client. A limit that breaks a normal integration is worse than no limit, because it causes an outage. Measure the real usage before setting it.

No Retry-After. A client that does not know how long to wait retries immediately, which makes the overload worse.

Rate limiting only at the application. A flood that reaches the application has already consumed the connection and the parsing cost. A limit at the edge rejects it earlier.

Confusing rate limiting with DDoS protection. A distributed attack needs edge absorption, not a per-key counter.

A quotable summary

Rate limiting rejects requests over a limit, usually with a 429; throttling slows them down instead. The token bucket is the most common algorithm because it allows a burst up to the bucket size while enforcing an average rate, and it must be implemented atomically in a shared store so the limit is not multiplied by the number of servers. The key determines the fairness: IP before authentication, user or API key after. A 429 with a Retry-After header and the limit headers lets a client back off correctly.

For the API design around this, see rest-api-rate-limiting and api-error-responses-guide. For the broader context, caching-strategies-explained and load-testing-basics.

Frequently asked questions

What is the difference between rate limiting and throttling?
Rate limiting sets a maximum number of requests in a window and rejects anything over it, usually with a 429. Throttling slows requests down, either by delaying them or by reducing a quota gradually, rather than rejecting them outright. In practice the two are often used together, and the terms are frequently used interchangeably.
Which rate limiting algorithm should I use?
The token bucket is the most common because it allows a burst up to the bucket size while enforcing an average rate. A fixed window is the simplest but allows a burst at the boundary. A sliding window is more accurate and more expensive. A leaky bucket smooths the output rate. For most APIs, a token bucket is the right default.
What status code should a rate limit return?
429 Too Many Requests, with a Retry-After header that says how many seconds to wait. Include the limit and the remaining count in headers such as RateLimit-Limit and RateLimit-Remaining so a well-behaved client can pace itself.
Should I rate limit by IP or by user?
Both, for different reasons. An IP limit protects against an unauthenticated flood and is the only option before login. A user or API key limit is fairer and is the right key for an authenticated API, because many users can share an IP behind a proxy. Use a combination, with a stricter limit on the unauthenticated endpoints.
Does rate limiting prevent a DDoS attack?
No. A distributed denial-of-service attack comes from many sources and is best absorbed at the network edge by a CDN or a scrubbing service. Rate limiting protects the application from an abusive client and from a runaway script, which is a different problem.
How do I rate limit across multiple servers?
The counter must be shared, so it lives in a store such as Redis rather than in the process memory. A local counter on each server multiplies the effective limit by the number of servers, which is a common bug. Use an atomic increment with an expiry, or a Lua script for a multi-step operation.