11. Redis Cluster: Horizontal Scaling
Scaling Redis Horizontally with Redis Cluster
Redis Cluster provides horizontal scaling by partitioning data across multiple nodes. It supports automatic failover and high availability. This module covers cluster architecture, configuration, operations, and best practices for large-scale Redis deployments.
Redis Cluster is essential for large-scale Redis deployments. It distributes data across multiple nodes using a hash slot approach. There are 16384 hash slots. Each node handles a subset of slots. Clients route requests to the correct node. The cluster automatically handles failover. This architecture provides horizontal scaling and high availability. Understanding Redis Cluster is essential for production Redis deployments at scale.
This module covers cluster architecture, configuration, hash slots, operations, and best practices with practical examples.
Redis Cluster Architecture
Redis Cluster partitions data across multiple nodes using a hash slot approach. There are 16384 hash slots. Each node handles a subset of slots. Clients route requests to the correct node. The cluster automatically handles failover. This architecture provides horizontal scaling and high availability.
Cluster nodes communicate using a gossip protocol to share state. Each node knows about the cluster topology. Clients use CLUSTER SLOTS to get the cluster map. MOVED and ASK errors redirect clients to the correct node. This design ensures clients can find the right node for each key.
# Create a Redis cluster (6 nodes minimum: 3 masters + 3 replicas) redis-cli --cluster create \ 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 \ 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 \ --cluster-replicas 1 # Check cluster status redis-cli -c -p 7000 cluster info redis-cli -c -p 7000 cluster nodes # Connect to cluster redis-cli -c -p 7000 # Check slot distribution CLUSTER SLOTS # Check cluster state CLUSTER INFO
Hash Slots and Data Distribution
Data is distributed across nodes using hash slots. Each key is assigned to a slot using CRC16(key) % 16384. Nodes manage ranges of slots. The cluster redirects clients to the correct node. This design ensures even data distribution. Use tags to control key grouping.
Hash tags allow you to group keys by enclosing a part of the key in {}. Keys with the same tag go to the same slot, enabling multi-key operations on the same node. This is useful for transactions and Lua scripts that operate on multiple keys.
// Using hash tags to group keys // Keys with the same tag go to the same slot SET user:{123}:profile "..." SET user:{123}:settings "..." // Both keys go to the same slot because of {123} // Without tags, keys may be on different nodes SET user:123:profile "..." SET user:123:settings "..." // These may be on different nodes // Multi-key operations with hash tags MSET user:{123}:profile "alice" user:{123}:settings "dark" // Works because both keys are in the same slot
Cluster Operations
Cluster operations include adding and removing nodes, rebalancing slots, and handling failover. Use redis-cli --cluster for operations. Monitor cluster health. Plan capacity carefully. These operations ensure cluster health and performance.
Adding nodes allows cluster expansion. Removing nodes requires rebalancing slots to redistribute data. Resharding moves slots between nodes. Rebalancing ensures even slot distribution. Failover handles node failures automatically. Regular maintenance keeps the cluster healthy.
# Add a new node redis-cli --cluster add-node 127.0.0.1:7006 127.0.0.1:7000 # Add as replica redis-cli --cluster add-node 127.0.0.1:7007 127.0.0.1:7000 --cluster-slave --cluster-master-id <master-id> # Reshard slots redis-cli --cluster reshard 127.0.0.1:7000 # Check cluster health redis-cli --cluster check 127.0.0.1:7000 # Rebalance slots redis-cli --cluster rebalance 127.0.0.1:7000 # Remove a node redis-cli --cluster del-node 127.0.0.1:7000 <node-id>
Knowledge Check
Ready to test your understanding of 11. Redis Cluster: Horizontal Scaling?