Dragonfly Cloud announces new enterprise security features - learn more

Question: How can you configure replication in Redis?

Answer

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:

  1. 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 6379.

  2. 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:

slaveof 127.0.0.1 6379

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:

redis-cli SLAVEOF 127.0.0.1 6379

After issuing this command, the slave begins to replicate the master immediately.

  1. Verification: You can verify the replication status by running the INFO REPLICATION command in redis-cli.
redis-cli INFO REPLICATION

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.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost