Databases 13 min read

Why Redis Needs a Cluster: Step‑by‑Step Setup, Configuration & Best Practices

This guide explains the need for Redis clustering to achieve high availability, walks through Redis 3.0's decentralized cluster configuration, shows how to modify redis.conf, start multiple nodes, create the cluster, use hash slots, handle failures, and connect via Java Jedis, highlighting both advantages and limitations.

Raymond Ops
Raymond Ops
Raymond Ops
Why Redis Needs a Cluster: Step‑by‑Step Setup, Configuration & Best Practices

Why a Redis Cluster Is Needed

Production demands such as capacity expansion, concurrent writes, and master‑slave failover require a high‑availability solution.

Traditional Proxy Approach

Using a proxy host adds cost and complexity, especially with one‑master‑one‑slave per service.

Redis 3.0 Decentralized Cluster

Redis 3.0 introduces a master‑slave structure where any node can accept requests, reducing required servers to six and distributing keys across slots.

Decentralized cluster diagram
Decentralized cluster diagram

Cluster Overview and Setup

Redis cluster shards data across N nodes, each holding roughly 1/N of the keyspace. Availability is maintained as long as a majority of masters are reachable.

Configuration Changes

cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000

Copy the modified configuration to other ports (6380, 6381, 6389, 6390, 6391) and add masterauth rainbowsea if a password is set.

Starting the Nodes

# 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

Verify the processes are running.

Creating the Cluster

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:6391
# 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:6391

Use real IP addresses, not 127.0.0.1, and ensure all nodes‑xxxx.conf files exist.

Using the Cluster

Each key maps to one of 16384 hash slots calculated by CRC16(key) % 16384. The client automatically redirects to the correct node when the -c flag is used. redis-cli -c -p 6379 Multi‑key commands only work on keys that share the same slot, which can be forced by using a key hash tag, e.g. k1{order}.

Fault Recovery

If a master fails, its replica is promoted after a timeout. The cluster-require-full-coverage setting determines whether the whole cluster stops (yes) or only the affected slots become unavailable (no).

Jedis Java Client

Include the Jedis dependency in pom.xml and configure a JedisCluster with a set of HostAndPort objects and 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(60000);
JedisCluster cluster = new JedisCluster(nodes, 5000, 5000, 5, "rainbowsea", poolConfig);
cluster.set("address", "bj");
String address = cluster.get("address");
System.out.println("address=>" + address);
cluster.close();

Read and write operations automatically handle node redirection.

Pros and Cons

Scalable horizontal expansion.

Load distribution across nodes.

Simple decentralized configuration.

Multi‑key operations and transactions are not supported.

Lua scripts are limited.

Migrating from other clustering solutions can be complex.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javahigh availabilityredisConfigurationJedisCluster
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.