Learn Labs
Redis Cluster

Operating a Cluster

Failover, resharding, adding nodes, and full-coverage behaviour

Running a cluster adds a few operations a single instance doesn't have: nodes fail and are replaced, slots move, and the cluster has to decide whether to keep serving while part of it is missing. All of them go through redis-cli --cluster or CLUSTER commands.

Checking health

redis-cli --cluster check redis-node-1:6379slot coverage, keys and slots per primary, open slotsredis-cli --cluster info redis-node-1:6379summaryCLUSTER INFOcluster_state, slots_assigned, slots_fail, epochsCLUSTER NODESevery node: id, address, flags, primary, slotsCLUSTER SHARDSthe same, structured (what clients use)
docker exec redis-node-1 redis-cli --cluster check redis-node-1:6379
redis-node-1:6379 (8a87bdee...) -> 2 keys | 5361 slots | 1 slaves.
...
[OK] All nodes agree about slots configuration.
[OK] All 16384 slots covered.

Automatic failover

Measured in this lab:

docker stop redis-node-3            # a primary
# ~8 seconds later its replica is a primary
docker exec redis-node-1 redis-cli CLUSTER INFO | grep cluster_state
cluster_state:ok
docker exec redis-node-1 redis-cli -c GET user:42     # still served, from the promoted replica

docker start redis-node-3           # rejoins as a REPLICA of the node that replaced it
docker exec redis-node-3 redis-cli ROLE | head -1     # slave

The detection time comes from cluster-node-timeout (5 s here): a primary unreachable for that long is flagged PFAIL by each node that notices, becomes FAIL once a majority of primaries agree, and then its replica holds an election and takes over its slots.

Manual failover

For maintenance (upgrading a primary's host), promote its replica gracefully, with no lost writes:

# run on the REPLICA you want to promote
docker exec redis-node-4 redis-cli CLUSTER FAILOVER
# the primary pauses clients, the replica catches up completely, then they swap roles

Resharding

Move slots between primaries, keys and all, while the cluster serves traffic:

SRC=$(docker exec redis-node-1 redis-cli CLUSTER MYID)
DST=<id of another primary from CLUSTER NODES>

docker exec redis-node-1 redis-cli --cluster reshard redis-node-1:6379 \
--cluster-from $SRC --cluster-to $DST --cluster-slots 100 --cluster-yes

Moving slot 98 from redis-node-1:6379 to ...
Moving slot 99 from redis-node-1:6379 to ...

For each slot, the tool marks it MIGRATING on the source and IMPORTING on the target, moves keys in batches with MIGRATE, then updates ownership. During the move, clients asking for a key that has already moved get an ASK redirect. Big keys make this slow: each MIGRATE of a huge key blocks both nodes while it transfers.

Adding a node

# 1. start a new empty node with cluster-enabled yes, then join it
redis-cli --cluster add-node new-node:6379 redis-node-1:6379

# 2. it owns no slots yet. Give it a fair share:
redis-cli --cluster rebalance redis-node-1:6379 --cluster-use-empty-masters

# or add it as a replica of a specific primary:
redis-cli --cluster add-node new-node:6379 redis-node-1:6379 --cluster-slave --cluster-master-id <id>

Removing a node is the reverse: reshard its slots away first, then redis-cli --cluster del-node.

Replica migration

With cluster-allow-replica-migration yes (default), if one primary loses its only replica while another has two, a spare replica moves over automatically, so no primary is left without a backup. That's a reason to run one extra replica in the cluster.

Lab

  1. redis-cli --cluster check redis-node-1:6379 and read the output.
  2. Stop a primary, time the failover, GET a key it owned with -c, then restart it and confirm it rejoined as a replica.
  3. Do a manual CLUSTER FAILOVER on that replica to make it primary again.
  4. Reshard 100 slots from one primary to another and rerun check.
  5. Stop a primary and its replica. Try reading a key on a different node. Then CONFIG SET cluster-require-full-coverage no on the remaining nodes and try again.

On this page