How it works
The explanation, with examples you can run.
From key to slot
The slot is CRC16(key) mod 16384, using the CRC16-XMODEM variant. If the key contains a {, followed later by a } with at least one character between them, only that part is hashed. This plain-JS version gives the same answers as CLUSTER KEYSLOT on a real cluster-enabled server:
Press Run to see what your code prints, in order.
Runs in this browser, in an isolated frame with no access to this page. console.log, info, warn and error are shown; code runs as a classic script, so use an async function for await.
> CLUSTER KEYSLOT user:42(integer) 15880> CLUSTER KEYSLOT {user:42}:cart(integer) 15880user:42 and user:43 land in different slots; {user:42}:cart and {user:42}:profile both hash user:42, so they share one.
What changes for your code
Multi-key operations need one slot. MGET, MSET, SUNION, a MULTI/EXEC touching several keys, and a Lua script with several KEYS all fail with CROSSSLOT unless every key is in the same slot. Hash tags are how you put related keys together. Use them for data that is genuinely accessed together, and tag with something that has many values (a user or tenant id), or one slot, and one node, gets everything.
Clients follow redirects. Any node accepts a connection, but if it does not own a key's slot it replies with a redirect:
-MOVED 15880 10.0.0.7:6379Cluster-aware client libraries learn the slot map, send each command to the right node, and refresh the map on MOVED. During resharding, a slot being migrated answers ASK for keys that have already moved, for that one request only. With redis-cli, pass -c to follow redirects.
One database. Only database 0 exists; SELECT to another number is not allowed.
Failover and consistency
Each primary has replicas. Nodes gossip with each other; if a majority of primaries agree that one is unreachable, one of its replicas is promoted. Replication is still asynchronous, so the cluster specification says plainly that acknowledged writes can be lost during failovers and partitions. A primary on the minority side of a partition stops accepting writes after cluster-node-timeout, which bounds the loss. By default, if any slot has no reachable owner, the cluster stops serving queries and replies with CLUSTERDOWN errors (cluster-require-full-coverage yes); setting it to no keeps the covered slots available.
Sizing
The specification suggests a practical maximum on the order of 1,000 nodes. In practice: enough primaries that each holds a comfortable share of memory and load, at least one replica per primary on a different machine, and keys designed so that the operations you need stay within one slot.
Check yourself
3 questions. Get them all right to finish this topic.
0 of 3 right so far. Get all three to finish this topic.