Skip to content

Setting up Redis Cluster mode with sharding and replication

Bootstrap a six-node Redis Cluster, understand the 16384 hash-slot model, configure replicas per master, and verify cluster health before any traffic is routed.

Redis Cluster is the answer when one Redis instance cannot hold the working set or absorb the write rate of an application. It shards keys across nodes using a deterministic hash-slot scheme, replicates each shard for failover, and serves clients with a slot-aware client that learns the topology on connect. This article walks through a six-node cluster (three masters, three replicas) on Ubuntu, the bootstrap sequence, and the verifications that prove the cluster is healthy before traffic arrives.

How to verify

# Cluster topology
redis-cli -h 10.0.0.11 -p 6379 -a "$PASS" CLUSTER NODES
redis-cli -h 10.0.0.11 -p 6379 -a "$PASS" CLUSTER INFO
redis-cli -h 10.0.0.11 -p 6379 -a "$PASS" CLUSTER SLOTS

# Confirm slot coverage (should sum to 16384)
redis-cli -h 10.0.0.11 -p 6379 -a "$PASS" CLUSTER COUNTKEYSINSLOT 0
redis-cli -h 10.0.0.11 -p 6379 -a "$PASS" CLUSTER SHARDS

# Cluster bus port reachability between nodes
for n in 10.0.0.11 10.0.0.12 10.0.0.13 10.0.0.14 10.0.0.15 10.0.0.16; do
  nc -zv "$n" 16379
done

What’s happening

A Redis Cluster has 16384 hash slots distributed across the master nodes. Every key is mapped to a slot by CRC16(key) mod 16384, and each slot lives on exactly one master. A client computes the slot for a key, looks it up in a slot-to-node map cached on connect, and sends the command directly to the owning master. When the topology changes the master returns MOVED or ASK redirections; a well-behaved client refreshes its map and retries transparently.

Replicas are full copies of a master’s data, synchronized through the standard replication stream. A master losing quorum triggers a failover: the cluster bus on port 16379 carries gossip between nodes, and replicas elect a new master when the majority of masters agree. The cluster keeps serving as long as a majority of masters are reachable — losing all replicas of a shard is fatal for that shard’s keys but does not break the rest of the cluster.

Keys that must be operated on together (MGET, transactions, Lua scripts touching multiple keys) must hash to the same slot. The convention is curly-brace tags: user:{42}:cart and user:{42}:profile both hash on 42 and land on the same shard.

The procedure

  1. Install Redis on all six nodes (see /en/knowledge-base/redis-cluster-mode-setup/ for prerequisites). Identical version on every node — mixing 7.0 and 7.2 in a cluster works but is not recommended for production.

  2. Configure /etc/redis/redis.conf on each node. The values below are the cluster-specific minimums.

    port 6379
    cluster-enabled yes
    cluster-config-file /var/lib/redis/nodes.conf
    cluster-node-timeout 15000
    cluster-require-full-coverage no
    cluster-replica-validity-factor 10
    cluster-migration-barrier 1
    cluster-allow-reads-when-down no
    
    appendonly yes
    masterauth strong-password
    requirepass strong-password
    bind 0.0.0.0
    protected-mode yes

    Restart each daemon: sudo systemctl restart redis-server.

  3. Open the firewall for both Redis (6379) and the cluster bus (16379):

    for port in 6379 16379; do
      sudo ufw allow from 10.0.0.0/24 to any port "$port" proto tcp
    done
  4. Bootstrap the cluster from any node. This one command creates the cluster, assigns slots, and pairs masters with replicas.

    redis-cli -a "$PASS" --cluster create \
      10.0.0.11:6379 10.0.0.12:6379 10.0.0.13:6379 \
      10.0.0.14:6379 10.0.0.15:6379 10.0.0.16:6379 \
      --cluster-replicas 1

    The CLI proposes a topology and asks for confirmation. The first three nodes become masters, the last three replicas of distinct masters.

  5. Verify slot distribution. Each master should own roughly 5461 slots, and CLUSTER INFO should report cluster_state:ok, cluster_slots_assigned:16384, cluster_slots_ok:16384.

    redis-cli -a "$PASS" --cluster check 10.0.0.11:6379
  6. Test a key write and follow the redirection if you connect to the wrong node:

    redis-cli -a "$PASS" -h 10.0.0.11 -p 6379 SET foo bar
    # (error) MOVED 12182 10.0.0.13:6379
    
    # Cluster-aware mode follows the redirect automatically:
    redis-cli -a "$PASS" -c -h 10.0.0.11 -p 6379 SET foo bar
  7. Add the Redis exporter on each node for Prometheus visibility — redis_cluster_enabled, redis_cluster_state, and redis_cluster_slots_ok should all be alerted on.

Common pitfalls

  • cluster-require-full-coverage yes makes the entire cluster refuse writes if any shard is down. Set it to no for caches where partial availability is preferred to a full outage.
  • Multi-key operations across slots fail with CROSSSLOT errors. If you need atomicity across related keys, use hash tags: {user:42}:profile and {user:42}:cart co-locate.
  • The cluster bus port (16379 by default; computed as port + 10000) must be open between all nodes. A firewall that only opens 6379 produces a cluster that never converges.
  • A replica promoted to master inherits its predecessor’s slots but does not automatically get a new replica. Add a fresh replica node after every unplanned failover or run with cluster-replicas 2 from the start.
  • Mixing Redis versions across nodes during a rolling upgrade is supported but bus protocol incompatibilities have appeared in past minor versions — always test upgrades in a staging cluster first.

In the engagements we run, every Redis Cluster comes up under Ansible — six VMs, cluster config templated, exporters wired, and a smoke-test that writes 10000 keys across slots and reads them back before the cluster is declared ready. We operate clustered Redis under Managed Operations, and for clients running it alongside the rest of a multi-tier stack the clustered environments solution is what we use to describe the engagement shape.