How to Configure Master Slave in Redis: Simple Guide
To configure
master-slave replication in Redis, set up one Redis instance as the master and configure one or more instances as replicas by specifying the master's IP and port using the replicaof directive. This allows replicas to replicate data from the master automatically.Syntax
Use the replicaof <master-ip> <master-port> command in the replica's configuration file or CLI to link it to the master. The master runs normally without special settings.
- replicaof: Command to set the replica's master.
- master-ip: IP address of the master Redis server.
- master-port: Port number of the master Redis server (default 6379).
redis
replicaof 192.168.1.100 6379
Example
This example shows how to configure a master and a replica Redis instance on the same machine using different ports.
plaintext
# Master configuration (redis-master.conf) port 6379 # Replica configuration (redis-replica.conf) port 6380 replicaof 127.0.0.1 6379
Output
Starting Redis master on port 6379
Starting Redis replica on port 6380 replicating from 127.0.0.1:6379
Replica sync completed successfully
Common Pitfalls
- Forgetting to restart the Redis replica after changing
replicaofsetting. - Using
slaveofinstead ofreplicaofin Redis 5.0 and later (deprecated). - Network issues blocking communication between master and replica.
- Not setting unique ports for master and replica on the same machine.
redis
Wrong (deprecated): slaveof 127.0.0.1 6379 Right (current): replicaof 127.0.0.1 6379
Quick Reference
Remember these key points for Redis master-replica setup:
- The master is the main Redis server; replicas replicate from it.
- Use
replicaofin replica config to point to master. - Restart Redis after config changes.
- Check network connectivity between master and replicas.
Key Takeaways
Use the replicaof command in the replica config to link it to the master.
The master requires no special config; replicas replicate automatically after setup.
Always restart Redis instances after changing replication settings.
Avoid using deprecated slaveof command; use replicaof instead.
Ensure network connectivity and unique ports for master and replicas.