0
0
Redisquery~30 mins

Resharding hash slots in Redis - Mini Project: Build & Apply

Choose your learning style9 modes available
Resharding Hash Slots in Redis Cluster
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use a second loop to assign each slot to the target node with the correct command format.