منع اندفاعات ذاكرة التخزين المؤقت وThundering Herd
تعلّم ماهية اندفاعة ذاكرة التخزين المؤقت، ولماذا ترهق قاعدة البيانات عند انتهاء صلاحية مفتاح شائع، وتقنيات القفل والتحديث التي تحافظ على أمان الواجهة الخلفية تحت الضغط
منع اندفاعات ذاكرة التخزين المؤقت وThundering Herd درس مجاني في 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 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
What Is a Cache Stampede?
A cache stampede (or thundering herd) happens when a popular cached key expires and many requests miss the cache at the same moment. They all rush to the database to rebuild the value, overwhelming it.
Why It Is So Dangerous
Under high traffic, hundreds or thousands of concurrent misses can hit the database in milliseconds. The backend that normally serves zero load for that key suddenly takes the full firehose, causing latency spikes or outages.
The Naive Cache-Aside Flow
The basic pattern reads the cache, and on a miss queries the DB and stores the result. Every concurrent miss runs this DB query — that is the vulnerability.
Solution 1: A Recompute Lock
Let only the first missing request rebuild the value while others wait or serve stale. Redis SET NX grants a short-lived lock to exactly one client.
SET lock:product:42 "1" NX EX 10How the Lock Flow Works
On a miss:
- Try to acquire the lock with
SET NX - If you got it, query the DB and repopulate the cache
- If not, briefly wait and re-read the cache
Only one DB query runs per expiry.
Solution 2: Stale-While-Revalidate
Store the value with a logical expiry earlier than its physical TTL. When the logical time passes, serve the stale value immediately and refresh it in the background, so users never wait on a miss.
Solution 3: Early Probabilistic Expiry
Each request, with a small growing probability as expiry nears, voluntarily refreshes the key before it dies. This spreads recomputation across time so the herd never forms.
Adding Jitter to TTLs
If many keys are written together (e.g. on deploy) they expire together, causing a synchronized stampede. Add random jitter to each TTL so expiries spread out.
SET product:42 "..." EX 305
SET product:43 "..." EX 318Releasing the Lock Safely
After rebuilding, delete the lock. Give it a short TTL too, so a crashed worker does not hold it forever and block refreshes.
DEL lock:product:42Choosing a Strategy
Guidelines:
- Lock: simplest, briefly delays some requests
- Stale-while-revalidate: best UX, needs background refresh
- Probabilistic + jitter: smooths load, no waiting
Combine them for very hot keys.
Negative Caching
A related danger is the cache penetration miss: many requests for a key that does not exist in the DB always miss the cache and hammer the backend. Cache the not-found result briefly too, so repeated lookups are absorbed.
SET product:9999 "__NULL__" EX 30Quick Check
Test your stampede-prevention knowledge.
Recap
You learned to stop cache stampedes:
- A stampede floods the DB when a hot key expires
- A
SET NXrecompute lock limits rebuilds to one client - Stale-while-revalidate serves old data while refreshing
- Probabilistic early expiry spreads recomputation out
- TTL jitter prevents synchronized mass expiry
الأسئلة الشائعة
هل درس «منع اندفاعات ذاكرة التخزين المؤقت وThundering Herd» مجاني؟
نعم — نص درس «منع اندفاعات ذاكرة التخزين المؤقت وThundering Herd» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Redis Caching & Messaging (Pub/Sub, Streams)، انتقل إلى CoddyKit PRO. تتضمن دورة Redis Caching & Messaging (Pub/Sub, Streams) 4 دروس في المجموع.
ماذا ستتعلم في «منع اندفاعات ذاكرة التخزين المؤقت وThundering Herd»؟
تعلّم ماهية اندفاعة ذاكرة التخزين المؤقت، ولماذا ترهق قاعدة البيانات عند انتهاء صلاحية مفتاح شائع، وتقنيات القفل والتحديث التي تحافظ على أمان الواجهة الخلفية تحت الضغط تتمرن على Redis Caching & Messaging (Pub/Sub, Streams) مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Redis Caching & Messaging (Pub/Sub, Streams)؟
لا تُشترط خبرة سابقة. Redis Caching & Messaging (Pub/Sub, Streams) على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «منع اندفاعات ذاكرة التخزين المؤقت وThundering Herd»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Redis Caching & Messaging (Pub/Sub, Streams) هذا؟
نعم. كل درس في Redis Caching & Messaging (Pub/Sub, Streams) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- لماذا التخزين المؤقت؟ مقدمة إلى التخزين المؤقت
- تطبيق أنماط التخزين المؤقت الأساسية
- إخلاء ذاكرة التخزين المؤقت وانتهاء الصلاحية
- منع اندفاعات ذاكرة التخزين المؤقت وThundering Herd