Cluster creation in Redis - Time & Space Complexity
When creating a Redis cluster, it's important to understand how the time needed grows as the cluster size increases.
We want to know how the work changes when we add more nodes or slots to the cluster.
Analyze the time complexity of the following Redis cluster creation commands.
CLUSTER MEET 192.168.1.2 7001
CLUSTER MEET 192.168.1.3 7002
CLUSTER ADDSLOTS 0-5460
CLUSTER ADDSLOTS 5461-10922
CLUSTER ADDSLOTS 10923-16383
This code connects nodes and assigns hash slots to each node to form a cluster.
Look for repeated actions that take time.
- Primary operation: Assigning hash slots to nodes using CLUSTER ADDSLOTS.
- How many times: Each slot (up to 16384) is assigned once, so the operation repeats for each slot.
As the number of slots or nodes grows, the time to assign slots grows roughly in direct proportion.
| Input Size (slots) | Approx. Operations |
|---|---|
| 10 | 10 slot assignments |
| 100 | 100 slot assignments |
| 1000 | 1000 slot assignments |
Pattern observation: Doubling the number of slots roughly doubles the work needed to assign them.
Time Complexity: O(n)
This means the time to create the cluster grows linearly with the number of slots assigned.
[X] Wrong: "Adding more nodes will not affect the time because slots are assigned independently."
[OK] Correct: Even though slots are assigned per node, the total number of slots remains fixed, so the total work depends on all slots assigned across nodes.
Understanding how cluster creation scales helps you design systems that grow smoothly and avoid surprises when adding more nodes.
"What if we changed the number of hash slots from 16384 to a smaller number? How would the time complexity change?"
Practice
Solution
Step 1: Understand Redis cluster purpose
Redis cluster is designed to distribute data across multiple servers to improve availability and performance.Step 2: Compare options
Only To spread data across multiple servers for better safety and speed correctly describes spreading data for safety and speed; others are incorrect or unrelated.Final Answer:
To spread data across multiple servers for better safety and speed -> Option AQuick Check:
Redis cluster = data spread for safety and speed [OK]
- Thinking cluster stores data on one server only
- Confusing cluster with manual backup
- Assuming cluster only increases instance size
Solution
Step 1: Recall Redis cluster creation command
The correct command uses 'redis-cli --cluster create' followed by node addresses with ports.Step 2: Match options with correct syntax
redis-cli --cluster create 192.168.1.1:7000 192.168.1.2:7000 192.168.1.3:7000 matches the correct syntax; others use wrong keywords or formats.Final Answer:
redis-cli --cluster create 192.168.1.1:7000 192.168.1.2:7000 192.168.1.3:7000 -> Option CQuick Check:
Correct syntax = redis-cli --cluster create + nodes [OK]
- Omitting --cluster flag
- Using 'create cluster' instead of '--cluster create'
- Not specifying ports with IPs
redis-cli --cluster create 10.0.0.1:7000 10.0.0.2:7000 10.0.0.3:7000 --cluster-replicas 1What will be the effect of the
--cluster-replicas 1 option?Solution
Step 1: Understand --cluster-replicas option
This option sets how many replica nodes each master will have in the cluster.Step 2: Interpret the value 1
Setting it to 1 means each master node gets exactly one replica node for redundancy.Final Answer:
Each master node will have one replica node for backup -> Option BQuick Check:
--cluster-replicas 1 = one replica per master [OK]
- Thinking replicas are optional or manual
- Assuming no replicas with --cluster-replicas 1
- Confusing replicas with number of masters
redis-cli --cluster create 192.168.0.1:7000 192.168.0.2:7000 --cluster-replicas 1and get an error. What is the most likely cause?
Solution
Step 1: Recall cluster node requirements
Redis cluster requires at least 3 master nodes to function properly.Step 2: Analyze given nodes and replicas
With only 2 nodes and replicas set to 1, there aren't enough masters to assign replicas.Final Answer:
You need at least 3 master nodes to create a cluster with replicas -> Option DQuick Check:
Minimum 3 masters needed for replicas [OK]
- Trying to create cluster with too few nodes
- Assuming IP format causes error
- Forgetting replicas need masters
Solution
Step 1: Understand cluster replicas option
--cluster-replicas 1 means each master has one replica, so 3 masters + 3 replicas = 6 nodes total.Step 2: Check command correctness
redis-cli --cluster create 10.1.1.1:7000 10.1.1.2:7000 10.1.1.3:7000 10.1.1.4:7000 10.1.1.5:7000 10.1.1.6:7000 --cluster-replicas 1 lists 6 nodes and sets replicas to 1, matching the requirement. redis-cli --cluster create 10.1.1.1:7000 10.1.1.2:7000 10.1.1.3:7000 --cluster-replicas 3 has too few nodes. redis-cli --cluster create 10.1.1.1:7000 10.1.1.2:7000 10.1.1.3:7000 10.1.1.4:7000 10.1.1.5:7000 10.1.1.6:7000 --cluster-replicas 3 sets replicas to 3, which is invalid for 3 masters. redis-cli --cluster create 10.1.1.1:7000 10.1.1.2:7000 10.1.1.3:7000 10.1.1.4:7000 10.1.1.5:7000 10.1.1.6:7000 lacks replicas setting.Final Answer:
redis-cli --cluster create 10.1.1.1:7000 10.1.1.2:7000 10.1.1.3:7000 10.1.1.4:7000 10.1.1.5:7000 10.1.1.6:7000 --cluster-replicas 1 -> Option AQuick Check:
6 nodes with --cluster-replicas 1 = 3 masters + 3 replicas [OK]
- Setting replicas higher than number of masters
- Not listing all nodes in command
- Omitting --cluster-replicas option
Ready to prove it?
Pass the test and this pattern moves unknown
