⌨️ Codingadvancedrate-limitingbackendredisapisecurity

Implement a Rate Limiter

Build a production-ready rate limiter using token bucket or sliding window algorithm.

The Prompt

prompt.txt
Implement a rate limiter for the following requirements. Include:
1. Algorithm choice with justification (token bucket / sliding window / fixed window)
2. Complete implementation in [YOUR LANGUAGE]
3. Redis integration for distributed environments (if needed)
4. Middleware wrapper for easy integration
5. Proper HTTP headers (X-RateLimit-Limit, X-RateLimit-Remaining, Retry-After)
6. Different limits for different route groups or user tiers

Requirements:
- Limit: [e.g., 100 requests per minute per IP, 1000 per minute for authenticated users]
- Environment: [SINGLE SERVER / DISTRIBUTED / SERVERLESS]
- Framework: [EXPRESS / FASTAPI / NEXTJS]
- Storage: [REDIS / IN-MEMORY / UPSTASH]

Example Output

Implemented a sliding window rate limiter using Redis ZADD/ZCOUNT for distributed state. The Express middleware checks the user's tier (free: 60/min, pro: 600/min), returns 429 with Retry-After header when exceeded, and uses Lua scripts for atomic check-and-increment.

FAQ

Which AI model is best for Implement a Rate Limiter?

Claude Sonnet 4 — excellent for algorithm-heavy implementations with clear reasoning.

How do I use the Implement a Rate Limiter prompt?

Copy the prompt, replace the [BRACKETED] placeholders with your specific information, and paste into your preferred AI assistant (ChatGPT, Claude, Gemini, etc.). Implemented a sliding window rate limiter using Redis ZADD/ZCOUNT for distributed state. The Express middleware checks the user's tier (free: 60/min, pro: 600/min), returns 429 with Retry-After header when exceeded, and uses Lua scripts for atomic check-and-increment.