Resharding hash slots in Redis - Time & Space Complexity
When Redis redistributes hash slots between nodes, it moves keys from one place to another.
We want to know how the time to reshard grows as the number of keys changes.
Analyze the time complexity of the following redis commands used in resharding.
# Move keys from source node to target node
for each slot in slots_to_move:
keys = redis-cli --cluster getkeysinslot slot count
for each key in keys:
redis-cli --cluster MIGRATE target_host target_port key 0 5000
This code moves all keys in certain hash slots from one node to another.
Look for loops or repeated actions.
- Primary operation: Moving each key in the slots one by one.
- How many times: Once for every key in the slots being moved.
As the number of keys to move grows, the time to move them grows too.
| Input Size (n keys) | Approx. Operations |
|---|---|
| 10 | About 10 moves |
| 100 | About 100 moves |
| 1000 | About 1000 moves |
Pattern observation: The time grows roughly in direct proportion to the number of keys moved.
Time Complexity: O(n)
This means the time to reshard grows linearly with the number of keys moved.
[X] Wrong: "Resharding time depends only on the number of slots, not keys."
[OK] Correct: Each slot can have many keys, so moving slots means moving all their keys, which takes time proportional to key count.
Understanding how redis resharding scales helps you explain system behavior and design better data distribution strategies.
"What if we batch move keys instead of one by one? How would the time complexity change?"
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
