Typorithm · hard

Cluster & Sharding

redis-clusterredisclusterhashslotshard
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Cluster & Sharding

0%

Persistence & Scale

Redis Cluster shards data across 16384 hash slots; a key maps to a slot via CRC16 mod 16384 (CLUSTER KEYSLOT shows it). Each master owns a slot range, and CLUSTER NODES/SLOTS/INFO introspect the topology while ADDSLOTS assigns slots. Multi-key operations must share a slot, which you force with hash tags like {user:1}.

Redis Cluster partitions the keyspace into 16384 hash slots and spreads them across master nodes. A key's slot is CRC16 of the key modulo 16384, and CLUSTER KEYSLOT user:1 shows exactly which slot a given key lands in.

Each master owns a contiguous range of slots. CLUSTER INFO reports cluster health, CLUSTER NODES lists every node with its slot ranges, and CLUSTER SLOTS returns the slot-to-node mapping. CLUSTER ADDSLOTS 0 1 2 assigns specific slots to the current node when you build or rebalance a cluster.

Because a multi-key command only works if all keys live in the same slot, you use hash tags: wrapping part of the key in braces, like {user:1}:profile and {user:1}:cart, makes only the braced portion hash, forcing those keys into one slot.

01

Cluster shards data across 16384 hash slots.

02

Key slot = CRC16(key) mod 16384 (CLUSTER KEYSLOT).

03

Multi-key ops need one slot — use hash tags like {user:1}.

CLUSTER INFO
CLUSTER NODES
CLUSTER KEYSLOT user:1
CLUSTER ADDSLOTS 0 1 2
CLUSTER SLOTS

Read the implementation

Connect each part of the code to the concept before you type.

1. Cluster state

CLUSTER INFO reports overall cluster health, such as whether the cluster is up and how many slots are assigned.

CLUSTER INFO

2. Node topology

CLUSTER NODES lists every node, its role, and the hash-slot ranges it owns.

CLUSTER NODES

3. Key to slot

CLUSTER KEYSLOT shows which of the 16384 slots a key maps to via CRC16 mod 16384.

CLUSTER KEYSLOT user:1

4. Assign slots

CLUSTER ADDSLOTS assigns the listed hash slots to the current node.

CLUSTER ADDSLOTS 0 1 2

5. Slot mapping

CLUSTER SLOTS returns the mapping of slot ranges to the masters and replicas serving them.

CLUSTER SLOTS

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Cluster & Sharding: Lesson & Practice · Typorithm | ApplyDjinn