GPT-4System design
موجه تصميم محدد معدل API

لماذا تهم هذه المطالبة
Rate limiting protects both your API and your users' experience. A well-designed system with clear tiers and headers builds developer trust.
فيم نستخدمها
System design
المطالبة
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.
النتيجة
## 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.
يحمي تحديد المعدل كلاً من واجهة API وتجربة المستخدمين. فالنظام المصمم بعناية، والذي يتميز بمستويات وترويسات واضحة، يُرسّخ ثقة المطورين. ويُظهر هذا الموجه نهجاً منظماً في تصميم الأنظمة، حيث يُنتج نتائج متسقة وعالية الجودة يمكن البناء عليها وتطويرها.