AI Hints
Home›System Design Tutorial›Rate Limiting
System Design · Lesson 34 of 120

Rate Limiting

Protecting the system.

⏱ 2 min read▶ Video at 39:10🎓 Free
Watch this lesson in the System Design courseWatch on YouTube ↗Full System Design course

One buggy script can send Shoply ten thousand requests a second. Without protection, it slows the site down for everyone. Rate limiting caps how many requests each client can make.

Rate Limiting
From the video: Rate Limiting
  • One buggy script
  • Slows everyone down
  • Cap each client

Why limit

Rate limits protect you in several ways. They stop accidental floods from buggy code. They slow down attackers guessing passwords. They keep costs under control. And they keep things fair, so one heavy user can't starve everyone else.

Bugsaccidental floods
Attackspassword guessing
Costpredictable bills
Fairnessno one hogs it

Designing the limit

When you design a limit, decide a few things. Who is limited, by user, API key, or IP address. What the limit is, like a hundred requests per minute. Where it runs, often in the gateway. And what to answer, which is status four two nine, too many requests, with a header saying when to try again.

keyvalue
Whouser, API key or IP
How manye.g. 100 per minute
Whereusually the gateway
Answer429 + Retry-After

Summary

Rate limiting caps requests per client, to stop floods, attacks, and unfair use. Over the limit, answer four two nine. Next, the algorithms that count requests.

Key takeaways
  • Per client limits
  • Usually in the gateway
  • 429 Too Many Requests
Next up: Rate Limiter Algorithms — token bucket and friends