Client-side cluster support in Redis - Time & Space Complexity
When using Redis with client-side cluster support, the client decides which server to talk to for each request.
We want to understand how the time to find the right server grows as the number of keys or servers increases.
Analyze the time complexity of this simplified client-side cluster key lookup:
// Assume slots is a map from slot number to server
function getServerForKey(key) {
const slot = hash(key) % totalSlots;
const server = slots[slot];
return server;
}
This code finds the server by hashing the key and looking up the slot in a map.
Look for repeated steps that affect performance.
- Primary operation: Computing the hash of the key and accessing the slot map.
- How many times: Once per key lookup; no loops inside this function.
The time to find the server depends on the hash and a direct map lookup, which stays quick even if the number of keys grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 keys | About 2 operations per lookup (hash + map access) |
| 100 keys | Still about 2 operations per lookup |
| 1000 keys | Still about 2 operations per lookup |
Pattern observation: The lookup time stays almost the same no matter how many keys there are.
Time Complexity: O(1)
This means the time to find the right server does not grow as you add more keys; it stays constant.
[X] Wrong: "Looking up the server will take longer as the number of keys grows because there are more keys to check."
[OK] Correct: The client uses a hash and direct slot map, so it does not check keys one by one. It jumps straight to the right server.
Understanding how client-side cluster support keeps lookups fast helps you explain efficient distributed systems in interviews.
What if the slot map was stored as a list instead of a map? How would the time complexity change?
Practice
Solution
Step 1: Understand client-side cluster support
Client-side cluster support means the client knows how to talk to the right Redis node directly.Step 2: Identify the main benefit
This direct connection helps speed up requests and balances load across nodes.Final Answer:
It allows the client to connect directly to the correct Redis node in the cluster. -> Option DQuick Check:
Client-side cluster support = direct node connection [OK]
- Thinking it reduces database size automatically
- Confusing encryption with cluster support
- Believing it merges nodes into one server
Solution
Step 1: Recall Redis client cluster initialization syntax
The standard way is to create a client with cluster option and provide nodes list.Step 2: Match syntax to options
redis.createClient({ cluster: true, nodes: [...] }) matches the known syntax: redis.createClient({ cluster: true, nodes: [...] }). Others are invalid method names.Final Answer:
redis.createClient({ cluster: true, nodes: [...] }) -> Option AQuick Check:
Correct cluster client init = createClient with cluster true [OK]
- Using non-existent methods like connectCluster
- Forgetting to set cluster: true
- Passing nodes incorrectly
const client = redis.createClient({ cluster: true, nodes: [ { port: 7000 }, { port: 7001 } ] });
await client.connect();
await client.set('key1', 'value1');
const val = await client.get('key1');
console.log(val);Solution
Step 1: Understand cluster client behavior on set/get
The client connects to multiple nodes and routes commands to the right node based on key hashing.Step 2: Analyze code flow
It sets 'key1' with 'value1' and then gets 'key1'. The client handles routing, so get returns 'value1'.Final Answer:
It will print 'value1' after storing the key in the correct cluster node. -> Option AQuick Check:
Cluster client routes keys correctly = prints value1 [OK]
- Thinking set/get are unsupported in cluster mode
- Assuming only one node is used
- Expecting null because of cluster complexity
const client = redis.createClient({ cluster: true, nodes: [ { port: 7000 } ] });
await client.connect();
What is the most likely cause?Solution
Step 1: Check cluster node requirements
For cluster support, the client should know multiple nodes to discover the cluster topology.Step 2: Analyze given nodes array
Only one node is provided, so the client cannot properly discover the cluster, causing connection errors.Final Answer:
You need to provide at least two nodes for cluster support. -> Option CQuick Check:
Cluster client needs multiple nodes to connect [OK]
- Assuming single node is enough for cluster
- Confusing default Redis port with cluster ports
- Misconfiguring cluster option
Solution
Step 1: Understand failover handling in cluster clients
Good cluster clients support automatic discovery of nodes and retry commands on failover.Step 2: Evaluate options for smooth failover
Configure the client with multiple cluster nodes and enable automatic node discovery and retries. uses multiple nodes and automatic discovery, which helps the client adapt to cluster changes without manual intervention.Final Answer:
Configure the client with multiple cluster nodes and enable automatic node discovery and retries. -> Option BQuick Check:
Automatic discovery and retries = smooth failover [OK]
- Relying on manual failover switching
- Disabling cluster mode loses benefits
- Restarting client on every error is inefficient
Ready to prove it?
Pass the test and this pattern moves unknown
