انتقل إلى المحتوى
أمين أبوزيد
MID-040

اتساق الذاكرة المؤقتة

  • 5 درس
  • 2 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • الاندفاعات (Stampedes)
  • البيانات القديمة
  • سباقات الإبطال
  • المفاتيح الساخنة (Hot Keys)

تعلّم أولًا

2 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Explain and prevent cache stampedes (locking, request coalescing, early refresh).
  2. Identify invalidation races that leave stale data and fix them (delete-after-write, versioning).
  3. Detect and mitigate hot keys.
  4. State an explicit staleness tolerance for each cached item.

الدروس

  1. 01الاندفاعات (Stampedes)
  2. 02سباقات الإبطال
  3. 03المفاتيح الساخنة
  4. 04ميزانيات القِدَم (Staleness Budgets)
  5. 05عرض عملي: اندفاع بعد انتهاء صلاحية الذاكرة المؤقتة ثم الإصلاح

المختبر والتمارين

Lab

MID-040-LAB1 · Flash sale

Outcomes: 1–4 · Time: ~2 h Simulate a flash sale: 2000 requests per second on one product whose cache entry expires during the test. Measure database queries per second. Add stampede protection (lock or coalescing plus TTL jitter) and a local short-lived cache for the hot key. Write the staleness budget for the product. Acceptance: database load stays bounded (state the measured maximum queries per second before and after).

Exercises (assessment: race-condition analysis)

IDOutcomeTask
MID-040-EX0111000 readers hit an expired key; each query takes 50 ms. Estimate database load without and with a single-flight lock.
MID-040-EX022Draw the interleaving that leaves a stale price after "database update then cache delete", and fix it with versioning.
MID-040-EX032Explain why "delete cache, then update database" is worse than the reverse.
MID-040-EX043Propose a mitigation for a hot product key across 20 instances.
MID-040-EX054Write a staleness budget for price, stock display, category tree, and product description.
أظهر الحلول
  • EX01 Without: up to 1000 identical queries in a 50 ms window. With a lock: 1 query; others wait about 50 ms or get the stale value.
  • EX02 Reader misses and reads old row; writer commits and deletes; reader writes old value. With versions: the cache write is "put if version > cached version", or the stored version is compared with the row version on read; the old value is rejected or short-lived.
  • EX03 A reader between the delete and the update reloads the old row and caches it; it stays stale until TTL.
  • EX04 Local Caffeine L1 with 1-2 s TTL, plus coalescing and jitter; optionally key replicas.
  • EX05 Example: price 1 s (event + short TTL), stock display 30 s (checkout verifies), category tree 1 h, description 10 min.

Outcome 3 is also covered by the lab (hot key).

الاختبار

Outcomes: O1 stampedes · O2 invalidation races · O3 hot keys · O4 staleness budgets.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1A cache stampede happens when:O1
  2. 2Which does NOT reduce stampedes?O1
  3. 3A lock around cache rebuilds should also have:O1
  4. 4A stale value stays in the cache after a delete because:O2
  5. 5Which order is safer?O2
  6. 6Versioning fixes races by:O2
  7. 7A hot key is:O3
  8. 8A good mitigation for a hot key:O3
  9. 9A staleness budget is:O4
  10. 10For data with zero staleness tolerance, such as a payment total:O4

الجزء العملي

Race-condition analysis (MID-040-EX02, EX03) plus lab MID-040-LAB1. Pass: quiz at least 8/10, correct race diagram and fix, and a lab with bounded database load measured.

أخطاء شائعة

Identical TTLs on everything; ignoring races because "it's rare".

التقييم

Race-condition analysis.

الصلة بالمسار الوظيفي

Large-scale performance (SRA-007).

اكتب للبحث في المستويات والمراحل والوحدات والدروس.