Setting Up a Redis Cluster

1. Configuring Redis Instances

Each Redis node in the cluster requires a specific configuration. Edit the redis.conf file for each instance to include the following key settings:

# Run as a background process
daemonize yes

# Assign a unique port to each instance (e.g., 7001-7006)
port 7001

# Enable cluster mode
cluster-enabled yes

# Specify the cluster configuration file name
cluster-config-file nodes.conf

# Set the node timeout in milliseconds
cluster-node-timeout 15000

# Enable AOF persistence for data durability
appendonly yes

# (Optional) Uncomment to set a password for the instance
# requirepass your_strong_password
# masterauth your_strong_password

# (Optional) Disable protected mode for easier cluster setup in trusted networks
# protected-mode no

The nodes.conf file is automatically generated by Redis and stores the cluster's state. To create a new cluster from scratch, ensure this file is deleted from all node directories.

2. Initializing the Cluster

The cluster initialization process involves two main tasks: creating the master-slave relationships and assigning hash slots.

Initial, you can use the redis-cli utility with the --cluster create command. However, this method can be inflexible if you need to apply specific configurations like passwords after the fact.

A more robust approach is to use the native cluster commands after starting all Redis instances. This grants finer control over the setup process. The key comamnds are:

  • CLUSTER MEET <ip> <port>: Introduces a new node to the cluster, making it aware of other nodes.
  • CLUSTER REPLICATE <master_node_id>: Configures the current node as a replica of the specified master node. This command is executed on the replica node.
  • CLUSTER ADDSLOTS <slot> [slot ...]: Assigns one or more hash slots to the current node. This command is run on master nodes.

3. Assigning Hash Slots

A Redis cluster has 16384 hash slots, which must be distributed among the master nodes. The distribution can be done manually using a script. For example, to assign the first range of slots to a master node, you could use a bash script similar to this:

#!/bin/bash
# Assign slots 0 to 5461 to the master at 192.168.1.10:7001
START_SLOT=0
END_SLOT=5461
MASTER_HOST="192.168.1.10"
MASTER_PORT=7001
PASSWORD="your_strong_password" # Use if password is set

for slot in $(seq $START_SLOT $END_SLOT); do
  redis-cli -h $MASTER_HOST -p $MASTER_PORT -a $PASSWORD CLUSTER ADDSLOTS $slot
done

Create similar scripts for your other master nodes to cover the remaining slots up to 16383.

4. Verifying the Cluster

Once the cluster is configured and all slots are assigned, you can verify its status.

Connect to any node in the cluster using the redis-cli with the -c flag. This flag enables cluster-aware mode, which automatically redirects commands to the correct node.

Example connection command

./redis-cli -h 192.168.1.10 -p 7001 -a your_strong_password -c


<p>After connecting, run the following commands to check the cluster health and functionality:</p>

<code># View all nodes and their roles, master/replica status, and slot assignments
CLUSTER NODES

# Test data insertion and redirection
SET test_key "Hello, Redis Cluster!"
GET test_key
</code>

<p>If the commands execute successfully and the <code>CLUSTER NODES</code> output shows all nodes online with correct slot ranges, your Redis cluster is operational.</p>

Tags: Redis redis-cluster NoSQL database-setup Clustering

Posted on Sat, 10 Oct 2026 16:51:41 +0000 by bogdani