If you've used standalone Redis and then switched to Redis Cluster, you may have encountered this error:
CROSSSLOT Keys in request don't hash to the same slot
Enter fullscreen mode
Exit fullscreen mode
Why does the same code work locally but fail in Redis Cluster?
Let's understand how Redis distributes keys and why certain operations require keys to share the same hash slot.
1. What Are Redis Hash Slots?
Standalone Redis stores all keys on a single server. Redis Cluster distributes them across multiple primary nodes using 16,384 hash slots .
Each key is assigned a slot using:
HASH_SLOT = CRC16(key) % 16384
Enter fullscreen mode
Exit fullscreen mode
For example (illustrative slot numbers):
Redis Cluster | +-- Node A (Slots 0-5460) | +-- user:1 | +-- Node B (Slots 5461-10922) | +-- user:2 | +-- Node C (Slots 10923-16383) +-- user:3
Enter fullscreen mode
Exit fullscreen mode
Why use hash slots instead of assigning keys directly to nodes?
Scalability and rebalancing.
When adding or removing…