IICTPIICTP

Blog / Cloud & Architecture / System Design & Architecture

System Design & ArchitectureNovember 20, 20264 MIN READ

What is rate limiting, and how do you choose the limits?

Rate limiting caps how many requests a client can make in a period of time. It protects a service from overload, keeps one heavy user from crowding out everyone else, and slows down abuse such as password guessing and scraping.

In this post
  1. How do the common algorithms work?
  2. What should the client see when it is limited?
  3. What do you limit by?
  4. How do you choose the numbers?
  5. Where should rate limiting run?
  6. Sources and further reading

How do the common algorithms work?

A fixed window counts requests per minute or hour and resets at the boundary, which is simple but lets a burst through at the edge of two windows. A sliding window smooths that out. A token bucket adds tokens at a steady rate up to a maximum, and each request spends one, so it allows short bursts while holding the average rate.

What should the client see when it is limited?

In HTTP, the standard response is status 429 Too Many Requests, defined in RFC 6585. Include a Retry-After header saying when to try again, so well-behaved clients wait instead of retrying at once.

What do you limit by?

Limit by whatever identifies the party you are protecting against: an API key, a signed-in user, an account or, as a last resort, an IP address. IP addresses are shared by many users behind one network and are easy for an attacker to rotate, so they make a poor only key.

How do you choose the numbers?

Start from what the service can actually handle, measured under load, and from how real clients behave, taken from your logs. Set limits a little above normal use, alert when clients hit them, and review them as usage changes. Sensitive actions such as sign-in and password reset need much lower limits than reads.

Where should rate limiting run?

Run it as early as possible, at the API gateway or load balancer, so rejected requests cost little. In a service with many instances, keep the counters in a shared store, or each instance will allow the full limit on its own.

Sources and further reading

  1. IETF. RFC 6585, Additional HTTP Status Codes. 2012.
  2. IETF. RFC 9110, HTTP Semantics (Retry-After). 2022.
  3. OWASP. API Security Top 10: Unrestricted Resource Consumption. owasp.org.

Go deeper: CTP™ credential · the TPBOK™ standard · Check your experience

IICTP is an independent technical credentialing body.

Keep reading

← All posts

Where do you stand?

Find the credential that fits the work you do.