Cluster Sharding & Hash Slots

7 questions found

What is sharding in a Redis Cluster, and why is it necessary for very large datasets?

Beginner
Sharding splits your data across multiple Redis nodes instead of storing everything on a single server, letting you scale beyond the memory and processing limits of one machine by distributing both the data and the workload of serving requests across several nodes working together.
-- A Redis Cluster automatically distributes keys
-- across multiple nodes based on hash slots
redis-cli -c -h node1 set user:123 'John'
Real-world example A large social media platform shards its user session data across many Redis nodes, since a single server could never hold enough memory to store session data for millions of active users at once.

Common follow-ups: How does Redis Cluster decide which node a specific key belongs to?;What happens if you need to add more nodes to a cluster later?

Clustering;Redis Replication

What are hash slots in Redis Cluster, and how many are there in total?

Beginner
Redis Cluster divides its entire key space into sixteen thousand three hundred and eighty four hash slots, and every key is assigned to exactly one of these slots based on a hash calculation, with each node in the cluster responsible for owning and serving a specific range of these slots.
-- Check which hash slot a key belongs to
redis-cli cluster keyslot user:123
Real-world example A three node Redis Cluster might divide its sixteen thousand three hundred eighty four hash slots roughly evenly, with each node responsible for owning about five thousand four hundred slots and the keys that map to them.

Common follow-ups: How do you check which specific node currently owns a particular hash slot?;What happens to hash slots when a node is added or removed from the cluster?

Clustering;Cluster Sharding & Hash Slots

How does Redis Cluster determine which hash slot a specific key belongs to?

Intermediate
Redis Cluster applies a hashing algorithm called CRC16 to the key, then takes the result modulo sixteen thousand three hundred eighty four to determine the specific hash slot, ensuring keys are distributed pseudo randomly but consistently across the entire cluster's available slots.
-- The hash slot calculation happens automatically
-- CRC16(key) mod 16384 determines the slot
redis-cli cluster keyslot session:abc123
Real-world example A developer troubleshooting uneven load across cluster nodes checks the hash slot calculation for their most frequently accessed keys, discovering a naming pattern that was accidentally concentrating too many keys onto a single node.

Common follow-ups: What is a hash tag, and how does it let you force related keys into the same slot?;Can you manually move a hash slot from one node to another?

Clustering;Redis Performance Tuning & Benchmarking

What is a hash tag in Redis Cluster, and why would you use one to control which slot certain keys are assigned to?

Intermediate
A hash tag is a portion of a key wrapped in curly braces, and when present, Redis Cluster only uses that specific portion for the hash slot calculation rather than the entire key, letting you deliberately force multiple related keys to land on the same slot and therefore the same node, which is required for operations that need to work with multiple keys together.
-- Both keys will be assigned to the same hash slot
-- because of the shared {user:123} hash tag
redis-cli set '{user:123}:profile' 'data'
redis-cli set '{user:123}:settings' 'data'
Real-world example An application storing both a user's profile and settings under related keys uses hash tags to guarantee both keys always land on the same node, allowing them to be safely used together in a single multi key operation.

Common follow-ups: What operations in Redis Cluster require keys to be on the same node?;What happens if you forget to use a hash tag for keys that need to be grouped together?

Clustering;Cluster Sharding & Hash Slots

How would you plan and execute resharding a Redis Cluster to add a new node and redistribute existing hash slots?

Advanced
You add the new node to the cluster, then use a resharding tool or command to move a portion of hash slots, and the keys within them, from existing nodes to the new node, carefully doing this gradually to avoid overwhelming the cluster with data migration traffic while it continues serving live production requests.
redis-cli --cluster add-node newnode:6379 existingnode:6379
redis-cli --cluster reshard existingnode:6379
Real-world example A growing e-commerce platform adds a fourth node to their three node Redis Cluster during a low traffic period, gradually resharding slots to the new node to spread out the increasing data volume without disrupting the live shopping experience.

Common follow-ups: How long does resharding typically take for a large amount of data?;What precautions should be taken before starting a resharding operation in production?

Clustering;Redis Backup & Disaster Recovery

What happens to client requests during a hash slot migration, and how does Redis Cluster handle keys that are in the middle of being moved?

Advanced
During migration, a slot briefly exists in a special migrating or importing state on the source and destination nodes, and if a client requests a key that has already moved, the node responds with a redirect telling the client exactly which node now holds that key, allowing operations to continue correctly even while migration is actively in progress.
-- A client might receive an ASK or MOVED redirect
-- during an in progress slot migration
(error) ASK 3999 destinationnode:6379
Real-world example A properly built Redis Cluster client library automatically follows ASK and MOVED redirects during a live resharding operation, ensuring the application experiences no errors even while data is actively being redistributed in the background.

Common follow-ups: What is the difference between an ASK redirect and a MOVED redirect?;Do all Redis client libraries handle these redirects automatically?

Clustering;Connection Pooling & Client Libraries

How do you check the current distribution of hash slots and keys across the nodes in a Redis Cluster to identify potential imbalances?

Intermediate
You use the CLUSTER SLOTS or CLUSTER SHARDS command to see exactly which node owns which range of hash slots, and combine this with checking key counts per node, to identify if certain nodes are holding significantly more data or slots than others, which could indicate an imbalance worth addressing through resharding.
redis-cli cluster slots
redis-cli -h node1 dbsize
redis-cli -h node2 dbsize
Real-world example A database administrator notices one node in their cluster holding twice as much data as the others after checking slot distribution and key counts, prompting a resharding operation to better balance the load.

Common follow-ups: What tools provide a more visual way to monitor hash slot distribution?;How often should cluster balance be checked in a production environment?

Redis Monitoring & Observability;Clustering