Hash slots distribution in Redis - Time & Space Complexity
When using Redis clusters, data is split into parts called hash slots. Understanding how time grows when distributing keys into these slots helps us see how fast Redis can find or store data.
We want to know how the work changes as we add more keys to the cluster.
Analyze the time complexity of the following Redis commands for distributing keys into hash slots.
# Calculate hash slot for each key
for key in KEYS {
slot = CRC16(key) % 16384
assign key to slot
}
This code calculates which of the 16384 hash slots each key belongs to using a hash function, then assigns the key to that slot.
Look for repeated steps in the process.
- Primary operation: Calculating the hash slot for each key using CRC16 and modulo.
- How many times: Once for every key in the input list.
As you add more keys, the number of hash calculations grows directly with the number of keys.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 hash calculations |
| 100 | 100 hash calculations |
| 1000 | 1000 hash calculations |
Pattern observation: The work grows evenly as you add more keys; doubling keys doubles the work.
Time Complexity: O(n)
This means the time to assign keys to hash slots grows in direct proportion to the number of keys.
[X] Wrong: "Calculating hash slots is constant time no matter how many keys there are."
[OK] Correct: Each key needs its own hash calculation, so more keys mean more work, not the same amount.
Understanding how Redis distributes keys helps you explain how data is balanced and accessed quickly in real systems. This skill shows you can think about how systems handle growing data smoothly.
"What if we used a more complex hash function that takes longer to compute? How would the time complexity change?"
Practice
Solution
Step 1: Recall Redis Cluster design
Redis Cluster divides data into hash slots to distribute keys evenly.Step 2: Identify the number of hash slots
The standard number of hash slots in Redis Cluster is 16384.Final Answer:
16384 -> Option AQuick Check:
Redis Cluster uses 16384 hash slots [OK]
- Confusing hash slots with number of keys
- Thinking slots are 1024 or 2048
- Assuming slots equal to max integer value
Solution
Step 1: Understand Redis cluster commands
Redis provides commands to inspect cluster state and slot distribution.Step 2: Identify the command for slot distribution
TheCLUSTER SLOTScommand returns how hash slots are assigned to nodes.Final Answer:
CLUSTER SLOTS -> Option CQuick Check:
CLUSTER SLOTSshows slot distribution [OK]
- Using CLUSTER NODES which shows node info but not slots
- Confusing CLUSTER INFO with slot details
- Assuming CLUSTER KEYS exists
Solution
Step 1: Understand slot ranges per node
Node1 covers slots 0 to 5460, Node2 covers 5461 to 10922, Node3 covers 10923 to 16383.Step 2: Locate slot 7000
Slot 7000 falls between 5461 and 10922, so it belongs to Node2.Final Answer:
Node2 -> Option DQuick Check:
7000 is in Node2's slot range [OK]
- Choosing Node1 because 7000 is less than 10922
- Choosing Node3 by mistake
- Not checking slot ranges carefully
CLUSTER SLOTS but get an error: "ERR This instance has cluster support disabled". What is the likely cause?Solution
Step 1: Analyze the error message
The error says cluster support is disabled, meaning the instance is not a cluster node.Step 2: Understand cluster mode requirement
Only Redis instances running in cluster mode supportCLUSTER SLOTScommand.Final Answer:
The Redis instance is not running in cluster mode -> Option AQuick Check:
Cluster commands need cluster mode enabled [OK]
- Assuming syntax error causes this
- Thinking slots are unassigned causes error
- Blaming Redis version without checking mode
Solution
Step 1: Calculate slots per node
16384 slots divided by 4 nodes = 4096 slots per node.Step 2: Determine Node3's slot range
Node1: 0-4095, Node2: 4096-8191, Node3: 8192-12287, Node4: 12288-16383.Final Answer:
8192 to 12287 -> Option BQuick Check:
Each node gets 4096 slots; Node3 starts at 8192 [OK]
- Off-by-one errors in slot ranges
- Assigning wrong ranges to nodes
- Not dividing slots evenly
Ready to prove it?
Pass the test and this pattern moves unknown
