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, Redis offers two features: WAIT command and synchronous replication.

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.

As of Redis 7.0, there is also support for synchronous replication to certain replicas by using the ASYNC command line option when making changes. This feature allows you to specify which replicas should receive updates synchronously and which should continue to receive them asynchronously.

Remember, while these features can mitigate the risk of data loss, they are not a replacement for regular backups.

Was this content helpful?

Start building today

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