How to Setup Redis Replication

What is Redis?
Redis (Remote Directory Server) is a popular in-memory data structure store used as a database, cache and message broker.
It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperlogs, geospatial indexes with radius queries and streams.
It provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.
Why does Redis perform so well?
In order to achieve its outstanding performance, Redis works with an in-memory dataset. Depending on your use case, you can persist it either by dumping the dataset to disk every once in a while or by appending each command to a log.
Persistence can be optionally disabled if you just need a feature-rich, networked, in-memory cache.
Redis also supports trivial-to-setup master-slave asynchronous replication, with very fast non-blocking first synchronization, auto-reconnection with partial resynchronization on net split.
Today, we will explore how to setup Redis Replication with Cluster-Mode Disabled in Amazon Linux 2.
Please note, in this replication setup we aim to have a master node with one or two replicas with cluster mode disabled. In this case, we have single master read/write node and two or more read-only replica nodes. There is a single node group which contains all the cluster’s data in each node.
Why this setup?
In this setup, a client can write on master but read from any node in the cluster. Once any write activity has been performed on master, it breeds to all the connected slaves for updating the datasets. Let us begin.
Starting up Redis
First install Redis.
yum install redis
systemctl start redis
systemctl enable redis
systemctl status redis
Configuring Redis Master Server
Open the Redis config file, using the /etc/redis.conf.
vi /etc/redis.conf
By default, Redis is configured to listen and accept connections on the loopback interface using the bind directive.
To enable communication between replicas, the master should be configured to listen IPv4 loopback address and its LAN IP address i.e. 10.35.0.110
bind 127.0.0.1 10.35.0.110
To allow communications with the replica, set the protected-mode parameter to no:
protected-mode no
Additionally, if we want to secure communications, we can protect the master using the requirepass directive, so that the clients/replicas request an authentication password before running any commands.
requirepass DLT@DevOps44
We then configure Redis to interact with systemd. To do this, the supervised parameter needs to be added:
supervised systemd
Once this is done, save the file and close it. Restart the Redis service for the changes to take effect.
systemctl daemon-reload
systemctl restart redis
The next step is to configure Redis’ Replica servers.
Configuring Redis’ replica servers
On the replica servers, we need to configure in the same way we did for the master.
Add the respective IPv4 address in the bind directive on both the replica servers.
On 1st replica server
bind 127.0.0.1 10.36.0.211
On 2nd replica server
bind 127.0.0.1 10.37.0.122
Now, we set up our Redis instance as a replica
Setting up Redis instance as a replica
In order to configure the Redis instance as a replica, we can use the replicaof parameter and set the master node’s IP and port to identify the master and enable communication.
replicaof 10.35.0.110 6379
Don’t forget to add the same config in all other replica sets and the authentication parameter too. 😊
To add the authentication on the replica nodes will use the masterauth parameter
masterauth DLT@DevOps44
Once these configurations are complete, restart the Redis services in all your replica nodes.
systemctl restart redis
Also, keep in mind to open the ports between master and replica servers so that they will communicate with each other. Otherwise, this will create chaos in the next steps. 😊
Now we will verify the Master-Replica status.
Verifying master-replica status
On the master node run the following command.
redis-cli
127.0.0.1:6379> AUTH DLT@DevOps44
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.36.0.211,port=6379,state=online,offset=5849,lag=1
slave1:ip=10.37.0.122,port=6379,state=online,offset=5849,lag=1
master_repl_offset:5849
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:5848
Check the replication status on the slaves as well.
# Replication
role:slave
master_host: 10.35.0.110
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:41519
slave_priority:100
slave_read_only:1
connected_slaves:0
min_slaves_good_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
Testing your setup
Now let’s test our effort by setting a key-value in master instance and check their replication in the replica sets.
On master node,
redis-cli
127.0.0.1:6379> AUTH DLT@DevOps44
127.0.0.1:6379> set org ‘DLTLabs’
Now check the same in any of the replica nodes. We should see this:
redis-cli
127.0.0.1:6379> get org
If we do, kudos!! Our efforts have paid off. The key-values are being replicated among each node.
We have successfully setup Redis Replication with Cluster-Mode Disabled in Amazon Linux 2.
Curious about caching with Redis? Read on to know more:
Author — Rahul Kumar, DLT Labs™
About the Author: Rahul has professional experience primarily in Infrastructure/DevOps Engineering with FinTech start-ups.
He is an expert problem solver who likes to understand issues until a resolution is reached using proper analysis. When he is not solving problems, he explores infrastructure and security best practices and is passionate about travel photography and music.