Home > Software > How to Set Up Redis Master-Slave Replication with Docker Compose

How to Set Up Redis Master-Slave Replication with Docker Compose

Anastasios Antoniadis

Discover how to set up Redis master-slave replication with Docker Compose through our comprehensive guide.

Docker (1)

Redis is a popular in-memory data store used as a database, cache, and message broker. Setting up Redis with master-slave replication in production environments is crucial for data redundancy and read scalability. Docker Compose simplifies the deployment of multi-container applications, making it an ideal tool for setting up a Redis master-slave configuration. This article will guide you through creating a Docker Compose setup for Redis master-slave replication.

Understanding Redis Master-Slave Replication

Redis master-slave replication allows data from the master server to be replicated to one or more slave servers. This architecture enhances data redundancy and load distribution, as read operations can be offloaded to the slave instances.


  • Docker and Docker Compose installed on your system
  • Basic understanding of Docker and containerization concepts

Step 1: Create a Docker Compose File

Create a file named docker-compose.yml in your project directory and open it in your text editor. This file will define the services, networks, and volumes for your Redis master and slave instances.

Step 2: Define the Redis Master Service

Start by defining the master service in your docker-compose.yml file. This service will use the official Redis image from Docker Hub and set necessary environment variables.

version: '3'

    image: redis:alpine
    command: redis-server --appendonly yes
      - "6379:6379"
      - redis-network

This configuration starts a Redis server in append-only mode for persistence and exposes it on the default Redis port 6379.

Step 3: Define the Redis Slave Service

Next, define the slave service. The slave will also use the official Redis image. It will connect to the master using the --slaveof option, specifying the master service name and port.

    image: redis:alpine
    command: redis-server --slaveof redis-master 6379 --appendonly yes
      - "6380:6379"
      - redis-master
      - redis-network

This service is configured to start Redis in slave mode, pointing to the redis-master service. The depends_on option ensures the master starts before the slave.

Step 4: Define the Network

Add a network definition to the bottom of your docker-compose.yml file to facilitate communication between the master and slave services.

    driver: bridge

Step 5: Launch the Redis Master-Slave Setup

With the docker-compose.yml file configured, you can start the Redis master and slave instances by running the following command in the directory containing your Docker Compose file:

docker-compose up -d

This command starts the Redis master and slave in detached mode.

Step 6: Verify Replication

To verify that replication is working as expected, connect to the Redis master and slave instances and check their roles.

  • Connect to the master:
docker exec -it <redis-master-container-id> redis-cli
  • Once connected, type info replication to view the master’s replication info.
  • Similarly connect to the slave:
docker exec -it <redis-slave-container-id> redis-cli
  • Type info replication to view the slave’s replication info and confirm it is connected to the master.


You have successfully set up Redis master-slave replication using Docker Compose. This configuration provides a solid foundation for implementing high availability and scalability for Redis in your applications. Docker Compose makes managing and scaling your Redis instances easy, ensuring your data is robustly stored and efficiently accessible.

Anastasios Antoniadis
Follow me
0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x