Sub topic 1
Why Services Need Rate Limiters
Prerequisites
Assume you know basic HTTP requests and responses, Python or a similar language, key-value stores such as Redis, and load-balanced service deployments.
Why Services Need Rate Limiters
A rate limiter controls how often a client can perform an operation during a period. It protects availability, limits accidental overload, and reduces the effect of abusive traffic. It can also enforce product rules, such as allowing 100 API requests per minute.
A useful policy answers three questions:
- What operation is limited?
- How many requests are allowed?
- Which client identity receives the allowance?
A limit should match the resource cost. A cheap read might allow many requests, while an expensive report or login attempt needs a lower limit. Decide whether rejected requests should wait, fail immediately, or consume quota.
Where to Enforce the Limit
Enforcement location affects coverage and information available to the limiter.
- A client-side limiter improves behavior but cannot protect your service from an untrusted client.
- A gateway or reverse proxy can apply a broad limit before requests reach application servers.
- Middleware can apply shared rules consistently across many routes.
- The service itself understands endpoint-specific costs and authorization context.
Most production systems combine layers. For example, a gateway can limit traffic per API key, while the service applies a stricter limit to password attempts.
Choosing the Key
The key identifies whose traffic is counted. Common choices have different weaknesses.
| Key | Useful for | Main problem |
|---|---|---|
| User ID | Authenticated usage limits | Unauthenticated traffic has no user |
| API key | Developer and application quotas | Shared keys combine multiple users |
| IP address | Broad abuse protection | Proxies and NAT can group many users |
| Endpoint plus identity | Expensive-operation limits | More state and policy complexity |
Use a stable identity when possible. Treat IP-based limits as a safety layer rather than the only identity rule. A malicious client can rotate addresses, while many legitimate users can share one address.
Fixed Window and Boundary Bursts
A fixed-window counter divides time into aligned intervals, such as one-minute windows. For each key, the limiter stores the current window number and request count. It allows requests while the count is below the limit, then resets the count in the next window.
This method is inexpensive and easy to implement. Its weakness is the boundary burst. With a limit of 100 requests per minute, a client could send 100 requests at 12:00:59 and 100 more at 12:01:00. The client sends 200 requests within about two seconds even though neither window exceeds its limit.
Exercise
Why might a service use both a gateway limit and an endpoint-specific service limit?
Show answer
The gateway blocks broad traffic before it consumes service capacity, while the service can apply stricter rules based on the operation's cost and authenticated identity.