How does sharding work in Redis Cluster with hash slots?
Redis Cluster maps keys to 16384 hash slots via CRC16 mod 16384 and spreads them across nodes. Learn slot routing, MOVED redirects, and hash tags.
Expected Interview Answer
Redis Cluster shards data by mapping every key to one of 16384 hash slots using CRC16(key) mod 16384, then distributing those slots across the cluster's primary nodes so each primary owns a subset of slots.
When a client sends a command, the target slot is computed from the key and the owning node handles it; a Redis-Cluster-aware client caches the slot-to-node map and reaches the right node directly. If a node receives a key it does not own it replies with a MOVED redirection pointing to the correct node, and ASK redirections handle keys mid-migration during resharding. Multi-key commands must operate on keys in the same slot, which is achieved with hash tags like {user1}, forcing related keys into one slot.
- Even data distribution across nodes via CRC16 hashing
- Fixed 16384 slots make resharding a matter of moving slots
- Clients route directly to the owning node after learning the map
- MOVED and ASK redirections keep routing correct during changes
- Hash tags co-locate related keys for multi-key operations
AI Mentor Explanation
Hash slots are like 16384 numbered seats in a stadium; a formula turns each fan's ticket into a fixed seat number, and blocks of seats are assigned to different gate staff, so any usher instantly knows which colleague handles a given seat.
Step-by-Step Explanation
Step 1
Compute the slot
For each key, calculate CRC16(key) mod 16384 to get its hash slot number.
Step 2
Assign slots to primaries
Distribute the 16384 slots across the cluster's primary nodes so each owns a range.
Step 3
Route the command
A cluster-aware client uses its cached slot map to send the command to the owning node.
Step 4
Handle MOVED redirects
If a node does not own the slot, it returns MOVED with the correct node so the client updates its map.
Step 5
Co-locate with hash tags
Wrap a shared substring in braces, like {user1}:profile, so related keys hash to the same slot for multi-key commands.
What Interviewer Expects
- Stating the CRC16(key) mod 16384 formula
- Explaining slot-to-node assignment and client-side routing
- Describing MOVED and ASK redirections
- Understanding hash tags and multi-key slot constraints
- Knowing resharding moves slots between nodes
Common Mistakes
- Saying keys hash directly to nodes instead of to fixed slots
- Believing multi-key commands work across different slots
- Confusing MOVED (stable ownership) with ASK (mid-migration)
- Thinking the slot count changes when nodes are added
Best Answer (HR Friendly)
“Redis Cluster splits data into 16384 fixed buckets called hash slots. A formula turns each key into one bucket, and the buckets are shared out across the servers, so the system always knows which server holds any given key and can move whole buckets between servers to rebalance.”
Code Example
# Compute the slot for a key
redis-cli -c CLUSTER KEYSLOT user1
# (integer) 8106
# Without cluster mode, a wrong node replies with MOVED
redis-cli SET user1 alice
# (error) MOVED 8106 192.168.1.12:6379# Braces force the same slot -> multi-key ops allowed
redis-cli -c MSET {user1}:name alice {user1}:email [email protected]
redis-cli -c CLUSTER KEYSLOT {user1}:name
redis-cli -c CLUSTER KEYSLOT {user1}:email
# Both return the same slot number
# Move slots between nodes when resharding
redis-cli --cluster reshard 192.168.1.10:6379Follow-up Questions
- Why 16384 slots rather than a larger number?
- What is the difference between MOVED and ASK redirection?
- How do hash tags force keys into the same slot?
- What happens to slot ownership when you add a new node?
- Why do cross-slot multi-key commands fail in a cluster?
MCQ Practice
1. How is a key mapped to a hash slot in Redis Cluster?
Redis Cluster computes CRC16 of the key modulo 16384 to determine which of the fixed hash slots the key belongs to.
2. What does a MOVED reply indicate?
MOVED tells the client the slot's stable owner is a different node, so the client should update its cached slot map and retry there.
3. How do you force two keys into the same slot?
Only the substring inside the first {} pair is hashed, so keys sharing that tag land in the same slot, enabling multi-key operations.
Flash Cards
Slot formula? — CRC16(key) mod 16384 gives the key's hash slot.
How many hash slots? — Exactly 16384 fixed slots, distributed across the cluster's primaries.
MOVED vs ASK? — MOVED means stable new ownership; ASK is a temporary redirect while a slot is migrating.
What is a hash tag? — A braced substring like {user1} that forces keys sharing it into the same slot.
Continue Learning
Related Interview Questions
What is the difference between Redis replication and Redis Cluster?
medium
How does live resharding work in Redis Cluster, and what can go wrong during slot migration?
hard
How should a client library route commands in Redis Cluster, and what happens during MOVED, ASK and failover?
hard
How do you find and fix big keys and hot keys in a Redis deployment?
hard