Managing Redis Cluster
1. Understanding Cluster Architecture
| Concept | Detail |
|---|---|
| Slots | 16384 hash slots distributed across masters |
| Sharding | CRC16(key) mod 16384 |
| High availability | Each master can have replicas |
| No DB select | Only DB 0 supported |
2. Creating Cluster Nodes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-require-full-coverage no
3. Joining Cluster
| Command | Description |
|---|---|
redis-cli --cluster create h1:p h2:p ... --cluster-replicas 1 | Bootstrap cluster |
CLUSTER MEET ip port | Add node to existing cluster |
4. Adding Slots to Nodes
| Command | Description |
|---|---|
CLUSTER ADDSLOTS slot [slot ...] | Assign individual slots |
CLUSTER ADDSLOTSRANGE s e [s e] | Range form 7.0+ |
CLUSTER DELSLOTS / DELSLOTSRANGE | Unassign |
5. Migrating Slots
| Tool | Description |
|---|---|
redis-cli --cluster reshard | Interactive slot migration |
CLUSTER SETSLOT slot MIGRATING|IMPORTING|STABLE|NODE | Low-level state |
6. Getting Cluster Info
| Command | Returns |
|---|---|
CLUSTER INFO | state, slots_assigned, known_nodes |
CLUSTER SHARDS | Shard-level layout 7.0+ |
CLUSTER COUNTKEYSINSLOT slot | Per-slot key count |
7. Listing Cluster Nodes
| Command | Description |
|---|---|
CLUSTER NODES | Full node table (id, addr, flags, slots) |
CLUSTER MYID | Local node id |
8. Understanding Hash Slot Distribution
Example: Hashtag pins keys to one slot
SET {user:42}:profile "..."
SET {user:42}:cart "..."
# Both keys map to the same slot via "user:42"
| Use | Detail |
|---|---|
| Multi-key ops | Require same slot — use {tag} |
9. Configuring Cluster Replicas
| Command | Description |
|---|---|
CLUSTER REPLICATE master-id | Attach replica to master |
cluster-replica-validity-factor | Disqualify stale replicas for failover |
10. Handling Cluster Failover
| Command | Use |
|---|---|
CLUSTER FAILOVER | Replica takes over its master |
CLUSTER FAILOVER FORCE | Skip handshake |
CLUSTER FAILOVER TAKEOVER | Bypass majority — use only in disaster |
11. Resharding Cluster
| Command | Description |
|---|---|
redis-cli --cluster reshard host:port | Interactive resharding |
redis-cli --cluster rebalance | Even slot distribution |
12. Monitoring Cluster Health
| Command | Description |
|---|---|
redis-cli --cluster check host:port | Topology + slot coverage check |
CLUSTER LINKS | Per-link diagnostic 7.0+ |
13. Using Cluster-Aware Clients
| Capability | Detail |
|---|---|
| MOVED redirection | Client caches slot→node mapping |
| ASK redirection | Single-command redirect during migration |
| READONLY | Issue on replica connection for replica reads |
14. Handling Network Partitions
| Risk | Mitigation |
|---|---|
| Split-brain writes | Set cluster-require-full-coverage yes to stop writes when slots missing |
| Minority partition | Refuses writes if it can't reach majority |