Jack Wallen demonstrates how easy it is to set up a Redis cluster for database high availability and automatic failover.
Redis is an open-source, in-memory database that offers tons of features and more than enough performance to support your enterprise-grade app or service. Recently, I wrote about implementing Redis on a single server. While a single server will serve your project well, chances are you’ll want to add the level of failover that comes with Redis clustering. And that’s exactly what I’m going to show you.
With a Redis cluster, you get high performance, asynchronous replication, and linear scaling for up to 1,000 nodes.
TO SEE: Hiring Kit: Database Engineer (Tech Republic Premium)
To properly deploy a Redis cluster, the recommended installation is six nodes with three serving as controllers. We will set up such a cluster with the following layout:
- Regulator 1: 192.168.1.100
- Controller 2: 192.168.1.101
- Regulator 3: 192.168.1.102
- Node 1: 192.168.1.200
- Node 2: 192.168.1.201
- Node 3: 192.168.1.202
I assume you followed the original tutorial and installed Redis on all machines. Once you’ve got that sorted, you’re ready to move on to the cluster phase.
What you need
You need six machines running Redis. All you need is a user with sudo privileges. That’s it: let’s cluster.
How to configure the cluster
You must edit the configuration file on all six machines. Open the file for editing with the command:
sudo nano /etc/redis/redis.conf
The first line you need to change is the bind option. Where you see either bind 127.0.0.1 or bind 0.0.0.0, you want to replace that with:
bind SERVER
Where SERVER is the IP address of that particular machine. So Controller 1 will be (in my example):
bind 192.168.1.100
Regulator 2 would be:
bind 192.168.1.101
etc.
The following rules to be configured look like this:
protected-mode no
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
appendonly yes
Save and close the file.
Restart Redis on any machine with:
sudo systemctl restart redis
How to initialize the cluster
Once everything is configured, it’s time to initialize the cluster. This is handled with the Redis CLI common and must be done with each IP:port as follows:
redis-cli --cluster create 192.168.1.100:7000 192.168.1.101:7000 192.168.1.102:7000 192.168.1.200:7000 192.168.1.201:7000 192.168.1.202:7000 --cluster-replicas 1
The --cluster-replicas 1
option means there is exactly one node for each controller. The output of the command shows each node along with a randomly generated ID for each. Make sure to type yes when prompted and the cluster will be initialized.
If you want to check the status of each node, you can do it with the command:
redis-cli -h 192.168.1.100 -p 7000 cluster nodes
The output of the above command should list all nodes.
Congratulations, you have just deployed your first Redis cluster for high availability and failover. This may be the easiest cluster configuration you’ll ever experience, and the benefit you get from automatic failover cannot be overstated.
Enjoy that extra Redis power.
Subscribe to TechRepublic’s How to make technology work on YouTube for the latest technical advice for business professionals from Jack Wallen.