How it works
Client-side throttling can improve behavior, but an untrusted client can bypass it. A gateway, a proxy in front of your services, or middleware, code that runs before each handler, can reject requests before application work; an application-level limiter can use richer business context, such as a customer’s plan. Amazon API Gateway and NGINX both offer rate limiting without custom code, which is a good reason to start there. Existing gateway features, control over custom rules, and the cost of adding a new hop drive the choice.
A typical path is client → load balancer → limiter → API service. The limiter reads the current rule and counter, makes one decision, then forwards an allowed request or returns a throttle response. Keep policy configuration separate from fast-changing counters: rules change rarely and can be cached, while counters change on every request. A centrally shared counter store lets many stateless limiter instances enforce one logical quota.
Placement has a measurable payoff. In an example where an abusive client sends 5,000 requests per second and each would use 20 ms of application CPU, rejecting them at the gate saves 100 CPU cores of work (5,000 × 0.02 s). The price is a store round trip on allowed requests too, and one more dependency that can fail. Authenticate before any per-key limit, since an unverified key could spend someone else’s quota, and keep a coarse IP limit in front of authentication to protect it.
A mobile app already waits between calls. Why does the API still need its own limiter?
The app can be modified or replaced, and other clients may call the API. Server-side enforcement is necessary for a shared, trustworthy quota.
Use this as a guide for self review; written answers are not graded.Keep in mind
- Place enforcement before the resource it protects.
- Separate rule distribution from counter updates.