DeployU
Interviews / Databases / How do you do replication in Redis?

How do you do replication in Redis?

practical Replication Interactive Quiz Code Examples

The Scenario

You are a database administrator at a social media company. You are responsible for a Redis database that is critical to the business.

You need to set up replication to a secondary database to provide high availability and disaster recovery.

The Challenge

Explain how you would set up replication in Redis. What is a replica set, and what are the different types of nodes in a replica set?

Wrong Approach

A junior engineer might not be aware of replication. They might try to solve this problem by just taking periodic backups of the database, which would not provide high availability.

Right Approach

A senior engineer would know that replication is a critical part of database administration. They would be able to explain what a replica set is and would have a clear plan for how to set up replication for a production database.

Step 1: Understand What a Replica Set Is

A replica set is a group of Redis servers that maintain the same data set. A replica set provides redundancy and high availability.

Step 2: The Different Types of Nodes

Node TypeDescription
MasterThe master node is the only node that can accept write operations.
SlaveThe slave nodes replicate the data from the master node. They can be used for read operations.

Step 3: Set Up a Replica Set

Here’s how we can set up a simple replica set with one master and two slaves:

1. Configure the master server:

Edit the redis.conf file and set a password for the master server.

requirepass mypassword

2. Configure the slave servers:

Edit the redis.conf file on each slave server and set the following parameters:

replicaof <master_ip> <master_port>
masterauth mypassword

3. Start the servers:

Start the master server and then start the slave servers.

4. Check the status of the replica set:

You can check the status of the replica set by running the INFO replication command on the master server.

Failover

If the master node goes down, you will need to manually promote one of the slave nodes to be the new master. You can do this by running the REPLICAOF NO ONE command on the slave node.

For automatic failover, you can use Redis Sentinel.

Practice Question

You have a three-node replica set and you want to be able to perform reads from the slave nodes. What should you do?