Redis Replication Tutorial
Introduction to Replication
Replication is a critical feature in Redis that allows data to be copied from one Redis server (the master) to one or more Redis servers (the replicas). This helps in improving availability, balancing the load, and ensuring data redundancy.
Setting Up Replication
To set up replication, follow these steps:
Step 1: Configure the master server. Ensure your Redis master server is running.
Step 2: Configure the replica servers. Add the following line to your replica's redis.conf
file:
replicaof
For example, if your master is running on 192.168.1.100
and port 6379
, you would use:
replicaof 192.168.1.100 6379
Verifying Replication
To verify that replication is working correctly, start the Redis replica server and then run the info replication
command on the replica:
info replication
You should see output similar to this:
# Replication role:slave master_host:192.168.1.100 master_port:6379 master_link_status:up
Handling Master Failures
If the master fails, the replicas can be promoted to masters to ensure continued availability. This process can be manually executed by using the slaveof no one
command on the replica that you want to promote.
slaveof no one
After running this command, the replica will become a master.
Reconfiguring after Failover
Once the master is back up, you may need to reconfigure the replication setup. To do this, you can use the slaveof
command on the previous master to make it a replica of the new master.
slaveof
Advanced Configuration Options
Redis offers several advanced options for replication, such as configuring the replica to serve reads, setting a replica read-only, and adjusting the replica priority for automatic failover. These options can be set in the redis.conf
file.
For example, to set a replica to read-only mode, add the following line:
replica-read-only yes