طراحی محدودکننده نرخ API
Why this prompt matters
Rate limiting protects both your API and your users' experience. A well-designed system with clear tiers and headers builds developer trust.
What we use it for
System design
Prompt
Design a rate limiting system for the described API. Include: 1) Algorithm choice with tradeoffs (token bucket, sliding window, fixed window), 2) Rate limit tiers for different user types, 3) Response headers (X-RateLimit-*), 4) What to do when limits are exceeded (429 response body), 5) Distributed rate limiting strategy for multiple servers, 6) Implementation pseudocode.
Result
## Rate Limiting Design
### Algorithm: Sliding Window Counter Combines the precision of sliding window with the memory efficiency of fixed window.
### Tiers | Tier | Requests/min | Burst | Cost | |------|-------------|-------|------| | Free | 30 | 5 | $0 | | Pro | 300 | 50 | $29/mo | | Enterprise | 3000 | 500 | Custom |
### Response Headers ``` X-RateLimit-Limit: 300 X-RateLimit-Remaining: 287 X-RateLimit-Reset: 1710000060 Retry-After: 30 ```
### Distributed Strategy Use Redis with Lua scripts for atomic increment-and-check.
Rate limiting protects both your API and your users' experience. A well-designed system with clear tiers and headers builds developer trust.
This prompt demonstrates a structured approach to system design, producing consistent, high-quality results that can be iterated upon.