Redis path · Keeping it running · Topic 10 of 11

Redis Cluster and hash slots

Not started

One Redis process runs commands on one core and holds its data in one machine's memory. Redis Cluster spreads data across several primaries, each with its own replicas, without a proxy in between. Instead of a hash ring, the key space is split into 16,384 fixed hash slots. Each primary owns some of the slots, and moving a slot from one node to another is how a cluster is resized.

Why it matters: Cluster lets Redis grow past one machine, but it changes which multi-key operations work and how clients talk to it.

  1. 1Keys map to slots

    Every key belongs to one of 16,384 hash slots, computed as CRC16 of the key modulo 16384; each primary owns a set of slots.

  2. 2Hash tags group keys

    If a key contains {...}, only the part inside the braces is hashed, so keys with the same tag land in the same slot.

  3. 3Multi-key commands need one slot

    MGET, MULTI/EXEC across keys and Lua scripts only work when all their keys hash to the same slot.

  4. 4Clients follow redirects

    A node that does not own a key's slot replies MOVED (or ASK during resharding); cluster-aware clients cache the slot map.

1

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:

⌘/Ctrl + Enter
OutputNot run yet

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) 15880

user: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:6379

Cluster-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.

2

Check yourself

3 questions. Get them all right to finish this topic.

  1. 1.In a cluster, MGET user:42 user:43 fails with CROSSSLOT. Why?
  2. 2.Which keys are guaranteed to be in the same slot?
  3. 3.All keys of a large multi-tenant system use the hash tag {tenant}, with the same literal text. What goes wrong?

0 of 3 right so far. Get all three to finish this topic.