النافذة المنزلقة باستخدام المجموعات المرتبة في Redis
نفّذ محدِّد معدل موزعًا ودقيقًا بالنافذة المنزلقة باستخدام المجموعات المرتبة في Redis، مع عمليات ذرية وانتهاء تلقائي للإدخالات القديمة.
النافذة المنزلقة باستخدام المجموعات المرتبة في Redis درس مجاني في API Rate Limiting & Scalability Patterns على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في API Rate Limiting & Scalability Patterns، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة API Rate Limiting & Scalability Patterns 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
From Theory to Production
You understand the sliding window log and counter conceptually. Now build one that works across many servers using Redis sorted sets, the most common production technique for accurate distributed rate limiting.
Why Sorted Sets
A Redis sorted set (ZSET) stores members ranked by a numeric score. By using the request timestamp as the score, we get an ordered log of recent requests we can trim and count efficiently.
One Key Per Client
Each client gets a key like rl:user123. Every incoming request adds a member to that client's sorted set, scored by the current timestamp in milliseconds.
ZADD rl:user123 1700000000123 1700000000123Trimming the Window
Before counting, remove entries older than the window. If the window is 60 seconds, delete everything with a score below now - 60000. This keeps only the requests inside the current sliding window.
ZREMRANGEBYSCORE rl:user123 0 (now-60000)Counting Requests
After trimming, the cardinality of the set is the number of requests in the window. Compare it against the limit to allow or deny.
ZCARD rl:user123Atomicity Matters
Doing trim, add, and count as separate commands creates a race condition under concurrency. Wrap them in a single Lua script so Redis executes them atomically per client.
The Lua Script
A Lua script run with EVAL performs all steps in one atomic operation, returning whether the request is allowed. No two requests can interleave mid-check.
redis.call('ZREMRANGEBYSCORE', KEYS[1], 0, ARGV[1])
local count = redis.call('ZCARD', KEYS[1])
if count < tonumber(ARGV[3]) then
redis.call('ZADD', KEYS[1], ARGV[2], ARGV[2])
return 1
end
return 0Setting Expiry
Always set a TTL on the key (a bit longer than the window) so abandoned clients do not leak memory. Idle keys expire automatically.
EXPIRE rl:user123 120Accuracy vs Memory
This approach is highly accurate because it tracks every request timestamp, but memory grows with request volume per window. For very high-traffic clients, the sliding window counter approximation uses far less memory.
Handling Many Nodes
Because all API nodes talk to the same Redis, the limit is enforced globally regardless of which node handles a request. Use a Redis cluster or replica setup for availability, mindful that replication lag can slightly relax limits.
Failure Modes
Decide what happens if Redis is unreachable: fail open (allow traffic, risk overload) or fail closed (block traffic, risk outage). Most public APIs fail open with a local fallback limiter.
Quick Check
Test your understanding of the Redis sliding window.
Recap
You built a distributed sliding window:
- Store request timestamps in a Redis sorted set, one key per client.
- Trim old entries with
ZREMRANGEBYSCORE, count withZCARD. - Wrap trim/count/add in a Lua script for atomicity.
- Set a TTL to free memory, and decide fail-open vs fail-closed for Redis outages.
الأسئلة الشائعة
هل درس «النافذة المنزلقة باستخدام المجموعات المرتبة في Redis» مجاني؟
نعم — نص درس «النافذة المنزلقة باستخدام المجموعات المرتبة في Redis» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة API Rate Limiting & Scalability Patterns، انتقل إلى CoddyKit PRO. تتضمن دورة API Rate Limiting & Scalability Patterns 4 دروس في المجموع.
ماذا ستتعلم في «النافذة المنزلقة باستخدام المجموعات المرتبة في Redis»؟
نفّذ محدِّد معدل موزعًا ودقيقًا بالنافذة المنزلقة باستخدام المجموعات المرتبة في Redis، مع عمليات ذرية وانتهاء تلقائي للإدخالات القديمة. تتمرن على API Rate Limiting & Scalability Patterns مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ API Rate Limiting & Scalability Patterns؟
لا تُشترط خبرة سابقة. API Rate Limiting & Scalability Patterns على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «النافذة المنزلقة باستخدام المجموعات المرتبة في Redis»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس API Rate Limiting & Scalability Patterns هذا؟
نعم. كل درس في API Rate Limiting & Scalability Patterns يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تنفيذ سجل النافذة المنزلقة
- استراتيجية عدّاد النافذة المنزلقة
- مقارنة الخوارزميات والمفاضلات
- النافذة المنزلقة باستخدام المجموعات المرتبة في Redis