Question: How does Redis handle replication and persistence?

Answer

Redis supports both data replication and persistence, which are crucial for ensuring data integrity, availability, and durability in a distributed system.

Replication: Redis uses asynchronous replication. A master can have multiple slaves, and the data is replicated to any number of slave Redis servers. Slaves are also capable of accepting connections from other slaves. This capability allows for a variety of configurations, such as chaining different slaves or creating a tree of slaves for larger and more robust systems.

Here's how you set it up:

# On the slave Redis server redis-cli SLAVEOF <master-ip> <master-port>

Persistence: Redis offers two types of persistence - RDB (Redis DataBase file) and AOF (Append Only File).

  1. RDB is a point-in-time snapshot of your dataset at specified intervals. It's perfect for backups or disaster recovery and provides faster restarts with large datasets.

For example, to save the DB on disk (creates a dump.rdb):

redis-cli SAVE
  1. AOF logs every write operation received by the server. It provides better durability as you can set Redis to log every change hitting the disk. AOF files are usually bigger than equivalent RDB files for the same dataset but they can be configured to be synchronized to disk less frequently.

To enable AOF persistence:

redis-cli CONFIG SET appendonly yes

In production environments, it's common to combine both RDB and AOF. The decision depends on your particular use case and understanding the trade-offs between performance, durability, and speed of recovery.

Was this content helpful?

Start building today

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