Practical Guide to Setting Up and Scaling a Redis Cluster (Redis 6.0+)
This article provides a step‑by‑step tutorial on building a Redis Cluster on a single server, covering node configuration, cluster handshaking, slot assignment, master‑slave replication, command routing, failover handling, and practical scaling operations such as adding and removing nodes using redis‑cli.
This article is a practical summary of learning Redis Cluster (based on Redis 6.0+), detailing the complete process of building a Redis Cluster environment and performing cluster scaling.
Redis Cluster Overview
Redis Cluster is Redis's distributed database solution that uses sharding to share data, providing replication and automatic failover. Compared with master‑slave replication and Sentinel, it offers a more complete high‑availability design and removes single‑node storage and write‑load limits.
1. Building the Redis Cluster Environment
All six nodes (three masters and three slaves) run on the same server, distinguished by ports. The basic architecture is illustrated with a diagram.
Redis 6.0+ is compiled from source to obtain redis-server and redis-cli . Since Redis 5.0, the former redis‑trib.rb tool is integrated into redis-cli .
The setup follows four manual steps (instead of using redis‑trib.rb ) to become familiar with basic cluster management:
Start nodes in cluster mode.
Node handshake – connect independent nodes into a network.
Slot assignment – distribute the 16384 slots among master nodes.
Master‑slave replication – assign slaves to masters.
1.1 Start Nodes
Each node starts as a master but with cluster-enabled yes . Example configuration for port 6379:
# redis_6379_cluster.conf
port 6379
cluster-enabled yes
cluster-config-file "node-6379.conf"
logfile "redis-server-6379.log"
dbfilename "dump-6379.rdb"
daemonize yesAfter editing the six configuration files, launch them with redis-server redis_XXXX_cluster.conf and verify with ps -aux | grep redis .
1.2 Node Handshake
Initially each node forms its own single‑node cluster. Use CLUSTER MEET to connect them:
127.0.0.1:6379> CLUSTER MEET 127.0.0.1 6380
127.0.0.1:6379> CLUSTER MEET 127.0.0.1 6381
...After the meet commands, CLUSTER NODES shows all six nodes as masters.
1.3 Slot Assignment
The cluster divides the key space into 16384 slots. Assign slots to masters using CLUSTER ADDSLOTS :
redis-cli -p 6379 cluster addslots {0..5000}
redis-cli -p 6380 cluster addslots {5001..10000}
redis-cli -p 6381 cluster addslots {10001..16383}After assignment, CLUSTER INFO reports cluster_state:ok .
1.4 Master‑Slave Replication
Configure slaves with CLUSTER REPLICATE <node_id> so that each master has a corresponding slave, achieving high availability.
redis-cli -p 6479 cluster replicate 87b7dfacde34b3cf57d5f46ab44fd6fffb2e4f52
redis-cli -p 6480 cluster replicate c47598b25205cc88abe2e5094d5bfd9ea202335f
...Alternatively, the whole cluster can be created in one command using redis-cli --cluster create … --cluster-replicas 1 .
1.5 Executing Commands in the Cluster
When the cluster is online, a client sends a command to any node. The node checks whether the key’s slot belongs to it; if not, it returns a MOVED error directing the client to the correct node.
127.0.0.1:6379> CLUSTER KEYSLOT name
(integer) 5798
127.0.0.1:6379> set name huey # redirected to 127.0.0.1:6380Commands sent to a slave are also redirected to the corresponding master.
1.6 Cluster Failover
If a master goes down, its slaves elect a new master automatically. The log shows the election process and the state transition back to cluster_state:ok . When the original master recovers, it becomes a slave of the new master.
2. Cluster Scaling Practice
Scaling involves re‑sharding to move slots between nodes.
2.1 Adding Nodes
Add two new nodes (6382 and 6482) and make 6482 a replica of 6382:
redis-cli --cluster add-node 127.0.0.1:6382 127.0.0.1:6379
redis-cli --cluster add-node 127.0.0.1:6482 127.0.0.1:6379 --cluster-slave --cluster-master-idThen use redis-cli --cluster reshard to move 4096 slots to the new master (6382) and set the replication relationship.
2.2 Removing Nodes
To delete the previously added nodes, first reshard all slots from 6382 to other masters, then run:
redis-cli --cluster del-node 127.0.0.1:6482
redis-cli --cluster del-node 127.0.0.1:6382After deletion, CLUSTER NODES shows the remaining six nodes.
3. Conclusion
Building a Redis Cluster involves node startup, handshake, slot assignment, and master‑slave replication; scaling follows the same steps. Using redis-cli --cluster commands simplifies management and reduces the risk of manual errors.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.