Video in progress
Why do you need rate limiting, and where do you put it?
Rate limiting caps how many requests one client can send in a given time, for example 100 per minute. It protects the service from abuse, brute force and accidental infinite loops.
📖 Answer
Where it is used:
- on the login endpoint — to stop password guessing (for example, 5 attempts / 15 minutes);
- on a public API — a per-key limit based on the plan;
- when sending SMS — no more than one SMS per minute to the same number, otherwise your money burns;
- on expensive endpoints — report generation, search.
What to count the limit by: user ID, API key or IP. Be careful with IP — behind one office or one mobile carrier, thousands of people can come out with the same IP.
Where to put it: usually at the API gateway or in Nginx (limit_req), because then the request is stopped before it reaches the application. Business-specific limits (for example, "3 free reports per day") go in the application itself, with a counter in Redis.
When the limit is exceeded, you return HTTP 429 Too Many Requests.
Sign up to mark studied questions and save your progress.