0
0
Redisquery~7 mins

Resharding hash slots in Redis

Choose your learning style9 modes available
Introduction
Resharding hash slots helps balance data across Redis cluster nodes so no single node gets too busy or full.
When adding a new node to a Redis cluster and you want to move some data to it.
When some nodes have too many keys and others have fewer, causing uneven load.
When you want to improve performance by spreading data more evenly.
When removing a node and you need to move its data to other nodes.
Syntax
Redis
CLUSTER SETSLOT <slot> IMPORTING <node_id>
CLUSTER SETSLOT <slot> MIGRATING <node_id>
CLUSTER GETKEYSINSLOT <slot> <count>
MIGRATE <host> <port> <key> 0 <timeout>
You move hash slots one by one between nodes using IMPORTING and MIGRATING states.
Use CLUSTER GETKEYSINSLOT to find keys in a slot before migrating.
Examples
Mark slot 121 as migrating from the current node to the node with id 07c3...
Redis
CLUSTER SETSLOT 121 MIGRATING 07c37dfeb235512dbd9f8d7a0e8f0f8a0e7a1f3a
Mark slot 121 as importing on the node with id 07c3...
Redis
CLUSTER SETSLOT 121 IMPORTING 07c37dfeb235512dbd9f8d7a0e8f0f8a0e7a1f3a
Get up to 10 keys that belong to slot 121 to migrate them.
Redis
CLUSTER GETKEYSINSLOT 121 10
Move key1 to the node at 192.168.1.2 port 6379 with a 5-second timeout.
Redis
MIGRATE 192.168.1.2 6379 "key1" 0 5000
Sample Program
This example moves slot 121 from the current node to the node with id 07c3... It marks the slot as migrating and importing, gets keys in the slot, migrates a key, then assigns the slot to the new node.
Redis
CLUSTER SETSLOT 121 MIGRATING 07c37dfeb235512dbd9f8d7a0e8f0f8a0e7a1f3a
CLUSTER SETSLOT 121 IMPORTING 07c37dfeb235512dbd9f8d7a0e8f0f8a0e7a1f3a
CLUSTER GETKEYSINSLOT 121 2
MIGRATE 192.168.1.2 6379 "user:1001" 0 5000
CLUSTER SETSLOT 121 NODE 07c37dfeb235512dbd9f8d7a0e8f0f8a0e7a1f3a
OutputSuccess
Important Notes
Always migrate keys in small batches to avoid blocking the server.
Make sure to update the slot ownership after migrating keys to keep cluster state consistent.
Use the node IDs from CLUSTER NODES command to identify nodes.
Summary
Resharding hash slots balances data across Redis cluster nodes.
Use CLUSTER SETSLOT with MIGRATING and IMPORTING to mark slot movement.
Migrate keys with MIGRATE command and update slot ownership after.