How to Set Up Redis Replication: Simple Guide
To set up
Redis replication, configure a Redis server as a master and one or more as replicas by adding replicaof <master-ip> <master-port> in the replica's configuration or via the REPLICAOF command. This makes replicas copy data from the master automatically.Syntax
Redis replication involves a master server and one or more replica servers. The replica servers use the replicaof <master-ip> <master-port> directive in their configuration file (redis.conf) or the REPLICAOF command at runtime to start replicating data from the master.
replicaof: Tells the Redis server to become a replica of the specified master.<master-ip>: IP address of the master Redis server.<master-port>: Port number of the master Redis server (default is 6379).
redis
replicaof <master-ip> <master-port>
Example
This example shows how to configure a Redis replica to replicate from a master running on IP 192.168.1.100 and port 6379. You can add this line to the replica's redis.conf file or run the command in the Redis CLI.
redis
# On replica server redis.conf replicaof 192.168.1.100 6379 # Or in Redis CLI on replica server > REPLICAOF 192.168.1.100 6379
Output
OK
Common Pitfalls
- Not setting replicaof correctly: Forgetting to specify the correct master IP and port will prevent replication.
- Firewall or network issues: The replica must be able to connect to the master on the specified port.
- Using
slaveofinstead ofreplicaof:slaveofis deprecated; always usereplicaof. - Master requiring authentication: If the master uses a password, replicas must be configured with
masterauthto authenticate.
redis
## Wrong (deprecated): slaveof 192.168.1.100 6379 ## Correct: replicaof 192.168.1.100 6379
Quick Reference
| Directive/Command | Purpose |
|---|---|
| replicaof | Set the server as a replica of the master at given IP and port |
| REPLICAOF | Runtime command to start replication from master |
| masterauth | Set password to authenticate with master if required |
| replicaof no one | Make the server stop replicating and become a master |
Key Takeaways
Use the replicaof directive or REPLICAOF command to configure replicas.
Ensure network connectivity between master and replicas on the Redis port.
Use masterauth if the master requires a password for replication.
Avoid deprecated slaveof; always use replicaof for replication setup.
Replicas automatically sync data from the master once configured.