How To Create A Redis Cluster For Database Failover

how-to-create-a-redis-cluster-for-database-failover

How To Create A Redis Cluster For Database Failover

Jack Wallen shows you how easy it is to set up a Redis cluster for database high availability and automatic failover.

Image: Gorodenkoff/Adobe Stock

Redis is an open-source, in-memory database that offers plenty of features and more than enough performance to power your enterprise-level app or service. Recently, I wrote about deploying Redis on a single server. Although a single server will serve your project well, there’s a good chance you’re going to want to add the level of fail-over that comes along with clustering Redis. And that’s exactly what I’m going to show you.

With a Redis cluster, you gain high performance, asynchronous replication and linear scaling for up to 1,000 nodes.

SEE: Hiring Kit: Database engineer (TechRepublic Premium)

To properly deploy a Redis cluster, the recommended setup is six nodes with three serving as controllers. We’ll set up such a cluster with the following layout:

  • Controller 1: 192.168.1.100
  • Controller 2: 192.168.1.101
  • Controller 3: 192.168.1.102
  • Node 1: 192.168.1.200
  • Node 2: 192.168.1.201
  • Node 3: 192.168.1.202

I’m going to assume you’ve followed the original tutorial and have Redis installed on all machines. Once you have that taken care of, you’re ready to move on to the clustering stage.

What you’ll need

You’ll need six machines with Redis running. The only other thing you’ll 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’ll need to change is the bind option. Where you might see either bind 127.0.0.1 or bind 0.0.0.0, you’ll want to replace that with:

bind SERVER

Where SERVER is the IP address of that particular machine. So Controller 1 will (in my example) be:

bind 192.168.1.100

Controller 2 would be:

bind 192.168.1.101

Et cetera.

The next lines to be configured will 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 each machine with:

sudo systemctl restart redis

How to initialize the cluster

With everything configured, it’s time to initialize the cluster. This is handled with the Redis CLI common and must be run with each IP:port like so:

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’ll be exactly one node for each controller. The output of the command will display 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 on the status of each node, you can do so 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 just deployed your first Redis cluster for high availability and failover. This might be the easiest cluster setup you’ll ever experience and the benefit you gain from having auto failover cannot be overstated.

Enjoy that added Redis power.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Jack Wallen shows you how easy it is to set up a Redis cluster for database high availability and automatic failover. Image: Gorodenkoff/Adobe Stock Redis is an open-source, in-memory database that offers plenty of features and more than enough performance to power your enterprise-level app or service. Recently, I wrote about deploying Redis on a…