Replication in Redis is a process where a Redis server (master) can accept connections from other Redis servers (slaves) and send them the WRITE commands it receives, so that the data on the master ends up being copied to its slaves.
To configure replication, you have to set up a master-slave relationship between at least two Redis instances. Here's a basic example of how to do it:
Master Configuration: The master doesn't need any specific configuration because by default every Redis server can accept connections from an unlimited number of slaves. However, for demonstration purposes, let's assume our redis master instance is running on
localhost at port
Slave Configuration: To make a Redis server a slave of another Redis server, you need to include the
slaveof directive in your configuration file (redis.conf), or you could use the
SLAVEOF command while the server is running.
Here's an example:
This line in the redis.conf file of the slave tells the Redis instance to replicate the one running on localhost at port 6379.
Alternatively, you can configure a slave dynamically using the
SLAVEOF command in the redis-cli, like this:
After issuing this command, the slave begins to replicate the master immediately.
INFO REPLICATION command in redis-cli.
The output will show you if the instance is a master or a slave, and the state of its replication.
Keep in mind that replication is asynchronous, which means that slaves do not need to be connected all the time in order to replicate their master. They can reconnect and continue replicating where they left off if there's any network interruption.