Basic Replication in Redis

06.13.2022

Intro

In this article, we will get a brief look on how to replicate in Redis. In production, you will often want to use a cluster or a manage instance. However, learning how to replicate manually will help with some ad hoc backups or debugging.

Getting Set Up

For this article, we will use docker and docker compose, so start by installing those.

Next create a new file called docker-compose.yml.

Add the following to lines to the top to declare our version and services:

version: "3.2"
services:

A warning, Redis still uses the old Master and Slave language, but this is old and should be changed.

With that in mind, let's add our master (main) service called redis. Everything here is standard except we pass REDIS_REPLICATION_MODE=master

redis:
    image: "redis:alpine"
    command: redis-server --requirepass some_pass
    ports:
     - "6379:6379"
    volumes:
      - $PWD/redis-data:/var/lib/redis
      - $PWD/redis.conf:/usr/local/etc/redis/redis.conf
    environment:
     - REDIS_REPLICATION_MODE=master

Next, let's add a single slave (worker) server. Here we change the local mapping port to 6378, and set the replication mode to slave.

redis2:
  image: "redis:alpine"
  command: redis-server --requirepass some_pass
  ports:
    - "6378:6379"
  volumes:
    - $PWD/redis-data:/var/lib/redis
    - $PWD/redis.conf:/usr/local/etc/redis/redis.conf
  environment:
    - REDIS_REPLICATION_MODE=slave

The full file looks like the following.

version: "3.2"
services:
 redis:
    image: "redis:alpine"
    command: redis-server --requirepass some_pass
    ports:
     - "6379:6379"
    volumes:
      - $PWD/redis-data:/var/lib/redis
      - $PWD/redis.conf:/usr/local/etc/redis/redis.conf
    environment:
     - REDIS_REPLICATION_MODE=master

 redis2:
    image: "redis:alpine"
    command: redis-server --requirepass some_pass
    ports:
     - "6378:6379"
    volumes:
      - $PWD/redis-data:/var/lib/redis
      - $PWD/redis.conf:/usr/local/etc/redis/redis.conf
    environment:
     - REDIS_REPLICATION_MODE=slave

Now, start the services using docker-compose up.

Connecting to the Redis Servers

Next we need to connect to our redis servers. For this, I use an open source GUI:

Click "New Connection" and enter the information like the following. You can change the Connection Name to whatever you want. For this, I named it "Main". The password should be some_pass if you used the yml file above.

redis new connection

Do the same for the worker, but change the port to 6378 and change the connection name.

Replicating the Server

To start, expand the first server and add a new key.

add new key

save new key

Now, open the connection to the worker server and click the cli button to connect to the server.

redis-cli

First, set the master pass to some_pass (since we used the same password for both servers).

config set masterauth some_pass

Then, use the replicaof command to point to the master server.

replicaof redis 6379

Click the refresh icon, and you should see the master server key replicated. Add more keys to the master and the worker will continue to replicate.

replicate data

To stop replication, you can use the following.

replicaof no one

Conclusion

That's it for the basics of replication. This helps if you want to quickly copy down some data for testing. There is much more to explore, but this should be a good start.