0Pricing
Redis Caching & Messaging (Pub/Sub, Streams) · درس

تحديد معدل الطلبات الموزّع

نسّق حدود الطلبات عبر عدة مثيلات للتطبيق باستخدام عدادات Redis ونصوص Lua الذرية لخوارزميات النافذة الثابتة والنافذة المنزلقة ودلو الرموز

تحديد معدل الطلبات الموزّع درس مجاني في Redis Caching & Messaging (Pub/Sub, Streams) على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Redis Caching & Messaging (Pub/Sub, Streams)، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Redis Caching & Messaging (Pub/Sub, Streams) 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Local Limits Do Not Scale

An in-memory rate limiter only counts requests on one server. With many app instances behind a load balancer, you need a shared view of usage. Redis, being central and atomic, is the natural coordination point.

Fixed Window Counter

The simplest algorithm: a counter per time window. INCR the key; set a TTL equal to the window on first increment. Reject when the count exceeds the limit.

INCR rl:user:42:1716900000
EXPIRE rl:user:42:1716900000 60

The Race Condition

Doing INCR then EXPIRE as two commands risks a key without a TTL if the client dies in between. An atomic Lua script fixes this by running both as one operation.

local c = redis.call('INCR', KEYS[1])
if c == 1 then redis.call('EXPIRE', KEYS[1], ARGV[1]) end
return c

Why Atomicity Matters

Across many instances, concurrent requests could otherwise read and write counters in interleaved order. Lua scripts run atomically on the server, so the entire check-and-increment happens with no interleaving.

Fixed Window Burst Problem

Fixed windows allow bursts at the boundary: a client can send a full window's worth at the end of one window and again at the start of the next, doubling the effective rate.

Sliding Window Log

A sorted set of request timestamps gives a precise sliding window. Drop old entries, count what remains, and add the new request, all in one script.

ZREMRANGEBYSCORE rl:user:42 0 (now-window)
ZCARD rl:user:42
ZADD rl:user:42 now now

Token Bucket

The token bucket allows controlled bursts. Tokens refill at a fixed rate up to a cap; each request consumes one. Store tokens and last-refill time in a hash and update atomically with Lua.

HSET rl:tb:user:42 tokens 10 ts 1716900000

Refill Logic

On each request, compute elapsed time, add elapsed * rate tokens (capped at the bucket size), then allow the request if at least one token remains. The Lua script keeps this consistent across instances.

Choosing an Algorithm

Fixed window: cheapest, allows boundary bursts. Sliding log: precise but more memory. Token bucket: smooth with controlled bursts, great for APIs.

Returning Useful Headers

Tell clients about their limits: return remaining requests and reset time so well-behaved clients can self-throttle.

# X-RateLimit-Remaining: 7
# X-RateLimit-Reset: 1716900060

Resilience Note

Decide a fallback if Redis is unreachable: fail open (allow traffic) for availability, or fail closed (deny) for protection. The right choice depends on whether the limit guards cost or correctness.

Quick Check

Test your understanding of distributed rate limiting.

Recap

You built distributed rate limiting on Redis: fixed-window counters, atomic Lua to avoid TTL leaks and races, sliding-window logs with sorted sets, and token buckets for smooth bursts. Centralizing the counters makes limits consistent across all app instances; choose your fail-open vs fail-closed fallback deliberately.

الأسئلة الشائعة

هل درس «تحديد معدل الطلبات الموزّع» مجاني؟

نعم — نص درس «تحديد معدل الطلبات الموزّع» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Redis Caching & Messaging (Pub/Sub, Streams)، انتقل إلى CoddyKit PRO. تتضمن دورة Redis Caching & Messaging (Pub/Sub, Streams) 4 دروس في المجموع.

ماذا ستتعلم في «تحديد معدل الطلبات الموزّع»؟

نسّق حدود الطلبات عبر عدة مثيلات للتطبيق باستخدام عدادات Redis ونصوص Lua الذرية لخوارزميات النافذة الثابتة والنافذة المنزلقة ودلو الرموز تتمرن على Redis Caching & Messaging (Pub/Sub, Streams) مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Redis Caching & Messaging (Pub/Sub, Streams)؟

لا تُشترط خبرة سابقة. Redis Caching & Messaging (Pub/Sub, Streams) على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «تحديد معدل الطلبات الموزّع»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Redis Caching & Messaging (Pub/Sub, Streams) هذا؟

نعم. كل درس في Redis Caching & Messaging (Pub/Sub, Streams) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. الأقفال الموزّعة باستخدام Redis
  2. أنماط انتخاب القائد
  3. Redis كخدمة تنسيق
  4. تحديد معدل الطلبات الموزّع
← العودة إلى Redis Caching & Messaging (Pub/Sub, Streams)