Question: How does Redis replication work?


Redis replication is a process in which the same data is stored on multiple Redis servers. The primary server, known as the master, performs all write operations while secondary servers, known as slaves, replicate the data of the master server.

In detail, here's how it works:

  1. The replication process begins when the slave connects to the master.
  2. After the connection is established, the master starts a background saving process and simultaneously buffers all new commands that will alter the dataset.
  3. Once the background saving is complete, the master transfers the database file to the slave, which stores the file on disk and loads it into memory.
  4. After this initial sync, all new commands from the client that will modify the dataset are sent to the slaves by the master.
  5. Each slave server is capable of further transferring its data to more slave servers in a cascading pattern.

Here is a basic example of how you might set up replication in Redis:

# On the master machine: redis-server --port 6379 # On the slave machine: redis-server --slaveof master_host 6379

In this example, the 'master_host' would be replaced with the IP address or hostname of the master machine. Port 6379 is the standard Redis port.

Keep in mind that Redis uses asynchronous replication: slaves do not need to acknowledge the reception of each write operation. In general, this results in high performance but at the risk of potential data loss if a master crashes before the slaves have had time to save the latest updates. However, Redis enables you to configure the number of slaves and the maximum lag between master and slave to mitigate this risk.

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.