Database Management Redis Subjective
Oct 05, 2025

How does Redis Cluster handle data distribution and failover?

Detailed Explanation
Redis Cluster uses hash slots for data distribution and automatic failover: **Hash Slot Distribution:** • 16384 total hash slots (0-16383) • Each key mapped to slot using CRC16(key) % 16384 • Slots distributed across master nodes • Each master handles subset of slots **Cluster Setup:** # Create 6-node cluster (3 masters + 3 replicas) redis-cli --cluster create \ 192.168.1.100:7000 192.168.1.101:7000 192.168.1.102:7000 \ 192.168.1.100:7001 192.168.1.101:7001 192.168.1.102:7001 \ --cluster-replicas 1 **Node Configuration:** # redis-cluster.conf port 7000 cluster-enabled yes cluster-config-file nodes-7000.conf cluster-node-timeout 15000 cluster-announce-ip 192.168.1.100 **Client Implementation:** from rediscluster import RedisCluster startup_nodes = [ {"host": "192.168.1.100", "port": "7000"}, {"host": "192.168.1.101", "port": "7000"}, {"host": "192.168.1.102", "port": "7000"} ] rc = RedisCluster( startup_nodes=startup_nodes, decode_responses=True, skip_full_coverage_check=True ) # Multi-key operations with hash tags rc.set("{user:1000}:profile", "data") rc.set("{user:1000}:settings", "config") # Both keys go to same slot/node **Failover Process:** 1. Nodes ping each other every second 2. Node marked as PFAIL after timeout 3. Becomes FAIL when majority agrees 4. Replica detects master failure 5. Replica starts election 6. Gets votes from other masters 7. Becomes new master if majority votes **Manual Operations:** # Check cluster status redis-cli -c -p 7000 CLUSTER NODES redis-cli -c -p 7000 CLUSTER SLOTS # Manual failover redis-cli -c -p 7001 CLUSTER FAILOVER # Add new node redis-cli --cluster add-node 192.168.1.103:7000 192.168.1.100:7000 # Reshard slots redis-cli --cluster reshard 192.168.1.100:7000 **Best Practices:** • Odd number of masters (3, 5, 7) • At least 1 replica per master • Use hash tags for related keys • Monitor cluster state continuously • Plan for slot migration
Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback