How to Build a High‑Availability Redis Cluster Without Centralized Configuration
This guide explains why Redis clustering is needed for capacity, concurrency and failover, describes Redis 3.0's decentralized cluster architecture, provides step‑by‑step commands to configure, launch and combine six nodes into a cluster, demonstrates slot calculations, client usage with Jedis, and outlines fault recovery, pros and cons, and cleanup procedures.
1. Why a Redis cluster and high availability?
Production environments often face capacity limits, heavy concurrent writes, and master‑slave IP changes that require manual reconfiguration; traditional proxy‑based solutions are costly and complex.
2. Redis 3.0 decentralized cluster solution
Redis 3.0 introduces a non‑centralized cluster where each Redis instance remains master‑slave, any node can accept client requests, and the whole cluster can be built with only six servers. Slots are used to distribute data, reducing hardware requirements.
3. Cluster overview and setup
Enable clustering in redis.conf:
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000Copy the base configuration to create additional files for ports 6380, 6381, 6389, 6390 and 6391, add masterauth rainbowsea if a password is required, and ensure each file ends with the cluster settings.
Start the six instances:
# redis-server /rainbowsea/redis6379.conf
# redis-server /rainbowsea/redis6380.conf
# redis-server /rainbowsea/redis6381.conf
# redis-server /rainbowsea/redis6389.conf
# redis-server /rainbowsea/redis6390.conf
# redis-server /rainbowsea/redis6391.confCombine them into a cluster (replace the IP with the real one):
redis-cli --cluster create --cluster-replicas 1 192.168.76.147:6379 192.168.76.147:6380 192.168.76.147:6381 192.168.76.147:6389 192.168.76.147:6390 192.168.76.147:6391If a password is set, add -a rainbowsea to the command.
Important notes:
All nodes must be running before creating the cluster.
Use the real IP address (output of ifconfig) instead of 127.0.0.1.
Specify --cluster-replicas 1 to create one replica per master.
4. Using the cluster
Redis clusters have 16384 hash slots (0‑16383). The slot for a key is calculated as CRC16(key) % 16384. Each node handles a range of slots (e.g., 0‑5460, 5461‑10922, 10923‑16383).
Clients automatically redirect to the correct node when the -c flag is used: redis-cli -c -p 6379 Multi‑key commands (e.g., MGET, MSET) only work if all keys belong to the same slot. Use hash tags to force keys into the same slot:
mset k1{order} "v1" k2{order} "v2" k3{order} "v3"5. Cluster management commands
CLUSTER KEYSLOT <key>– returns the slot number for a key. CLUSTER COUNTKEYSINSLOT <slot> – shows how many keys are stored in a slot. CLUSTER GETKEYSINSLOT <slot> <count> – lists keys in a slot.
6. Fault recovery
If a master node goes down, its replica is promoted after the cluster-node-timeout (default 15 s). When the original master returns, it becomes a replica.
The cluster-require-full-coverage setting controls behavior when an entire slot’s master and replica are lost: yes – the whole cluster becomes unavailable. no – only the affected slot is unusable; other slots continue to work.
7. Java client (Jedis) development
Add the Jedis dependency to pom.xml:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>Simple connection test:
Jedis jedis = new Jedis("192.168.76.147", 6379);
jedis.auth("rainbowsea");
String ping = jedis.ping();
System.out.println("Ping result = " + ping);
jedis.close();Cluster usage with a connection pool:
Set<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("192.168.76.147", 6379));
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(200);
poolConfig.setMaxIdle(32);
poolConfig.setMaxWaitMillis(60 * 1000);
poolConfig.setBlockWhenExhausted(true);
poolConfig.setTestOnBorrow(true);
JedisCluster cluster = new JedisCluster(nodes, 5000, 5000, 5, "rainbowsea", poolConfig);
cluster.set("address", "bj");
String val = cluster.get("address");
System.out.println("address=>" + val);
cluster.close();8. Advantages and disadvantages
Advantages
Scalable horizontal expansion.
Load is distributed across nodes.
Decentralized configuration simplifies deployment.
Disadvantages
Multi‑key operations are not supported.
Multi‑key transactions and Lua scripts are unavailable.
Migrating from other clustering solutions to Redis Cluster can be complex.
9. Cleanup
Delete persistent RDB/AOF files in the Redis data directory (e.g., rm -f dump*.rdb) to start with a clean state.
For firewalls, open all Redis ports (6379‑6391) using firewall-cmd --add-port=PORT/tcp --permanent and reload the firewall.
After the cluster is operational, you can verify connectivity with redis-cli -c -p 6379 and run typical commands.
Finally, delete any leftover dump*.rdb or appendonly.aof files to start fresh.
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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
