What if you could balance huge amounts of data across servers without lifting a finger?
Why Resharding hash slots in Redis? - Purpose & Use Cases
Imagine you have a huge collection of toys spread across several boxes. When one box gets too full or another box is almost empty, you want to move some toys around to balance the load. Doing this by hand means opening each box, counting toys, and moving them one by one.
Manually moving toys is slow and tiring. You might lose track of which toys belong where, or accidentally drop some. It's hard to keep everything balanced and organized without mistakes, especially as the number of toys grows.
Resharding hash slots in Redis automatically redistributes data across servers. It moves data in chunks called slots, keeping everything balanced and consistent without manual counting or moving. This makes scaling smooth and error-free.
Manually move keys one by one between servers
Use Redis commands to reshard hash slots automatically
It enables seamless scaling and balancing of data across servers without downtime or data loss.
A growing online game stores player data on multiple servers. When one server gets crowded, resharding hash slots moves some players' data to a less busy server automatically, keeping the game fast and smooth.
Manual data movement is slow and error-prone.
Resharding hash slots automates balanced data distribution.
This keeps systems scalable, reliable, and efficient.
Practice
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 AQuick Check:
Resharding = balance data [OK]
- Thinking resharding deletes keys
- Confusing resharding with backup
- Assuming hash slot size changes
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 BQuick Check:
MIGRATING slot = CLUSTER SETSLOT MIGRATING [OK]
- Using ADDSLOTS or DELSLOTS instead of SETSLOT
- Confusing INFO command with slot state change
- Omitting node_id in command
1. CLUSTER SETSLOT 5000 MIGRATING nodeB_id 2. MIGRATE nodeB_ip 6379 "" 0 5000 3. CLUSTER SETSLOT 5000 IMPORTING nodeA_id 4. CLUSTER SETSLOT 5000 NODE nodeB_id
Solution
Step 1: Mark slot as migrating on source node
Use CLUSTER SETSLOT 5000 MIGRATING nodeB_id to mark slot migrating from node A.Step 2: Mark slot as importing on destination node
Use CLUSTER SETSLOT 5000 IMPORTING nodeA_id on node B to accept keys.Step 3: Migrate keys with MIGRATE command
Use MIGRATE to move keys of slot 5000 from node A to node B.Step 4: Assign slot ownership to destination node
Use CLUSTER SETSLOT 5000 NODE nodeB_id to finalize ownership.Final Answer:
1, 3, 2, 4 -> Option DQuick Check:
Mark migrating -> mark importing -> migrate keys -> assign slot [OK]
- Migrating keys before marking IMPORTING
- Assigning slot ownership before migration
- Skipping IMPORTING step
CLUSTER SETSLOT 7000 MIGRATING nodeB_id MIGRATE nodeB_ip 6379 "" 0 7000 CLUSTER SETSLOT 7000 NODE nodeB_idWhat is missing to fix the 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 AQuick Check:
IMPORTING state needed on destination [OK]
- Skipping IMPORTING step
- Deleting slots prematurely
- Restarting nodes unnecessarily
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 CQuick Check:
MIGRATING -> IMPORTING -> MIGRATE -> ASSIGN [OK]
- Assigning slots before migration
- Migrating keys without marking IMPORTING
- Deleting slots before migration
Ready to prove it?
Pass the test and this pattern moves unknown
