Basic Replication in Redis
Tags: redis, replication
Published: 2022-08-14 21:17:31

---
title: Basic Replication in Redis
published: true
description: In this article, we will learn how to do basic replication in Redis.
tags: ['redis', 'replication']
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dyc67ac2rzehkuqhqow8.png
# Use a ratio of 100:42 for best results.
---

Original post: https://koalatea.io/redis-replicate/


## 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.

- Docker install: https://docs.docker.com/engine/install/
- Docker Compose: https://docs.docker.com/compose/install/

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

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

```yml
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`

```yml
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`.

```yml
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.

```yml
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:
- https://github.com/qishibo/AnotherRedisDesktopManager


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](https://koalatea.io/static/317f2ad3d96e9115eaafd219cc013632/1e093/redis-connection.png)

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](https://koalatea.io/static/f9d6545fbd59d0358bb55076e3e8df97/d2f5c/add-new-key.png)

![save new key](https://koalatea.io/static/b060beb3396d4123d42986afd0023b46/98432/save-new-key.png)

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


![redis-cli](https://koalatea.io/static/82804e8642229d85915a72ae253591f9/d99f2/redis-cli.png)


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](https://koalatea.io/static/847c53f48586d74cbd201ebdc2c63281/62452/replicate-data.png)



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.