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
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 # slaveThe 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.
If a primary and its replica both fail, its slots have no owner.
With the default cluster-require-full-coverage yes, the whole
cluster stops serving, even keys on healthy nodes, with
CLUSTERDOWN The cluster is down. Setting it to no keeps the rest of
the keyspace available while those slots are down. Which is better
depends on whether partial availability is worse than none for your
application. Also spread each primary and its replica across different
machines or zones.
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 rolesResharding
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.
Rebalancing moves data, so it costs network and CPU on every node involved. Run it during low traffic, watch latency while it runs, and prefer adding capacity before nodes are near their memory limit. A node at 95% memory has no headroom for the extra buffers migration needs.
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
redis-cli --cluster check redis-node-1:6379and read the output.- Stop a primary, time the failover,
GETa key it owned with-c, then restart it and confirm it rejoined as a replica. - Do a manual
CLUSTER FAILOVERon that replica to make it primary again. - Reshard 100 slots from one primary to another and rerun
check. - Stop a primary and its replica. Try reading a key on a different
node. Then
CONFIG SET cluster-require-full-coverage noon the remaining nodes and try again.