How does Redis Cluster sharding and the hash slot algorithm work in detail?
Answer
Redis Cluster divides the keyspace into exactly 16,384 hash slots. For a given key, the slot is computed as CRC16(keyname) % 16384. If the key contains a hash tag (text inside {}), only the content within the braces is hashed — this is the mechanism for co-locating related keys. Slots are assigned to master nodes in ranges; when a node is added or removed, slots are migrated atomically using the CLUSTER SETSLOT MIGRATING and IMPORTING states. During migration, the server returns an ASK redirect for keys in a migrating slot, which differs from a permanent MOVED redirect. Smart clients maintain a slot-to-node map, updating it on MOVED responses. The choice of 16,384 (not a larger number) was a deliberate trade-off: the cluster gossip protocol heartbeat includes the full slot bitmap — at 16,384 bits, this is 2KB, an acceptable overhead; at 65,536 it would be 8KB, too large for frequent gossip messages between all nodes.
Previous
How do you work around cross-slot transaction limitations in Redis Cluster?
Next
What is consistent hashing and how does it compare to Redis Cluster's approach?
More Redis Questions
View all →- Advanced What is consistent hashing and how does it compare to Redis Cluster's approach?
- Advanced What is memory optimization in Redis (ziplist, listpack, and encoding thresholds)?
- Advanced How does AOF rewrite work and how does it compare to RDB performance?
- Advanced What are Redis Functions (replacing Lua scripts in Redis 7+)?
- Advanced What is Redis Stack and what does it add to vanilla Redis?