Redis Cluster e sharding
Scopra come Redis Cluster scala orizzontalmente una cache su molti nodi usando gli hash slot, come vengono instradate le chiavi e come mantenere vicine le chiavi correlate tramite gli hash tag.
Redis Cluster e sharding è una lezione Caching Strategies: Redis + CDN + Edge Computing gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Caching Strategies: Redis + CDN + Edge Computing, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Caching Strategies: Redis + CDN + Edge Computing include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Impara Caching Strategies: Redis + CDN + Edge Computing con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 48
Domande Frequenti
La lezione «Redis Cluster e sharding» è gratuita?
Sì — il testo completo di «Redis Cluster e sharding» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Caching Strategies: Redis + CDN + Edge Computing, passa a CoddyKit PRO. Il corso Caching Strategies: Redis + CDN + Edge Computing include 4 lezioni in totale.
Cosa imparerò in «Redis Cluster e sharding»?
Scopra come Redis Cluster scala orizzontalmente una cache su molti nodi usando gli hash slot, come vengono instradate le chiavi e come mantenere vicine le chiavi correlate tramite gli hash tag. Eserciti Caching Strategies: Redis + CDN + Edge Computing con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Caching Strategies: Redis + CDN + Edge Computing?
Non è richiesta alcuna esperienza precedente. Caching Strategies: Redis + CDN + Edge Computing su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Redis Cluster e sharding»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Caching Strategies: Redis + CDN + Edge Computing?
Sì. Ogni lezione Caching Strategies: Redis + CDN + Edge Computing include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Persistenza e alta disponibilità di Redis
- Caching distribuito con Redis
- Redis Pub/Sub per l'invalidazione
- Redis Cluster e sharding