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

Redis

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

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

نظرة عامة

الموضوعات

  • النصوص (Strings)
  • الجداول المجزّأة (hashes)
  • المجموعات (sets)
  • المجموعات المرتّبة (sorted sets)
  • تصميم المفاتيح

تعلّم أولًا

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

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

  1. Use Redis strings, hashes, sets, sorted sets, and expirations for appropriate problems.
  2. Design key names and namespaces that support invalidation.
  3. Use Redis from Spring (Spring Data Redis, @Cacheable with a Redis cache manager).
  4. Explain Redis persistence options and memory limits, and what is lost on failure.

الدروس

  1. 01Redis في صورة واحدة
  2. 02هياكل البيانات واستخداماتها
  3. 03تصميم المفاتيح
  4. 04Redis من Spring
  5. 05الاستمرارية والذاكرة والإخلاء
  6. 06عرض عملي: لوحة متصدرين بالمجموعات المرتّبة
  7. 07عرض عملي: تخزين تفاصيل المنتج مؤقتًا باستخدام @Cacheable

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

Lab

MID-038-LAB1 · Catalog cache

Outcomes: 1–4 · Time: ~3 h Cache product details (by id) and category listings in Redis with different TTLs, using a RedisCacheManager. Add hit/miss metrics. Run a load test before and after and record hit ratio and p95 latency. Set maxmemory and an eviction policy for the Redis container. Acceptance: hit ratio and latency improvement measured under load.

Exercises (assessment: data-structure selection scenarios)

IDOutcomeTask
MID-038-EX011Choose a Redis structure for each: page-view counter, shopping cart, unique visitors per day, top 10 products, session token.
MID-038-EX021Using redis-cli, build a leaderboard with three products and read the ranking.
MID-038-EX032Design key names for product details, category products, and a "catalog version" that supports invalidation.
MID-038-EX043Write a RedisCacheManager bean with a 10-minute default TTL and a 1-hour TTL for categories.
MID-038-EX054Redis restarts with no persistence and holds product cache, sessions, and a pending-email queue. What is lost, and what must move elsewhere?
MID-038-EX064Pick maxmemory-policy for a pure cache and explain why not noeviction.
أظهر الحلول
  • EX01 Counter: string (INCR). Cart: hash. Unique visitors: set (or HyperLogLog if approximate is fine). Top 10: sorted set. Session: string with TTL.
  • EX02 ZADD board 5 p1 9 p2 2 p3, then ZRANGE board 0 -1 REV WITHSCORES gives p2, p1, p3.
  • EX03 shop:product:{id}:v1, shop:category:{id}:products:v{catalogVersion}, shop:catalog:version. Bumping the version makes old category keys unreachable; they expire by TTL.
  • EX04
java
RedisCacheConfiguration base = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(10));
RedisCacheManager.builder(factory).cacheDefaults(base)
    .withCacheConfiguration("categories", base.entryTtl(Duration.ofHours(1))).build();
  • EX05 Cache and sessions are lost (users logged out, cache cold, database spike). The email queue is lost permanently: move it to a durable queue or the database. Redis must not be the only copy.
  • EX06 allkeys-lru or allkeys-lfu: Redis evicts old entries to make room. noeviction rejects writes when full, so the cache stops working.

Outcomes 2-4 are also checked by the lab (key names, TTLs, memory limit).

الاختبار

Outcomes: O1 data structures · O2 key design · O3 Redis from Spring · O4 persistence and memory.

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

  1. 1Best structure for a leaderboard:O1
  2. 2Best structure to hold a cart with one field per product:O1
  3. 3Which command is atomic for counting?O1
  4. 4Which key is best designed?O2
  5. 5To find keys safely in production use:O2
  6. 6@Cacheable on a method fails to cache when:O3
  7. 7Which removes a cache entry?O3
  8. 8Per-cache TTL in Spring is configured in:O3
  9. 9RDB persistence is:O4
  10. 10For a pure cache, memory policy should typically be:O4

الجزء العملي

Structure-selection scenarios (MID-038-EX01, EX05) plus lab MID-038-LAB1. Pass: quiz at least 8/10, correct structures for all scenarios, and a lab with measured hit ratio and latency.

أخطاء شائعة

KEYS * in production; unbounded memory; Redis as the only copy of important data.

التقييم

Data-structure selection scenarios.

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

Caching patterns (MID-039) and rate limiting (MID-034).

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