Introducing Dragonfly Cloud! Learn More

Question: Is Redis replication synchronous or asynchronous?

Answer

No, Redis replication is not synchronous; it's asynchronous. When data is written to the primary Redis server, the changes are sent to replica servers asynchronously. This means that the primary server will continue its operations without waiting for the replicas to acknowledge that they have received and processed the data.

Here's a simple example of how to set up replication:

# On the primary server redis-server --port 6379 # On the replica server redis-server --port 6380 --slaveof <primary_host> 6379

In the above example, <primary_host> would be the IP address or hostname of the primary server.

There are several benefits to this asynchronous approach. It minimizes latency on the primary server because it doesn't have to wait for all replicas to acknowledge writes. However, there's a potential risk of data loss if the primary server crashes before the data can be replicated to the replicas.

To address this limitation, the WAIT command can be used. The WAIT command is a mechanism that allows you to ensure a specified number of replicas have saved the latest changes before the execution of the command returns. This reduces the risk of data loss at the expense of increased latency.

# Example usage of the WAIT command in redis-cli 127.0.0.1:6379> SET foo bar OK 127.0.0.1:6379> WAIT 1 1000 (integer) 1

In this example, WAIT pauses the client until the change to key foo has been picked up by at least one replica or 1000 milliseconds have passed. Remember, while WAIT can mitigate the risk of data loss, it is just a best-effort attempt and does not make Redis a strongly consistent store.

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.