How to Set Up Redis Cluster: Step-by-Step Guide
To set up a
Redis Cluster, you need to start multiple Redis instances on different ports or servers, configure each with cluster-enabled settings, and then use the redis-cli --cluster create command to link them. This creates a distributed setup where data is automatically sharded and replicated across nodes.Syntax
Setting up a Redis cluster involves these main steps:
- Start Redis instances: Run multiple Redis servers with
cluster-enabled yesin their config files. - Create the cluster: Use
redis-cli --cluster createfollowed by the list of node addresses and ports. - Assign slots: The cluster command automatically assigns hash slots to nodes for data distribution.
bash
redis-server /path/to/redis-node.conf redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 --cluster-replicas 1
Example
This example shows how to start 3 Redis nodes locally and create a cluster with 1 replica per master node.
bash
# Create config files for each node with cluster enabled cat > redis-7000.conf <<EOF port 7000 cluster-enabled yes cluster-config-file nodes-7000.conf cluster-node-timeout 5000 dir ./7000 appendonly yes EOF cat > redis-7001.conf <<EOF port 7001 cluster-enabled yes cluster-config-file nodes-7001.conf cluster-node-timeout 5000 dir ./7001 appendonly yes EOF cat > redis-7002.conf <<EOF port 7002 cluster-enabled yes cluster-config-file nodes-7002.conf cluster-node-timeout 5000 dir ./7002 appendonly yes EOF # Start each Redis server in the background redis-server redis-7000.conf & redis-server redis-7001.conf & redis-server redis-7002.conf & # Create the cluster with 1 replica per master redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 --cluster-replicas 1
Output
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:7001 to 127.0.0.1:7000
Adding replica 127.0.0.1:7002 to 127.0.0.1:7001
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
>>> Cluster created successfully!
Common Pitfalls
Common mistakes when setting up a Redis cluster include:
- Not enabling
cluster-enabled yesin each node's config file. - Using the same
cluster-config-filename for multiple nodes, causing conflicts. - Failing to open the required ports (default 7000-7005) in firewalls.
- Not starting enough nodes; a cluster needs at least 3 master nodes for fault tolerance.
- Incorrectly specifying node addresses in the
redis-cli --cluster createcommand.
Example of a wrong config and the fix:
bash
# Wrong: cluster-enabled missing port 7000 # cluster-enabled yes missing here cluster-config-file nodes-7000.conf # Right: port 7000 cluster-enabled yes cluster-config-file nodes-7000.conf
Quick Reference
Summary tips for Redis cluster setup:
- Run at least 3 master nodes for a stable cluster.
- Enable
cluster-enabled yesin every node config. - Use unique
cluster-config-filenames per node. - Open all Redis ports (default 7000-7005) in your firewall.
- Use
redis-cli --cluster createwith all node addresses.
Key Takeaways
Enable cluster mode in each Redis node configuration with unique config files.
Start multiple Redis instances and connect them using redis-cli cluster create command.
Ensure at least 3 master nodes for fault tolerance and open required ports in firewall.
Use the --cluster-replicas option to add replicas for high availability.
Double-check node addresses and configs to avoid common setup errors.