📖 Scenario: You are managing a Redis cluster that distributes data across multiple nodes using hash slots. To balance the load, you need to move some hash slots from one node to another.This project will guide you through the steps to reshard hash slots manually using Redis commands.
🎯 Goal: Learn how to reshard hash slots by moving a specific range of slots from one Redis node to another, ensuring data consistency and cluster stability.
📋 What You'll Learn
Create a list of hash slots assigned to the source node
Define the target node ID to receive the hash slots
Use Redis commands to migrate keys belonging to the hash slots
Update the cluster configuration to assign the hash slots to the target node
💡 Why This Matters
🌍 Real World
Redis clusters use hash slots to distribute data. Resharding hash slots helps balance load and improve performance.
💼 Career
Database administrators and backend engineers often need to reshard Redis clusters to maintain system health and scalability.
Progress0 / 4 steps
1
Set up the list of hash slots to move
Create a variable called slots_to_move that contains the list of hash slots from 1000 to 1010 inclusive.
Redis
Hint
Use the range function to create a list of numbers from 1000 to 1010.
2
Define the target node ID
Create a variable called target_node_id and set it to the string 'node-02' which represents the Redis node that will receive the hash slots.
Redis
Hint
Assign the string 'node-02' to the variable target_node_id.
3
Generate commands to migrate keys for each hash slot
Write a for loop using slot as the iterator over slots_to_move. Inside the loop, create a Redis command string called migrate_command that uses the format "CLUSTER SETSLOT {slot} MIGRATING {target_node_id}".
Redis
Hint
Use an f-string to format the command with the current slot and target_node_id.
4
Assign the hash slots to the target node
After migrating keys, write a for loop over slots_to_move using slot as the iterator. Inside the loop, create a Redis command string called assign_command with the format "CLUSTER SETSLOT {slot} NODE {target_node_id}" to assign each slot to the target node.
Redis
Hint
Use a second loop to assign each slot to the target node with the correct command format.
Practice
(1/5)
1. What is the main purpose of resharding hash slots in a Redis cluster?
easy
A. To balance data evenly across cluster nodes
B. To delete unused keys from the database
C. To backup data to an external storage
D. To increase the size of each hash slot
Solution
Step 1: Understand hash slots in Redis cluster
Redis cluster divides data into 16384 hash slots distributed among nodes.
Step 2: Purpose of resharding
Resharding moves hash slots to balance load and storage evenly across nodes.
Final Answer:
To balance data evenly across cluster nodes -> Option A
Quick Check:
Resharding = balance data [OK]
Hint: Resharding means redistributing data evenly [OK]
Common Mistakes:
Thinking resharding deletes keys
Confusing resharding with backup
Assuming hash slot size changes
2. Which Redis command is used to mark a hash slot as migrating during resharding?
easy
A. CLUSTER ADDSLOTS <slot>
B. CLUSTER SETSLOT <slot> MIGRATING <node_id>
C. CLUSTER DELSLOTS <slot>
D. CLUSTER INFO
Solution
Step 1: Identify commands related to slot state changes
CLUSTER SETSLOT command changes slot ownership or state.
Step 2: Marking migrating slot
Using MIGRATING flag with CLUSTER SETSLOT marks slot as migrating from a node.
Final Answer:
CLUSTER SETSLOT <slot> MIGRATING <node_id> -> Option B
Quick Check:
MIGRATING slot = CLUSTER SETSLOT MIGRATING [OK]
Hint: Use CLUSTER SETSLOT with MIGRATING to mark slot move [OK]
Common Mistakes:
Using ADDSLOTS or DELSLOTS instead of SETSLOT
Confusing INFO command with slot state change
Omitting node_id in command
3. Given the following commands during resharding, what is the correct order to migrate a hash slot 5000 from node A to node B?
A. Mark slot as IMPORTING on node B before MIGRATE
B. Run CLUSTER DELSLOTS 7000 on node A first
C. Use CLUSTER ADDSLOTS 7000 on node B after migration
D. Restart both nodes before migration
Solution
Step 1: Identify missing IMPORTING state
Destination node must mark slot as IMPORTING before receiving keys.
Step 2: Add missing command
Use CLUSTER SETSLOT 7000 IMPORTING nodeA_id on node B before MIGRATE.
Final Answer:
Mark slot as IMPORTING on node B before MIGRATE -> Option A
Quick Check:
IMPORTING state needed on destination [OK]
Hint: Always mark IMPORTING on target node before MIGRATE [OK]
Common Mistakes:
Skipping IMPORTING step
Deleting slots prematurely
Restarting nodes unnecessarily
5. During a large-scale resharding, you want to move multiple hash slots from node A to node B without downtime. Which sequence of actions ensures data consistency and minimal disruption?
hard
A. Assign slots to node B first, then migrate keys, finally mark MIGRATING on node A
B. Migrate keys first, then mark slots IMPORTING on node B, finally mark MIGRATING on node A
C. Mark slots MIGRATING on node A, mark IMPORTING on node B, migrate keys, then assign slots to node B
D. Delete slots on node A, migrate keys, then assign slots to node B
Solution
Step 1: Mark slots as MIGRATING on source node
This prevents writes to those slots on node A during migration.
Step 2: Mark slots as IMPORTING on destination node
Allows node B to accept keys for those slots.
Step 3: Migrate keys using MIGRATE command
Safely transfer keys from node A to node B.
Step 4: Assign slots ownership to node B
Finalizes the slot ownership after migration completes.
Final Answer:
Mark slots MIGRATING on node A, mark IMPORTING on node B, migrate keys, then assign slots to node B -> Option C
Quick Check:
MIGRATING -> IMPORTING -> MIGRATE -> ASSIGN [OK]
Hint: Follow MIGRATING, IMPORTING, MIGRATE, then assign slots [OK]
Common Mistakes:
Assigning slots before migration
Migrating keys without marking IMPORTING
Deleting slots before migration
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.