Skip to content

Redis - Backup

While Redis is an in-memory database, it offers robust mechanisms for persisting data to disk. This is essential for durability, backups, and restarts. Redis provides two main persistence models: RDB and AOF.

RDB persistence performs point-in-time snapshots of your dataset at specified intervals. It creates a compact, binary file (dump.rdb by default) that is fast to load, making it ideal for backups and quick restarts.

You can trigger a snapshot manually with two commands:

127.0.0.1:6379> BGSAVE
Background saving started

BGSAVE (Background Save) is the recommended command. It forks the main Redis process, allowing the parent to continue serving requests while the child process writes the database to disk. This is non-blocking.

127.0.0.1:6379> SAVE
OK

SAVE is a synchronous, blocking command. It will freeze the server, preventing it from serving any other requests until the snapshot is complete. This should be avoided on production systems.

AOF persistence logs every write operation received by the server. This log, stored in a file (e.g., appendonly.aof), is replayed on startup to reconstruct the original dataset. Because it logs every change, AOF provides much better durability than RDB.

AOF is enabled in your redis.conf file. As the AOF file grows, Redis can automatically rewrite it in the background to a more compact format using the BGREWRITEAOF command.

FactorRDB (Snapshotting)AOF (Append Only File)
DurabilityLower. You can lose the last few minutes of data between snapshots.Much higher. You can configure it to sync every second, losing at most 1 second of data.
File SizeVery compact binary file.Larger than RDB, as it’s a log of operations. Can be rewritten to shrink.
PerformanceParent process performance is high (uses a forked child). Restarts are fast due to compact file.Can have a slight performance impact on writes depending on sync policy. Restarts can be slower as commands are replayed.
Use CaseIdeal for backups, disaster recovery, and faster restarts with acceptable data loss.Ideal for primary persistence where minimizing data loss is critical.

For maximum data safety, the industry best practice is to enable both persistence methods. AOF ensures durability against crashes, while RDB provides a reliable point-in-time backup for disaster recovery or migrations.

Restoring is typically automatic. Simply place the dump.rdb or appendonly.aof file in the Redis working directory (specified by the dir config in redis.conf) and restart the server. If both files are present, Redis will prioritize the AOF file for restoration as it guarantees better data durability.