Question: How does replication work in Redis?


Redis supports data replication using a master-slave model. This feature allows one or multiple Redis servers (the slaves) to replicate the data of another Redis server (the master). It's a key component for maintaining high availability and data redundancy.

The process begins when a slave connects to a master, sending a SYNC command. The master then starts background saving, collects all subsequent commands received that will modify the dataset, and sends the database file to the slave, which saves it on disk, loads into memory, and starts executing the accumulated commands to catch up with the Master's dataset.

Here is an example of how to set up replication:

# On the master: redis-server --port 6379 # On each slave: redis-server --port 6380 --slaveof localhost 6379

In this example, the second command starts a Redis server on port 6380 and configures it as a slave of the master running on localhost:6379.

Note that if the connection between the master and a slave is lost, the replication process will automatically attempt to resume from where it left off, ensuring data consistency across your Redis servers.

Redis's replication is beneficial for read scalability as reads can be distributed over multiple slave servers. However, write scalability remains limited because all writes must go to the master.

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

Start building today

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