Master Redis Cluster Setup: High Availability, Configuration, and Java Integration
This guide walks through why Redis clustering is needed for high availability, explains the decentralized cluster architecture introduced in Redis 3.0, provides step‑by‑step configuration of multiple redis.conf files, demonstrates creating a six‑node cluster, shows key slot calculations, fault‑tolerance handling, and includes Java Jedis code for connecting to the cluster.
1. Why Need Cluster – High Availability
Why need cluster – high availability:
Production environment needs and problems:
Insufficient capacity – how to scale Redis.
Concurrent write operations – how to distribute load.
Master‑slave IP changes on host failure require configuration updates.
Traditional solution: proxy host.
Diagram explanation:
Client request first reaches proxy server. Proxy forwards request to the appropriate business processor. For high availability, proxy service, A service, B service, C service each need a master‑slave structure (at least one‑master‑one‑slave), requiring at least eight servers. This scheme is costly and hard to maintain; one‑master‑many‑slaves increases cost further.
Redis 3.0 provides a decentralized cluster configuration solution:
Each Redis service still uses a master‑slave structure.
All Redis services are interconnected; any server can act as the request entry point.
Connected servers can forward requests.
This decentralized approach reduces required servers to six.
The configuration distributes keys to slots based on the key value, relieving pressure on a single host.
Redis recommends using the decentralized cluster configuration.
In production, each Redis server should be deployed on a different machine to avoid single‑point failures.
2. Cluster Overview and Setup
Redis cluster implements horizontal scaling: launching N Redis nodes distributes the entire database across those N nodes, each storing roughly 1/N of the total data.
Redis cluster provides availability through partitioning; even if some nodes fail or lose communication, the cluster continues handling commands.
Redis cluster setup – practical demonstration:
redis.conf configuration modification
cluster-enabled yes # enable cluster mode
cluster-config-file nodes-6379.conf # node config file name
cluster-node-timeout 15000 # node timeout (ms) for automatic failovervi /rainbowsea/redis6379.conf , delete unnecessary content and add cluster configuration. Final file example:
include /rainbowsea/redis.conf
pidfile "/var/run/redis_6379.pid"
port 6379
dbfilename "dump6379.rdb"
masterauth rainbowsea
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000Copy the configuration file to create additional node configs:
# cp redis6379.conf redis6380.conf
# cp redis6379.conf redis6381.conf
# cp redis6379.conf redis6389.conf
# cp redis6379.conf redis6390.conf
# cp redis6379.conf redis6391.confModify the remaining five configuration files with a find‑replace to change the port number: :%s/6379/6380 Remember to add the password line to each file: masterauth rainbowsea # add Redis password if needed
include /rainbowsea/redis.conf
pidfile "/var/run/redis_6379.pid"
port 6379
dbfilename "dump6379.rdb"
masterauth rainbowsea
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000Start the six Redis services
# 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.conf
# ps -aux | grep redisCombine the six nodes into a single cluster
Run the cluster creation command (no password):
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:6391Or with password:
redis-cli --cluster create -a rainbowsea --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:6391Important notes:
Before combining, ensure all Redis servers for the required ports are started and that nodes-xxxx.conf files are generated.
Use the real IP address (from ifconfig ) instead of 127.0.0.1.
Use --cluster-replicas 1 for a simple one‑master‑one‑slave setup (three groups).
If the cluster creation fails, stop any running sentinel processes and retry.
3. Using the Redis Cluster
What are slots?
Redis cluster contains 16384 hash slots (0‑16383). Every key maps to one slot using CRC16(key) % 16384. Multiple keys can share the same slot.
Each node handles a range of slots, e.g.:
Node A: slots 0‑5460 Node B: slots 5461‑10922 Node C: slots 10923‑16383
Inserting values:
When a key is set, Redis calculates its slot; if the client is connected to a node that does not own the slot, the server redirects the client to the correct node.
The -c option of redis-cli enables automatic redirection.
Example: redis-cli -c -p 6379 then set/get keys and the client follows redirects.
Multi‑key operations: Keys that belong to different slots cannot be used with MGET, MSET, etc.
192.168.76.147:6381> mset k1 "v1" k2 "v2" k3 "v3"To group keys into the same slot, use hash tags {}:
192.168.76.147:6381> mest k1{order} "v1" k2{order} "v2" k3{order} "v3"Query the slot of a key: 192.168.76.147:6381> CLUSTER KEYSLOT k1 Count keys in a slot:
192.168.76.147:6381> CLUSTER COUNTKEYSINSLOT 12706Get keys from a slot:
192.168.76.147:6381> CLUSTER GETKEYSINSLOT 16025 24. Redis Cluster Fault Recovery
If a master goes down, its replica is automatically promoted after the 15‑second timeout.
# redis-cli -c -p 6380When the original master returns, it becomes a replica.
If both master and replica for a slot are down, behavior depends on cluster-require-full-coverage:
If set to yes , the entire cluster stops. If set to no , only the affected slots become unavailable; the rest of the cluster continues operating.
5. Redis Cluster Development with Jedis (Java)
Even when connecting to a replica, the cluster redirects writes to the master (write to master, read from replica).
Decentralized master‑slave cluster ensures data written on any node is readable from all nodes.
Open required Redis ports in the firewall.
# firewall-cmd --add-port=6379/tcp --permanent
# firewall-cmd --add-port=6380/tcp --permanent
# firewall-cmd --add-port=6381/tcp --permanent
# firewall-cmd --add-port=6389/tcp --permanent
# firewall-cmd --add-port=6390/tcp --permanent
# firewall-cmd --add-port=6391/tcp --permanent
# firewall-cmd --reloadIn pom.xml add the Jedis dependency:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>Simple connection test:
package com.rainbowsea.jedis;
import org.junit.Test;
import redis.clients.jedis.Jedis;
public class JedisCluster_ {
@Test
public void con() {
// Connect using IP and port
Jedis jedis = new Jedis("192.168.76.147", 6379);
// Authenticate if password is set
jedis.auth("rainbowsea");
String ping = jedis.ping();
System.out.println("Connection successful, ping = " + ping);
jedis.close();
}
}Cluster client example:
import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
import java.util.HashSet;
import java.util.Set;
public class JedisCluster_ {
public static void main(String[] args) {
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 jedisCluster = new JedisCluster(nodes, 5000, 5000, 5, "rainbowsea", poolConfig);
jedisCluster.set("address", "bj");
String address = jedisCluster.get("address");
System.out.println("address=>" + address);
jedisCluster.close();
}
}6. Advantages and Disadvantages of Redis Cluster
Advantages:
Enables horizontal scaling.
Distributes load across multiple nodes.
Decentralized configuration is relatively simple.
Disadvantages:
Multi‑key operations are not supported.
Multi‑key transactions and Lua scripts are not supported.
Migration from other clustering solutions can be complex because Redis Cluster was introduced later.
7. Additional Tips
Delete existing RDB/AOF files in the root directory before rebuilding:
# rm -f dump*.rdb8. Final Note
“In this final chapter I want to express my gratitude to every reader. Your attention and feedback are the driving force behind my work. I will keep your encouragement close to my heart and continue to strive in other fields. Thank you, and we will meet again someday.”
© Original author, all rights reserved. (Link: https://www.cnblogs.com/TheMagicalRainbowSea/p/18703659)
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
