Question: How does replication work with lists in Redis?

Answer

Redis replication is a key feature that allows the copying of data from a master instance to one or more replica instances. This process is used for redundancy and data availability.

When we talk about 'list replication,' it's important to remember: Redis replication works on a command basis. Any write commands executed on the master are propagated and executed on the replicas. So, it doesn't matter whether the data structure is a list, set, hash, etc.

For example, imagine you execute the following commands on the master:

LPUSH mylist "world" LPUSH mylist "hello"

These commands will be propagated to the replicas as follows:

LPUSH mylist "world" LPUSH mylist "hello"

Each replica will then independently execute these commands to update its local copy of the list. In this way, the list is 'replicated' across all instances.

The Replication feature in Redis is easy to set up and requires minimal configuration. To set up a Redis instance as a replica of another, use the SLAVEOF command, like this:

SLAVEOF master_host master_port

This command makes the current Redis server a replica of the specified master Redis server. Once the connection is established, the master starts sending the replica bulk transfer of all data it has, followed by all new commands it receives.

It's worth noting that Redis uses asynchronous replication. By default, commands are piped from master to its replicas without waiting for acknowledgments. However, it's possible to configure Redis to stop operations if there are less than a specified number of replicas connected or if the lag between master and replica exceeds a certain amount.

Asynchronous replication provides a high-performance solution, but it can lead to data loss in case of abrupt termination of the master instance. Synchronous replication can protect against this, but it comes with an impact on performance. Therefore, you should choose according to your application's requirements.

If data safety and zero data loss are critical for your use case, consider using Redis's AOF persistence and enabling both RDB snapshots and the append-only file (AOF). These settings provide a good balance between performance and durability.

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.