Redis Cluster und Sharding
Lernen Sie, wie Redis Cluster einen Cache mithilfe von Hash-Slots horizontal über viele Knoten skaliert, wie Schlüssel weitergeleitet werden und wie Sie zusammengehörige Schlüssel mithilfe von Hash-Tags zusammenhalten.
Redis Cluster und Sharding ist eine kostenlose Caching Strategies: Redis + CDN + Edge Computing-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Caching Strategies: Redis + CDN + Edge Computing-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Caching Strategies: Redis + CDN + Edge Computing-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.
Lerne Caching Strategies: Redis + CDN + Edge Computing mit einem KI-Tutor — kostenlos
Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.
- Kurse
- 12
- Lektionen
- 48
Häufig gestellte Fragen
Ist die Lektion „Redis Cluster und Sharding“ kostenlos?
Ja — der vollständige Text von „Redis Cluster und Sharding“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Caching Strategies: Redis + CDN + Edge Computing-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Caching Strategies: Redis + CDN + Edge Computing-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Redis Cluster und Sharding“?
Lernen Sie, wie Redis Cluster einen Cache mithilfe von Hash-Slots horizontal über viele Knoten skaliert, wie Schlüssel weitergeleitet werden und wie Sie zusammengehörige Schlüssel mithilfe von Hash-T… Du übst Caching Strategies: Redis + CDN + Edge Computing mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Caching Strategies: Redis + CDN + Edge Computing zu starten?
Keine Vorkenntnisse erforderlich. Caching Strategies: Redis + CDN + Edge Computing auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Redis Cluster und Sharding“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Caching Strategies: Redis + CDN + Edge Computing-Lektion Code schreiben und ausführen?
Ja. Jede Caching Strategies: Redis + CDN + Edge Computing-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Redis-Persistenz und Hochverfügbarkeit
- Verteiltes Caching mit Redis
- Redis Pub/Sub zur Invalidierung
- Redis Cluster und Sharding