Skip to main content

How are slots distributed among nodes?

In Redis Cluster, every key is distributed across 16384 hash slots, and those slots are split among the master nodes.

1. Hash slots

  • There are 16384 slots in total (0-16383).

  • Every key lands in a slot via this formula:

    javascript
    CRC16(key) % 16384
  • The slot determines which master holds the key.

2. Distributing slots among master nodes

  • When the cluster is created, slots are spread evenly across all master nodes. An example with 3 masters:

    javascript
    master1 → slots 0-5460 master2 → slots 5461-10922 master3 → slots 10923-16383
  • If a new master is added, some slots get moved from the existing masters onto it.

3. Replicas

  • Replicas don't receive slots directly.
  • They simply copy their master's data.
  • So a read request to a replica only works with the data of the master it's subordinate to.

4. Redistribution during scaling

  • Redis Cluster supports resharding:
    • some slots move from an overloaded master to a new one;
    • the data for those slots moves automatically;
    • clients get notified about the slots' new location via MOVED.

5. Summary

  • Slots are a logical partitioning of keys.
  • Every master serves a specific range of slots.
  • Replicas copy their master's slots.
  • Adding new nodes or removing old ones comes with a redistribution of slots to balance the load.

This mechanism provides horizontal scaling, even load, and fault tolerance.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.