Tutorial

Rate Limiting Strategies

Pick a rate limit strategy, fixed window, sliding window, or token bucket, and apply it fairly per caller so one client cannot starve the rest.

On this page

  1. Introduction
  2. Fixed window
  3. Sliding window
  4. Token bucket
  5. Per caller
  6. 429 with Retry-After
  7. Test burst and steady
  8. Sources

Introduction

This guide covers rate limiting strategies, a recurring concern in the resilience scenario. Without a rate limit one client can starve the rest, and the wrong strategy lets bursts through or rejects unfairly. Fixed window is simple but bursty at the edge, sliding window is smoother, and token bucket allows bursts up to a capacity, so the choice depends on your traffic shape.

Fixed window

Count requests in a fixed time window and reset at the edge; it is simple but allows a burst where two windows meet, which may be fine for coarse limits.

The trade-off worth naming is that count requests in a fixed time window and reset at the edge; it is simple but allows a burst where two windows meet, which may be fine for coarse limits adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

const count = new Map();
function allow(key, limit, win) {
  const now = Date.now();
  const w = Math.floor(now / win);
  const c = (count.get(key) ?? [w, 0]);
  if (c[0] !== w) { c[0] = w; c[1] = 0; }
  return ++c[1] <= limit && count.set(key, c) || false;
}

Sliding window

Track timestamps in a rolling window so the limit is smooth and there is no burst at the edge, at the cost of storing timestamps.

The trade-off worth naming is that track timestamps in a rolling window so the limit is smooth and there is no burst at the edge, at the cost of storing timestamps adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Token bucket

Refill tokens at a steady rate and allow a request if one is available, so a client can burst up to the bucket capacity then settle to the refill rate.

The trade-off worth naming is that refill tokens at a steady rate and allow a request if one is available, so a client can burst up to the bucket capacity then settle to the refill rate adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Per caller

Key the limit by api key or ip so one caller is limited independently, which is the fairness that stops a noisy client from starving the rest.

The trade-off worth naming is that key the limit by api key or ip so one caller is limited independently, which is the fairness that stops a noisy client from starving the rest adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

429 with Retry-After

On a limit breach, return 429 with a Retry-After header so the client knows when to try again, which is the polite contract.

The trade-off worth naming is that on a limit breach, return 429 with a retry-after header so the client knows when to try again, which is the polite contract adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

if (!allow(key, limit, win)) {
  res.set('Retry-After', '1').status(429).json({ error: 'rate limit' });
  return;
}

Test burst and steady

Fire a burst and assert some are 429, then fire at the steady rate and assert all pass, so the limit matches the strategy.

The trade-off worth naming is that fire a burst and assert some are 429, then fire at the steady rate and assert all pass, so the limit matches the strategy adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Sources

Sources: Rate limiting, Token bucket.

FAQ

Fixed window or sliding window?
Fixed window is simpler but allows a burst at the window edge where two windows meet; sliding window smooths that out at the cost of a little bookkeeping.
How do I key the limit?
By api key or ip per route, so one caller is limited independently of the others, which is the fairness point of rate limiting.

Related Examples