0
0
Redisquery~30 mins

Client-side cluster support in Redis - Mini Project: Build & Apply

Choose your learning style9 modes available
Client-side Cluster Support with Redis
📖 Scenario: You are managing a Redis database that is distributed across multiple nodes in a cluster. To efficiently access data, you want to simulate client-side cluster support by mapping keys to specific nodes.
🎯 Goal: Build a simple Redis client-side cluster support setup by creating a dictionary that maps keys to nodes, configuring a hash slot function, and implementing a function to get the node for a given key.
📋 What You'll Learn
Create a dictionary called cluster_nodes with three nodes: 'node1', 'node2', and 'node3'.
Create a variable called hash_slots set to 16384 representing total hash slots in Redis cluster.
Write a function called get_node_for_key that takes a key and returns the node name by computing the hash slot modulo hash_slots and mapping it to a node.
Add a final dictionary called key_node_map that maps example keys 'user:1', 'order:5', and 'product:3' to their respective nodes using get_node_for_key.
💡 Why This Matters
🌍 Real World
Redis clusters distribute data across multiple nodes to improve performance and reliability. Client-side cluster support helps clients know which node to contact for a given key.
💼 Career
Understanding client-side cluster support is important for backend developers and database administrators working with distributed Redis setups.
Progress0 / 4 steps
1
Create cluster nodes dictionary
Create a dictionary called cluster_nodes with these exact entries: 0: 'node1', 1: 'node2', and 2: 'node3'.
Redis
Need a hint?

Use a dictionary with integer keys 0, 1, 2 and string values 'node1', 'node2', 'node3'.

2
Set total hash slots
Create a variable called hash_slots and set it to 16384.
Redis
Need a hint?

Assign the integer 16384 to the variable hash_slots.

3
Write function to get node for a key
Write a function called get_node_for_key that takes a parameter key. Inside, compute the hash slot by taking the built-in hash(key) modulo hash_slots. Then, get the node index by taking the hash slot modulo 3. Return the node name from cluster_nodes using this index.
Redis
Need a hint?

Use hash(key) % hash_slots to get the slot, then slot % 3 to get the node index.

4
Map example keys to nodes
Create a dictionary called key_node_map that maps these keys: 'user:1', 'order:5', and 'product:3' to their respective nodes by calling get_node_for_key for each key.
Redis
Need a hint?

Use a dictionary with keys as strings and values from calling get_node_for_key on each key.