Question: How can one set up read-only replication in Redis?

Answer

Redis Replication is a feature in which data from one Redis server (the master) is replicated to any number of other Redis servers (the slaves). By default, both read and write operations are allowed on the master and all its slave nodes. However, it's possible to configure certain replicas as read-only.

To setup read-only replicas, you would need to:

  1. Set up replication: This is done by using the SLAVEOF command in Redis, where you specify the master's IP address and port. To make a Redis server a replica of another server, use the following command:
redis-cli SLAVEOF <master-ip> <master-port>
  1. Make the replica read-only: Use the CONFIG SET command to make a Redis server read-only. This will prevent any client from writing to the database. However, it does not stop the server acting as a slave from receiving write commands from its master server. Use the command:
redis-cli CONFIG SET slave-read-only yes

While these commands can be run directly from the command line or included in an initialization script, it's also possible to include them in the redis configuration file:

slaveof <master-ip> <master-port> slave-read-only yes

These lines can be placed in the 'redis.conf' file of the replica server.

Please note that the slave-read-only configuration option was renamed to replica-read-only since Redis 5.0, so if you're using a newer version of Redis, you should use replica-read-only instead.

Finally, ensure you have proper security measures in place when using Redis replication, as it does not implement any form of encryption or secure communication between the master and the replica servers. So, sensitive data can be exposed if it's transmitted over an insecure network.

Was this content helpful?

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.