Redis Cluster.
Why
- Sharding: > single-node RAM.
- HA: per-shard primary + replica.
- Auto-failover via internal gossip.
Setup
# 6 nodes (3 primaries + 3 replicas)
for port in 7000 7001 7002 7003 7004 7005; do
mkdir $port && cd $port
cat > redis.conf <<EOF
port $port
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF
redis-server redis.conf &
cd ..
done
redis-cli --cluster create 127.0.0.1:7000 ... 127.0.0.1:7005 --cluster-replicas 1
Hash slots
16384 slots total, distributed across primaries.
key → CRC16(key) % 16384 → slot → node.
Hash tags
SET {user:1}:profile ...
SET {user:1}:sessions ...
{user:1} → same slot → multi-key ops possible.
Client
from redis.cluster import RedisCluster
client = RedisCluster(host="node1", port=7000)
# Cluster-aware: routes to correct node, handles MOVED redirects.
Use cluster-aware client. Plain client gets MOVED errors on miss.
Resharding
redis-cli --cluster reshard 127.0.0.1:7000
# Interactive: how many slots, from where, to where
Live migration.
Add node
redis-cli --cluster add-node 127.0.0.1:7006 127.0.0.1:7000
# Then reshard to give it slots
Remove node
redis-cli --cluster reshard ... (move slots away)
redis-cli --cluster del-node 127.0.0.1:7000 <node-id>
Failover
redis-cli -p 7004 CLUSTER FAILOVER # on replica
Cluster status
redis-cli -p 7000 CLUSTER INFO
redis-cli -p 7000 CLUSTER NODES
redis-cli -p 7000 CLUSTER SLOTS
SCAN in cluster
for key in cluster.scan_iter(match="user:*"):
...
Cluster-aware client iterates each node.
Multi-key ops
# Only works if keys hash to same slot
client.mget("{u:1}:a", "{u:1}:b") # ok
client.mget("a", "b") # may fail with CROSSSLOT
Pub/sub in cluster
Pub/sub messages broadcast to all nodes by default. Use SHARDED PUBSUB (Redis 7+):
SSUBSCRIBE channel
SPUBLISH channel msg
Sharded by channel name.
Replica reads
client.execute_command("READONLY")
# Then reads route to replicas
For read scaling.
Common mistakes
- Non-cluster client → MOVED errors.
- Forgetting hash tags → multi-key ops fail.
- KEYS pattern → only on single node.
- Adding nodes without resharding.
- Mixing cluster + non-cluster instances.
Read this next
If you want my cluster setup, it’s at rajpoot.dev .
Building something AI-, backend-, or data-heavy and want a second pair of eyes? I do consulting and freelance work — see my projects and ways to reach me at rajpoot.dev .