Redis Cluster Introduction, Installation, and Manual/Automatic Setup Guide
This article provides a comprehensive tutorial on Redis Cluster, covering its architecture, data partitioning and redundancy, step‑by‑step instructions for downloading, compiling, and installing Redis on CentOS, as well as detailed procedures for manually or automatically creating a six‑node cluster with master‑slave replication and slot allocation.
Redis Cluster is a decentralized, distributed storage solution introduced in Redis 3.0 that consists of multiple nodes forming a mesh network, allowing clients to connect to any node and automatically redirect to the node holding the requested key.
The cluster uses 16,384 hash slots to partition data across nodes, mapping keys to slots via CRC16 and modulo operations, and provides high availability through master‑slave replication and automatic failover.
Download & Install Redis
On a CentOS 7.4 system, install a single‑node Redis (version 5.0.3) if not already present:
cd /usr/local
wget http://download.redis.io/releases/redis-5.0.3.tar.gz
tar -zxvf redis-5.0.3.tar.gz
cd redis-5.0.3
make && make installIf the GCC compiler is missing, install it first:
yum -y install gcc
make distclean
make && make installSuccessful installation is confirmed by the presence of Redis binaries in /usr/local/bin.
Build a Redis Cluster (Manual Method)
Plan six nodes (three masters A‑C and three slaves D‑F) on a single machine using different TCP ports. Create configuration directories and copy the default redis.conf into each:
mkdir -p /usr/local/redis-cluster/{7001,7002,7003,8001,8002,8003}
cp redis.conf /usr/local/redis-cluster/7001/
... (repeat for each node) ...Modify each redis.conf to enable clustering, set the port, bind address, daemonize, and specify a cluster config file, e.g. for node A:
bind 192.168.83.128
port 7001
pidfile /var/run/redis_7001.pid
daemonize yes
cluster-enabled yes
cluster-node-timeout 15000
cluster-config-file nodes-7001.confStart all six nodes:
/usr/local/bin/redis-server /usr/local/redis-cluster/7001/redis.conf
/usr/local/bin/redis-server /usr/local/redis-cluster/7002/redis.conf
... (repeat for each node) ...Verify they are running with ps -ef|grep redis.
Node Handshake
Connect the nodes using the cluster meet command so each node knows about the others. Only a subset of meet commands is needed; the rest of the mesh is formed automatically.
cluster meet 127.0.0.1 7002
cluster meet 127.0.0.1 7003
cluster meet 127.0.0.1 8001
cluster meet 127.0.0.1 8002
cluster meet 127.0.0.1 8003Check connections with cluster nodes.
Slot Allocation
Assign the 16,384 hash slots evenly to the three master nodes:
redis-cli -p 7001 cluster addslots {0..5461}
redis-cli -p 7002 cluster addslots {5462..10922}
redis-cli -p 7003 cluster addslots {10923..16383}After allocation, cluster info should show cluster_state:ok.
Master‑Slave Replication
Link each slave to its master using cluster replicate <node-id>. Retrieve node IDs with cluster nodes and run:
redis-cli -p 8001 cluster replicate 61e8c4ed8d1ff2a765a4dd2c3d300d8121d26e12
redis-cli -p 8002 cluster replicate 1b4b3741945d7fed472a1324aaaa6acaa1843ccb
redis-cli -p 8003 cluster replicate 19147f56e679767bcebb8653262ff7f56ca072a8The cluster is now fully operational with high availability.
Automatic Cluster Creation
For Redis 5.0+, the built‑in redis-cli --cluster create command replaces the older Ruby redis-trib.rb tool. After starting the six nodes, run:
redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:8001 127.0.0.1:8002 127.0.0.1:8003 --cluster-replicas 1The tool allocates slots, creates replicas, and performs a cluster check, resulting in a ready‑to‑use Redis Cluster with one replica per master.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
