How To Manage Redis Cluster Topology With Command Line

Manual intervention is often necessary to understand and manage the current topology of a Redis cluster. This article covers essential commands for interacting with and managing the Redis cluster, along with retrieving its state information.

CLUSTER INFO

This command gives you an overview of your Redis cluster, including details about key distribution, participating nodes, cluster size, and message statistics.


CLUSTER NODES

With CLUSTER NODES, find information about the nodes that are a part of the Redis cluster.


Questions

CLUSTER SLOTS

This command provides the mapping of the hash slots to cluster nodes so that we can identify which slots are served with which primary and replica nodes.

The output displays information about each hash slot in the cluster. Notably, the first node ID listed for each slot corresponds to the primary node holding that slot's data, while the second node ID represents its replica.

CLUSTER FORGET <node_id>

A troubled node within your Redis cluster can lead to slow response times for client requests. To maintain optimal performance and avoid latency spikes, the CLUSTER FORGET command allows you to proactively remove the problematic node from the cluster, directing client requests to healthy nodes.

Shell
 
for port in {30001..30006}; do
  # Run the CLUSTER FORGET command for each node
  redis-cli -p $port CLUSTER FORGET $NODE_ID
done


Before removing the node:

We can see the node with the id 717d7a44655dad4a1ec6879c6894c0fdb02d0d6c and port 30003 is present in the cluster. 

Now we can run a script like this:

Shell
 
NODE_ID="717d7a44655dad4a1ec6879c6894c0fdb02d0d6c"

for port in {30001..30006}; do
  redis-cli -p $port CLUSTER FORGET $NODE_ID
done


After running the script:

We can now see the node with the id 717d7a44655dad4a1ec6879c6894c0fdb02d0d6c and port 30003 is not present and hence not a part of the cluster. 

CLUSTER MEET <ip> <port>

When your Redis cluster needs to grow, the CLUSTER MEET command comes in handy. It lets you seamlessly add new nodes to distribute the workload. Additionally, if you previously removed a node due to problems (as we did above using CLUSTER FORGET), you can use CLUSTER MEET to reintroduce it to the cluster once the issues are fixed.

Before running the cluster meet (node with port 30003 is not a part of the cluster):

Run the command:

After running the command:

We can see that the node with port 30003 is again a part of the cluster now.

CLUSTER REPLICATE <primary_node_id>

Expanding a Redis cluster involves not just adding nodes but also configuring them for high availability. This command helps us achieve that by promoting a new node to become a replica of an existing node (identified by its ID). This ensures data redundancy and allows clients to access replicas if the primary node fails.

References

 

 

 

 

Top