Prompt
Should I put rate limits at the gateway or in the app?
Latest observation
Nov 2, 2026
OpenAI APIWeb search: off
Usually: do both, but for different reasons.
Put rate limits at the gateway when you want:
- Early protection against abuse, spikes, bots, or accidental traffic floods
- One central place to enforce limits across many services
- To reduce load on your app and downstream dependencies
- Simple policies like per-IP, per-token, per-tenant, or per-route limits
Put rate limits in the app when you want:
- Business-aware limits like per-user, per-plan, per-feature, or per-resource
- Context-sensitive rules that the gateway may not know
- Protection at the domain level, e.g. “10 password resets per hour” or “5 expensive searches per minute”
- Defense in depth in case requests bypass the gateway internally
Best practice
- Gateway for coarse, universal throttling
- App for fine-grained, business-specific throttling
Rule of thumb
If the limit is about traffic control, use the gateway.
If the limit is about business logic or cost control, use the app.
A practical setup
- Gateway: per-IP / per-api-key / per-tenant global limits
- App: per-user actions, expensive endpoints, and quota enforcement
If you want, I can sketch a recommended rate-limiting design for your stack (e.g. Nginx, Kong, API Gateway, Express, Spring, FastAPI, etc.).