Redis Cluster is an architecture that automatically distributes your data across multiple nodes and provides high availability. It offers an indispensable solution, especially for large-scale applications. In this article, we will walk through the Redis Cluster setup and scaling steps step by step. If you are using Redis on a single server, migrating to Cluster will give you more flexibility and fault tolerance.
What is Redis Cluster?
Redis Cluster divides data into 16,384 hash slots and distributes these slots among nodes. Each node only manages the slots assigned to it. For example, in a 3-node cluster, slots are roughly divided as 5461, 5461, and 5462. This distribution allows keys to be automatically routed to the correct node.
Advantages of Cluster
- High performance: Operations are not concentrated on a single point; the load is distributed.
- Automatic sharding: You don't have to manually partition your data.
- High availability: By defining replica (backup) nodes for each node, you can prevent service interruptions in case of failure.
- Easy scaling: You can increase capacity by adding nodes and redistributing slots.
Redis Cluster Setup Steps
1. Environment Preparation
I recommend using at least 3 master nodes and, in the recommended configuration, a total of 6 nodes with one replica for each master. Set up a separate Linux server (or Docker container) for each node. For example, let's use the following IPs:
- 10.0.0.1:7000 (master1)
- 10.0.0.2:7000 (master2)
- 10.0.0.3:7000 (master3)
- 10.0.0.1:7001 (replica1)
- 10.0.0.2:7001 (replica2)
- 10.0.0.3:7001 (replica3)
Compile Redis from source or install it using a package manager on each node. Then, create a redis.conf file for each node. To enable cluster mode, add the following settings:
port 7000 cluster-enabled yes cluster-config-file nodes-7000.conf cluster-node-timeout 5000 appendonly yes
2. Starting the Nodes
Start the Redis server for each node. For example:
redis-server /path/to/redis-7000.confTo verify, run the command redis-cli -p 7000 ping. Each node should respond with PONG.
3. Creating the Cluster
You can set up the cluster using the redis-cli tool. The following command creates a cluster with 3 masters and 3 replicas:
redis-cli --cluster create 10.0.0.1:7000 10.0.0.2:7000 10.0.0.3:7000 10.0.0.1:7001 10.0.0.2:7001 10.0.0.3:7001 --cluster-replicas 1This command selects masters and assigns a replica to each. You will be prompted to confirm the slot distribution during the process. Type "yes" and press Enter.
4. Checking the Cluster Status
Use the following command to verify the setup:
redis-cli --cluster check 10.0.0.1:7000The output shows the status of all nodes, slot distribution, and replica relationships. You can also get detailed information with the cluster info and cluster nodes commands.
Scaling Steps
Adding a New Node
Suppose you want to add a new master node to your existing cluster. Prepare a new server (e.g., 10.0.0.4:7000) and start Redis in cluster mode. Then add the node to the cluster with the following command:
redis-cli --cluster add-node 10.0.0.4:7000 10.0.0.1:7000After this operation, the new node will be in "handshake" state and will not yet have any slots. To rebalance the slots, run the following command:
redis-cli --cluster rebalance 10.0.0.1:7000Important: The rebalance operation attempts to distribute slots evenly across all nodes. If you want to give more slots to specific nodes, you can use the --cluster-weight parameter.
Adding a Replica
Adding a new replica node follows a similar process. First, start the node, then add it with the following command:
redis-cli --cluster add-node 10.0.0.4:7001 10.0.0.1:7000 --cluster-slaveIf you want it to be a replica of a specific master, you can use --cluster-slave --cluster-master-id instead of --cluster-slave.
Removing a Node
To remove a node from the cluster, you must first empty all slots on that node. The following command moves slots from the source node to the target node:
redis-cli --cluster reshard 10.0.0.1:7000 --cluster-from --cluster-to --cluster-slotsAfter the slots are emptied, remove the node from the cluster:
redis-cli --cluster del-node 10.0.0.1:7000Performance and Considerations
When using Redis Cluster, it's important to keep the following points in mind:
- Client support: Ensure that the Redis client library you use supports Cluster. For example, popular libraries like Jedis, StackExchange.Redis, or redis-py can work in cluster mode.
- Network latency: Network latency between nodes can affect performance. Host nodes in the same data center for low latency.
- Backup: Cluster structure can complicate backup processes. Regularly back up your data using RDB or AOF files.
- Monitoring: Periodically check health using Redis's INFO command and redis-cli --cluster check.
Conclusion
When done correctly, Redis Cluster setup and scaling provide you with a flexible, high-performance, and fault-tolerant database infrastructure. Whether you start with 3 nodes or scale up to 100 nodes, Redis Cluster is an ideal choice for your growing applications. If you need support with your Redis Cluster configuration, feel free to contact us at HostingServer.com.tr.