📖 Scenario: You are managing a Redis cluster that distributes keys across multiple nodes using hash slots. Understanding how keys map to hash slots helps in balancing the load and troubleshooting data distribution.
🎯 Goal: Build a simple Redis key-to-hash slot mapping using a dictionary to simulate hash slot distribution. You will create the data structure, configure the number of hash slots, compute the hash slot for each key, and finalize the distribution map.
📋 What You'll Learn
Create a dictionary called keys with these exact entries: 'user:1001': 'Alice', 'user:1002': 'Bob', 'order:5001': 'Pending', 'order:5002': 'Shipped'
Create a variable called total_slots and set it to 16384
Create a dictionary called slot_distribution that maps each key to its hash slot using the formula: hash(key) % total_slots
Add a final line that sets a variable distribution_complete to True
💡 Why This Matters
🌍 Real World
Redis clusters use hash slots to distribute data evenly across multiple nodes, improving performance and scalability.
💼 Career
Understanding hash slot distribution is essential for database administrators and backend developers working with Redis clusters to optimize data storage and retrieval.
Progress0 / 4 steps
1
Create the initial keys dictionary
Create a dictionary called keys with these exact entries: 'user:1001': 'Alice', 'user:1002': 'Bob', 'order:5001': 'Pending', 'order:5002': 'Shipped'
Redis
Hint
Use curly braces {} to create a dictionary with the exact key-value pairs.
2
Set the total number of hash slots
Create a variable called total_slots and set it to 16384
Redis
Hint
Assign the integer 16384 to the variable total_slots.
3
Compute hash slot for each key
Create a dictionary called slot_distribution that maps each key in keys to its hash slot using the formula: hash(key) % total_slots
Redis
Hint
Use a dictionary comprehension to map each key to hash(key) % total_slots.
4
Mark distribution as complete
Add a final line that sets a variable distribution_complete to True
Redis
Hint
Simply assign True to the variable distribution_complete.
Practice
(1/5)
1. What is the total number of hash slots used by Redis Cluster to distribute data?
easy
A. 16384
B. 1024
C. 65536
D. 2048
Solution
Step 1: Recall Redis Cluster design
Redis Cluster divides data into hash slots to distribute keys evenly.
Step 2: Identify the number of hash slots
The standard number of hash slots in Redis Cluster is 16384.
2. Which Redis command shows the current distribution of hash slots across cluster nodes?
easy
A. CLUSTER NODES
B. CLUSTER KEYS
C. CLUSTER SLOTS
D. CLUSTER INFO
Solution
Step 1: Understand Redis cluster commands
Redis provides commands to inspect cluster state and slot distribution.
Step 2: Identify the command for slot distribution
The CLUSTER SLOTS command returns how hash slots are assigned to nodes.
Final Answer:
CLUSTER SLOTS -> Option C
Quick Check:
CLUSTER SLOTS shows slot distribution [OK]
Hint: Use CLUSTER SLOTS to see slot assignments [OK]
Common Mistakes:
Using CLUSTER NODES which shows node info but not slots
Confusing CLUSTER INFO with slot details
Assuming CLUSTER KEYS exists
3. Given a Redis Cluster with 3 nodes and the following slot ranges: Node1: 0-5460, Node2: 5461-10922, Node3: 10923-16383. Which node holds the key with hash slot 7000?
medium
A. None of the above
B. Node1
C. Node3
D. Node2
Solution
Step 1: Understand slot ranges per node
Node1 covers slots 0 to 5460, Node2 covers 5461 to 10922, Node3 covers 10923 to 16383.
Step 2: Locate slot 7000
Slot 7000 falls between 5461 and 10922, so it belongs to Node2.
Final Answer:
Node2 -> Option D
Quick Check:
7000 is in Node2's slot range [OK]
Hint: Check which node's slot range includes the slot number [OK]
Common Mistakes:
Choosing Node1 because 7000 is less than 10922
Choosing Node3 by mistake
Not checking slot ranges carefully
4. You run CLUSTER SLOTS but get an error: "ERR This instance has cluster support disabled". What is the likely cause?
medium
A. The Redis instance is not running in cluster mode
B. The command syntax is incorrect
C. The cluster has no slots assigned yet
D. The Redis version is too new
Solution
Step 1: Analyze the error message
The error says cluster support is disabled, meaning the instance is not a cluster node.
Step 2: Understand cluster mode requirement
Only Redis instances running in cluster mode support CLUSTER SLOTS command.
Final Answer:
The Redis instance is not running in cluster mode -> Option A
Quick Check:
Cluster commands need cluster mode enabled [OK]
Hint: Check if Redis is started with cluster enabled [OK]
Common Mistakes:
Assuming syntax error causes this
Thinking slots are unassigned causes error
Blaming Redis version without checking mode
5. In a Redis Cluster, if you want to evenly distribute 16384 hash slots across 4 nodes, which slot range should Node3 handle?
hard
A. 12288 to 16383
B. 8192 to 12287
C. 4096 to 8191
D. 0 to 4095
Solution
Step 1: Calculate slots per node
16384 slots divided by 4 nodes = 4096 slots per node.