Rate limiting caps how many requests a client can make in a period. It protects your service from abusive clients, runaway scripts and retry storms, and it keeps one customer from using capacity meant for everyone.

Algorithms

AlgorithmHow it worksProsCons
Fixed windowCount requests per calendar window (e.g. per minute)Simple, cheapBursts at window edges (twice the limit across a boundary)
Sliding window logKeep timestamps of recent requestsAccurateMemory per client
Sliding window counterWeighted mix of current and previous window countsSmooth, cheapApproximate
Token bucketTokens refill at a rate; each request spends oneAllows controlled burstsSlightly more state
Leaky bucketRequests drain at a fixed rate from a queueSmooth outputAdds queueing latency

For most APIs, a token bucket or sliding window counter is a good balance. A fixed window is fine for coarse limits.

A Redis fixed-window limiter

async function allow(clientId: string, limit = 600, windowSeconds = 60) {
  const window = Math.floor(Date.now() / 1000 / windowSeconds);
  const key = `rl:${clientId}:${window}`;
  const count = await redis.incr(key);
  if (count === 1) await redis.expire(key, windowSeconds + 1);
  return { allowed: count <= limit, remaining: Math.max(0, limit - count), resetAt: (window + 1) * windowSeconds };
}

What to limit by

  • API key or user for authenticated traffic: fair per customer.
  • IP address for anonymous traffic: be careful behind proxies and NAT, where many users share an IP, and only trust forwarded headers from proxies you control.
  • Endpoint: expensive endpoints (search, exports, login) get lower limits.
  • Tenant or plan: higher limits for higher tiers.

Where to enforce

At the edge (API gateway, load balancer, CDN) for coarse protection, and in the application for business-aware limits. Credential endpoints such as login and password reset deserve their own strict limits to slow down credential stuffing.

Tell clients clearly

Return 429 Too Many Requests with headers:

HTTP/1.1 429 Too Many Requests
Retry-After: 12
X-RateLimit-Limit: 600
X-RateLimit-Remaining: 0
X-RateLimit-Reset: 1761231060

Document your limits, and make clients back off using Retry-After. Well-behaved clients plus exponential backoff with jitter avoid turning a limit into a retry storm.

Failing open or closed

If the limiter's store (often Redis) is down, do you allow all requests (fail open) or reject them (fail closed)? Most public APIs fail open for availability and alert loudly; security-sensitive limits such as login attempts may fail closed.

Monitoring rate limits

  • 429 rate per client and endpoint: a sudden rise may mean a client bug, a new integration or abuse.
  • Top clients by request volume: who is close to their limit.
  • Latency of the limiter itself: it is on every request's path.
  • Correlation with errors: if 429s rise together with 5xx, your limits may be protecting a struggling service, which is good, or your service may be slow enough that clients retry, which is a different fix.

VitalRix APM shows status codes per endpoint from traces, and alert rules can watch the share of 429 responses. The VitalRix API itself uses per-key and per-IP limits with X-RateLimit headers and Retry-After; see the developer platform.

A worked example

A public API saw latency spikes every few minutes. Traces showed one customer's integration retrying failed requests immediately in a loop, reaching 2,000 requests per second during each spike. A per-key token bucket (100 requests per second with bursts of 200) returned 429 with Retry-After to that client, which backed off. The spikes stopped, other customers' latency returned to normal, and the customer fixed their retry logic after seeing the 429s in their logs.

Designing limits for a public API

  1. Measure real usage per client for a few weeks.
  2. Set default limits comfortably above the normal peak of typical clients.
  3. Set lower limits for expensive endpoints (search, export, bulk operations).
  4. Offer higher limits on higher plans, documented clearly.
  5. Return clear headers and errors, and document how to handle them.
  6. Monitor who hits limits and why, and adjust.

Rate limiting inside your own system

Limits are not only for external clients. A background job that calls an internal service, or a service that calls a third-party API with its own limits, benefits from a client-side limiter so it never exceeds what the dependency can take.

Should I return 429 or 503 when rate limiting?

429 when a specific client exceeds its limit; 503 when the service as a whole is overloaded.

How do I choose a limit?

Look at real usage per client, set limits comfortably above normal peaks, and lower them for expensive endpoints.

Is IP-based limiting enough?

For anonymous traffic it is a start, but shared IPs and proxies make it coarse. Use keys or user IDs where you can.

Try VitalRix free

Uptime monitoring, APM, logs, real user monitoring and status pages in one place. No credit card needed.