Command Palette

Search for a command to run...

Hectal
PHASE 11Advanced ~10 min· topic 1 of 5

Topic 11.1

Cluster Architecture: Slots, Nodes, and the Cluster Bus

In one line

Redis Cluster splits the keyspace into 16,384 hash slots: slot = CRC16(key) mod 16384. Each primary owns a range of slots, replicas copy their primary, and nodes gossip over a cluster bus to agree on topology and detect failures. Clients cache the slot map and talk directly to the right node.

0/5 · 0%

Think of it like this

A library with 16,384 numbered shelves spread across several rooms. The shelf number comes from a formula on the book's title, so anyone can compute where a book lives, and each room knows which shelves it currently holds. Moving shelves between rooms rebalances the library without renaming any books.

Key ideas

  1. 01

    Key → slot: CRC16(key) mod 16384 (CRC16-XMODEM). CLUSTER KEYSLOT key shows it. Slots, not keys, are the unit of ownership and migration, so rebalancing moves whole slots.

  2. 02

    Nodes: each primary owns some slots and has zero or more replicas. A common production shape is 3–N primaries with 1–2 replicas each, spread across availability zones so a primary and its replicas never share a zone.

  3. 03

    Cluster bus: every node opens a second port (client port + 10000, for example 16379) and gossips with others using a binary protocol: heartbeats, slot ownership, failure reports and configuration epochs. The slot bitmap in each heartbeat (16,384 bits = 2 KB) is one reason the slot count is fixed at 16,384, and the recommended maximum is around 1,000 nodes.

  4. 04

    Clients are cluster-aware: they fetch the slot map (CLUSTER SHARDS, or the older CLUSTER SLOTS), send each command straight to the owning node, and update the map when they receive MOVED. There's no proxy in the data path, which keeps latency low.

  5. 05

    Limits compared to standalone: only database 0 (on Redis; newer Valkey versions add multi-db support), multi-key operations only within one slot, and KEYS/SCAN/FLUSHALL act per node, so tools must iterate all primaries.

  6. 06

    Why 16,384 slots and not consistent hashing: slots give fine-grained, explicit ownership that's easy to move and cheap to gossip, and they allow up to 16,384 shards. The slot map is the single source of truth, with no virtual-node rings to compute.

Code & diagrams

key-to-node.mermaiddiagram
Rendering diagram…
create-cluster.shbash
# 6 nodes started with: cluster-enabled yes, cluster-config-file nodes.conf, cluster-node-timeout 15000
redis-cli --cluster create 10.0.0.1:6379 10.0.0.2:6379 10.0.0.3:6379 \
  10.0.0.4:6379 10.0.0.5:6379 10.0.0.6:6379 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.0.0.5:6379 to 10.0.0.1:6379
...
[OK] All 16384 slots covered.

redis-cli -c -h 10.0.0.1 CLUSTER KEYSLOT user:123:cart
(integer) 9189
redis-cli -c -h 10.0.0.1 SET user:123:cart x
-> Redirected to slot [9189] located at 10.0.0.2:6379
OK
redis-cli -h 10.0.0.1 CLUSTER INFO | head -4
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_known_nodes:6

Interview problem

The problem

A dataset too big for one Redis

Your single Redis holds 180 GB and serves 600K ops/sec; it can't grow further. Design a Redis Cluster: how keys map to nodes, how many shards, how clients find data, and what changes for the application.

You're given

  • 180 GB, growing 30% a year
  • 600K ops/sec
  • 3 availability zones
  • Some Lua scripts touch several keys

The interviewer follows up

01

Why not put a proxy (like Twemproxy or Envoy) in front instead?

When it breaks

Client library not in cluster mode

What you see

Commands hit whichever node the client connected to; most return MOVED errors that the app treats as failures.

Fix & prevent

Use the cluster client (JedisCluster, Lettuce RedisClusterClient, redis-py RedisCluster, redis-cli -c) everywhere.

Explain it without notes

01

Trace a key from its name to the node that stores it.

02

Why does Redis Cluster use 16,384 slots?

Practice

01

Create a 6-node cluster in Docker with --cluster-replicas 1 and check which node holds user:1 and user:2.

Trade-offs

  • ↔

    Cluster scales memory and throughput linearly, at the cost of multi-key restrictions and cluster-aware clients.

Done when you can

  • I can compute and explain slot ownership.

  • I can size shards and replicas for a cluster.

  • I know what application changes Cluster requires.