What to give
- Language and framework: [e.g. Node.js with Express, Python with FastAPI, PHP with Laravel, Go]
- Shared store available: [e.g. Redis, the database, none and a single server]
- Routes to protect: [e.g. POST /login, POST /otp/send, GET /api/search, POST /contact]
- Normal use per route: [e.g. a real user logs in a few times a day, search up to 30 times a minute]
- Behind a proxy or CDN: [yes or no, and which header carries the client IP]
- Signed-in users and API keys: [how requests are identified]
Task
Design the limits and write the rate limiting code.
Design checklist
- Pick the key per route: client IP for anonymous routes, user ID or API key for signed-in ones, and the target account or phone for login and OTP routes.
- Use a sliding window or token bucket; explain the choice in one line.
- Read the client IP only from the trusted proxy header given; never trust a header the client can set freely.
- Store counters in the shared store with an expiry, so limits hold across servers.
- Return 429 with a Retry-After header and a short JSON error body.
- Add a stricter second tier for repeated failures on login and OTP, such as a growing wait.
- Allow-list health checks and internal services.
- Log blocked requests with route and key, never the password or OTP.
- Decide what happens when the store is down: fail open for search, fail closed for OTP.
Output format
- | Route | Key | Limit | Window | Algorithm | On store failure |
- The middleware code with comments.
- How to attach it to each route.
- Tests: under the limit passes, over the limit returns 429, the window resets, separate keys stay separate.
Guardrails
- Rate limiting slows abuse; it does not replace strong passwords, OTP expiry or bot checks.
- Avoid limits so tight that users behind one office or mobile network IP are locked out together.
- State every assumed number as an assumption to tune.






