If you are building a production-grade application and your application has use of the Redis database then you must replicate your data so that in case of any disaster for your main data you can still use the replicated data

Redis provides Replication in two ways

  • Master and Slave replication
  • Redis Cluster Replication

What is Redis Master Slave Replication

In the Redis replication setup, we have two Redis server

  • Master instance
    • All the writes are happening to the master instance
  • Slave instance
    • Once Write is completed on Master then the data is replicated on the slave instance

we will do this replication setup using docker-compose

You can follow the article to understand single redis server installation using docker-compose

Prerequisite

  • Docker must be installed on your machine.
  • Docker Compose must be installed on your machine.

Create a docker-compose file

touch redis-master-slave-replication-docker-compose.yml

Put the below content in the file

version: "3.3"
services:
  redis-master:
    image: redis:6.0.7
    container_name: redis-master
    restart: always
    volumes:
      - redis_master:/data
    ports:
      - 6379:6379

  redis-slave:
    image: redis:6.0.7
    container_name: redis-slave
    restart: always
    volumes:
      - redis_slave:/data
    ports:
      - 6479:6379
    command: redis-server --slaveof redis-master 6379
volumes:
  redis_master:
  redis_slave:
  • we are creating two containers here and the Redis version being used is 6.0.7
    • redis-master
    • redis-slave

Redis Master Container

  • This container is exposed on port 6379 to the outside world
  • The volume where data is being stored is redis_master

Redis Slave Container

  • This container is exposed on port 6479 because we can redis master has already used the 6379 port
  • The volume where data is being stored is redis_slave
  • In Redis Slave, we run a command to make it the slave of the master
command: redis-server --slaveof redis-master 6379

As both the containers are in the same docker network they can refer to each other with the service name itself so the slave container is not connecting to the master using an IP Address. It is connecting using the service name redis-master

Starting the Service

docker-compose -f redis-master-slave-replication-docker-compose.yml up 

This will start the service in foreground mode. If you want to start the service in background mode then you can append the -d option

The logs will be printed on the terminal since we have started the service in foreground mode

You can see that MASTER<->REPLICA sync: Finished with success

Let’s see the running docker container

docker ps

both redis-master and redis-slave containers are running

Connect to Redis Master and Slave

redis-cli -h 127.0.0.1 -p 6379

we are using port 6379 as the redis-master container is running on this port

redis-cli -h 127.0.0.1 -p 6379

we are using port 6479 as the Redis-slave container is running on this port

Verify Redis Master and Slave Working

If you check keys in both Redis instances, you will see an empty array because they do not have any keys.

keys *

Run the above command to check all the keys in Redis

Now insert some data in the master Redis

SET name codewithrajranjan

Then try to check the data in master and slave. If the replication is working fine then you will see the key in both the Redis

Run the below command in both master and slave instances

GET name

You can see that when we add a key in master then the key is also available in slave this means that Master-Slave replication is working fine.

Leave a Reply

Your email address will not be published. Required fields are marked *