Caching Strategies: Redis + CDN + Edge Computing · درس

عنقود Redis والتقسيم

تعلّم كيف يوسّع Redis Cluster ذاكرة التخزين المؤقت أفقيًا عبر عقد متعددة باستخدام خانات التجزئة، وكيف تُوجّه المفاتيح، وكيف تحافظ على المفاتيح المرتبطة معًا باستخدام وسوم التجزئة.

الدرس 4 من 413 خطوة

عنقود Redis والتقسيم درس مجاني في Caching Strategies: Redis + CDN + Edge Computing على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Caching Strategies: Redis + CDN + Edge Computing، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Caching Strategies: Redis + CDN + Edge Computing 4 دروس في المجموع.

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

Why Scale Beyond One Node?

A single Redis instance is limited by one server's memory and throughput. Redis Cluster spreads data across many nodes so the cache can grow far beyond a single machine.

Hash Slots

Redis Cluster divides the keyspace into 16384 hash slots. Each node owns a range of slots. A key belongs to exactly one slot, determined by hashing the key.

TOTAL_SLOTS = 16384

def slot_for(crc):
    return crc % TOTAL_SLOTS

print('Slot:', slot_for(123456789))

CRC16 Routing

The slot is computed as CRC16(key) mod 16384. The client (or cluster) uses this to know which node holds the key, so reads and writes go directly to the right place.

Slot Distribution

With three nodes, slots split roughly into thirds. Adding a node means moving some slots to it; removing one redistributes its slots. This is how the cluster rebalances.

nodes = 3
per_node = 16384 // nodes
print('Approx slots per node:', per_node)

MOVED and ASK Redirects

If a client contacts the wrong node, the node replies with a MOVED redirect pointing to the correct node. During slot migration, a temporary ASK redirect is used. Smart clients cache the slot map.

The Multi-Key Limitation

Operations across multiple keys (like MGET or transactions) only work if all keys live in the same slot. Otherwise the cluster rejects the command, because the keys may be on different nodes.

Hash Tags

Hash tags solve this. Only the part inside curly braces is hashed, so keys sharing a tag land in the same slot. user:{42}:profile and user:{42}:cart are co-located.

def hash_part(key):
    if '{' in key and '}' in key:
        start = key.index('{') + 1
        end = key.index('}')
        return key[start:end]
    return key

print(hash_part('user:{42}:cart'))

Replicas for Availability

Each master node can have replicas. If a master fails, a replica is promoted automatically, so the slot range stays available. This combines sharding with high availability.

Cluster vs Client-Side Sharding

Before Redis Cluster, apps sharded manually by hashing keys to instances in client code. Cluster mode moves that logic into Redis itself, handling rebalancing and failover automatically.

Trade-offs of Clustering

Clustering adds power but also constraints: cross-slot operations are limited, multi-key Lua scripts need same-slot keys, and operations are more complex. Use it when a single node truly is not enough.

When to Cluster

Reach for Redis Cluster when memory or throughput exceeds one node, when you need automatic failover at scale, or when a region demands many nodes. For smaller caches, a single primary with a replica is simpler.

Quick Check

You need MGET to fetch two related keys atomically in a Redis Cluster. How do you guarantee they live on the same node?

Recap

You learned Redis Cluster and sharding:

  • The keyspace splits into 16384 hash slots owned by nodes.
  • CRC16 routing plus MOVED/ASK redirects locate keys.
  • Multi-key ops need same-slot keys; hash tags co-locate them.
  • Replicas add automatic failover.

Clustering scales a cache horizontally when one node is not enough.

البدء مجانًا

تعلم Caching Strategies: Redis + CDN + Edge Computing مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
12
الدروس
48

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

هل درس «عنقود Redis والتقسيم» مجاني؟

نعم — نص درس «عنقود Redis والتقسيم» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Caching Strategies: Redis + CDN + Edge Computing، انتقل إلى CoddyKit PRO. تتضمن دورة Caching Strategies: Redis + CDN + Edge Computing 4 دروس في المجموع.

ماذا ستتعلم في «عنقود Redis والتقسيم»؟

تعلّم كيف يوسّع Redis Cluster ذاكرة التخزين المؤقت أفقيًا عبر عقد متعددة باستخدام خانات التجزئة، وكيف تُوجّه المفاتيح، وكيف تحافظ على المفاتيح المرتبطة معًا باستخدام وسوم التجزئة. تتمرن على Caching Strategies: Redis + CDN + Edge Computing مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Caching Strategies: Redis + CDN + Edge Computing؟

لا تُشترط خبرة سابقة. Caching Strategies: Redis + CDN + Edge Computing على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «عنقود Redis والتقسيم»؟

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

هل يمكنني كتابة وتشغيل أكواد في درس Caching Strategies: Redis + CDN + Edge Computing هذا؟

نعم. كل درس في Caching Strategies: Redis + CDN + Edge Computing يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

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

  1. استمرارية Redis والتوافر العالي
  2. التخزين المؤقت الموزّع باستخدام Redis
  3. Pub/Sub في Redis لإبطال التخزين المؤقت
  4. عنقود Redis والتقسيم
← العودة إلى Caching Strategies: Redis + CDN + Edge Computing