Redis - Backup
Redis - Persistence & Backup Strategies
Section titled “Redis - Persistence & Backup Strategies”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 (Redis Database) Snapshotting
Section titled “RDB (Redis Database) Snapshotting”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.
Triggering a Snapshot
Section titled “Triggering a Snapshot”You can trigger a snapshot manually with two commands:
127.0.0.1:6379> BGSAVEBackground saving startedBGSAVE (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> SAVEOKSAVE 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 (Append Only File) Persistence
Section titled “AOF (Append Only File) Persistence”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.
RDB vs. AOF: Which to Use?
Section titled “RDB vs. AOF: Which to Use?”| Factor | RDB (Snapshotting) | AOF (Append Only File) |
|---|---|---|
| Durability | Lower. 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 Size | Very compact binary file. | Larger than RDB, as it’s a log of operations. Can be rewritten to shrink. |
| Performance | Parent 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 Case | Ideal for backups, disaster recovery, and faster restarts with acceptable data loss. | Ideal for primary persistence where minimizing data loss is critical. |
Best Practice: Use Both
Section titled “Best Practice: Use Both”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 Data
Section titled “Restoring Data”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.