RedisMaster and Slave Setup

Introduction

Redis supports replication by allowing one Redis server to act as the master and another Redis server to act as the slave. The slave maintains a copy of the data from the master and can be used for read operations and redundancy.

This guide explains how to install Redis, configure the Redis master server, configure a Redis slave server, and enable Redis to start automatically after a reboot.

Note: This article uses Redis 3.0-era configuration and commands. In newer Redis versions, the term replica is preferred over slave, and some configuration directives may have changed.

Prerequisites

Before starting the Redis master-slave setup, make sure you have:

  • Two Linux/CentOS servers.
  • Root or sudo access.
  • Network connectivity between the Redis master and slave.
  • Port 6379 available between the servers.
  • GCC and Make installed for compiling Redis.
  • The hostname or IP address of the Redis master server.

Implementation

Step 1: Install Linux Dependencies on the Redis Server

Install GCC and Make using yum:

sudo yum -y install gcc make

Step 2: Download and Compile Redis 3.0

Download the Redis source package, extract it, and compile Redis:

wget http://<Download the latest file from the redis official site>
tar xzf redis-3.3.0-rc3.tar.gz
cd redis-3.0.0-rc3
make

Step 3: Create Redis Directories and Copy Files

Create the required directories:

sudo mkdir /etc/redis /var/lib/redis

Copy the Redis server and CLI binaries:

sudo cp src/redis-server src/redis-cli /usr/local/bin
sudo cp redis.conf /etc/redis

Step 4: Configure redis.conf on the Master

Open the Redis configuration file:

sudo vim /etc/redis/redis.conf

Configure the following settings:

daemonize yes
bind 0.0.0.0
dir /var/lib/redis
pidfile /var/run/redis.pid
port 6379
dir /var/lib/redis
logfile "/var/log/redis.log"

The bind setting determines which network interfaces Redis listens on. If Redis needs to communicate with a remote slave, make sure the network and firewall configuration allow the required traffic.

Step 5: Download the Redis Init Script

Download the Redis service init script:

wget https://raw.github.com/saxenap/install-redis-amazon-linux-centos/master/redis-server

Step 6: Move and Configure redis-server

Move the downloaded init script to /etc/init.d:

sudo mv redis-server /etc/init.d
sudo chmod 755 /etc/init.d/redis-server
sudo nano /etc/init.d/redis-server

Make sure the Redis binary path is configured as:

redis="/usr/local/bin/redis-server"

Step 7: Enable Redis at System Startup

Add Redis to the system services:

sudo chkconfig --add redis-server
sudo chkconfig --level 345 redis-server on

Step 8: Start the Redis Master Server

Start Redis:

sudo service redis-server start

The Redis master server is now ready to accept connections from the slave server.

Configure the Redis Slave Server

Step 9: Install and Configure Redis on the Slave

On the Redis slave server, follow Steps 1 through 6, except for the master-specific configuration in Step 4.

Open the Redis configuration file:

vim /etc/redis/redis.conf

Specify the following settings:

daemonize yes
slaveof <hostname or IPaddress> 6379
pidfile /var/run/redis.pid
port 6379

Replace <hostname or IPaddress> with the hostname or IP address of the Redis master server.

For example:

slaveof 192.168.1.10 6379

This configures the Redis server to replicate data from the specified master.

Step 10: Enable Redis on the Slave

Add Redis to the system startup configuration:

sudo chkconfig --add redi-server && sudo chkconfig --level 345 redis-server on

Step 11: Start Redis on the Slave

Start the Redis service:

sudo service redis-server start

Once the slave connects successfully to the master, Redis will begin replicating the master’s data.

Verify Redis Replication

After starting both servers, connect to Redis:

redis-cli

Run:

INFO

Check the role field.

On the master, you should see:

role:master

On the slave, you should see:

role:slave

You can also check the replication information on the master to verify that the slave is connected.

Conclusion

Redis master-slave replication provides a simple way to maintain a copy of Redis data on another server. This setup involves installing Redis on both servers, configuring one server as the master, configuring the second server as the slave, and enabling Redis to start automatically.

For newer Redis deployments, use current Redis documentation and configuration syntax because this guide is based on an older Redis release.

FAQs

1. What is Redis master-slave replication?

Redis master-slave replication allows a Redis master to replicate its data to one or more slave servers. In newer Redis terminology, these are generally called primary and replica servers.

2. Which port does Redis use by default?

Redis uses port 6379 by default. The master and slave servers must be able to communicate over the configured Redis port.

3. How can I check whether Redis is running as a master or slave?

Connect using redis-cli and run:

INFO

Then check the role field.

4. What does the slaveof configuration do?

The slaveof directive tells a Redis server to replicate data from the specified master server.

5. Can Redis replication be used for high availability?

Replication provides a copy of the data on another Redis server, but replication by itself is not the same as automatic failover. For high availability, Redis Sentinel or Redis Cluster may be more appropriate depending on the architecture.

  • Redis Common Commands – Learn commonly used Redis commands for managing keys, databases, replication, backups, connections, monitoring, and server statistics.
    Read the article
  • Enable Redis Module for PHP with Nginx Web Server – Learn how to enable Redis support in PHP and use Redis with applications running on an Nginx web server.
    Read the article

Talk to our experts

Looking for the right technology solution for your business? Our team of experts can help you with development, cloud, DevOps, design, and a wide range of other technology needs. Get in touch with our team here.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Comment

Scroll to Top