How to Add and Remove Nodes in a Redis Cluster
This guide demonstrates how to horizontally scale a Redis cluster by adding nodes as masters or slaves, configuring them, using redis-cli commands to join or remove nodes, and handling special cases such as removing a failed node, all with practical code examples.
Scalability is a key metric for software systems, and horizontal scaling is essential for achieving it. Redis Cluster provides a simple way to add or remove nodes, enhancing scalability. The following sections illustrate these operations with concrete examples.
Adding Nodes
There are two scenarios for adding a node to an existing cluster: as a master or as a slave.
1. Add as Master Create a new directory (e.g., cluster-test/7006 ) and copy a redis.conf file, then modify key settings such as:
port 7006 # port number
cluster-enabled yes # enable cluster mode
cluster-config-file nodes-7006.conf # cluster state file
cluster-node-timeout 5000 # timeout in ms
dbfilename "dump-7006.rdb" # data file
protected-mode no
logfile "logfile-7006.log"
requirepass 123456
masterauth 123456
pidfile "redis_7006.pid"Start the instance on port 7006 and add it to the cluster with:
redis-cli --cluster add-node 127.0.0.1:7006 127.0.0.1:7000 -a 123456The command’s first argument is the new node’s address, the second is any existing cluster node, and -a supplies the password. Verify the addition with cluster nodes , which shows the new node as a master without slots or election participation.
2. Add as Slave Use the same add-node command but include --cluster-slave :
redis-cli --cluster add-node 127.0.0.1:7006 127.0.0.1:7000 --cluster-slave -a 123456This randomly selects a master with few replicas. To attach the new node to a specific master (e.g., master 7002), run the command with the target master’s ID, after which the node appears as a slave of that master.
Removing Nodes
To remove a node, use del-node :
redis-cli --cluster del-node 127.0.0.1:7000 723d06ef31c5ada88ee3926249fc89f69fae3767 -a 123456The first argument is any cluster node, the second is the node ID to delete. If the node is a master, ensure it holds no data before removal.
For a failed node that cannot be contacted, del-node will fail. Instead, execute cluster forget on all remaining nodes:
redis-cli --cluster call 127.0.0.1:7000 cluster forget <node-id>This forces every node to forget the dead node’s ID.
Conclusion
The examples show that adding or removing nodes in a Redis cluster is straightforward, providing an effective horizontal scaling solution for high‑availability data stores.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.