Introducing Dragonfly Cloud! Learn More

Question: How does replication consistency work in Redis?

Answer

Redis uses a replication model where data is copied from a master node to any number of slave nodes. This allows for high read scalability and data redundancy.

The replication process in Redis is asynchronous, meaning that the master will continue to handle requests while sending updates to its replicas. This behavior leads to eventual consistency wherein all replicas will eventually reflect the changes made on the master given enough time.

However, this also means that there can be temporary inconsistencies between the master and the slave nodes. For instance, if a client writes data to the master and immediately reads the same data from a replica, it might not see the updated data because the replica may not yet have received the update from the master.

To ensure stronger consistency, Redis offers a couple of options:

  1. "WAIT" command: This command can be used after write operations to enforce synchronous replication up to a specified number of replicas. It blocks until the specified number of replicas have received the recent write operation or the specified timeout has elapsed. Here's an example:

    r = redis.Redis() pipe = r.pipeline() pipe.set('key', 'value') pipe.execute_command("WAIT", 1, 1000) pipe.execute()

    In this code, we wait for at least one replica to acknowledge the receipt of the write operation within 1000 milliseconds. This increases the likelihood that the write will be retained during failover, but it does not guarantee strong consistency (linearizability).

  2. Read from the master: If your application cannot tolerate stale reads, you can ensure consistency by only reading from the master. However, this reduces the scalability benefit provided by having multiple read replicas.

    r = redis.Redis(host='localhost', port=6379, db=0) r.set('key', 'value') print(r.get('key'))

    In this example, both the write and read operations are performed on the master, ensuring that you always get the most recent data.

It's important to note that while the WAIT command can help make failover more robust by ensuring more replicas have the write operation, it does not provide a hard guarantee of strong consistency. Redis Sentinel or Redis Cluster handle failover, and they promote the slave that appears to preserve the greatest amount of data. Despite that, there can still be failure scenarios where acknowledged writes are lost.

Therefore, balancing between consistency requirements and performance based on your application's needs is crucial, as ensuring stronger consistency can increase latency and reduce throughput.

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.