Understanding Redis Persistence: RDB and AOF Overview
This article provides a comprehensive overview of Redis persistence mechanisms, detailing the concepts, features, advantages, disadvantages, trigger strategies, operational processes, configuration options, and troubleshooting steps for both RDB (snapshot) and AOF (append‑only file) approaches.
1. RDB (Redis Database) Overview
RDB persistence creates snapshots of the dataset at specified intervals (N seconds or after M changes), storing a full backup of the data.
2. RDB Features
2.1 Advantages
Single file, easy to transfer, suitable for disaster recovery.
Faster recovery of large datasets compared to AOF.
Backup performed by a forked child process, leaving the parent process free of additional I/O, resulting in better performance than AOF.
2.2 Disadvantages
Unexpected crashes between intervals can cause significant data loss.
Forking a child process for very large datasets can be time‑consuming and may affect normal request latency.
3. RDB Strategies
RDB backup can be triggered automatically or manually.
3.1 Automatic Triggers
Based on the save rules configured in the Redis config.
Executing FLUSHALL command.
Graceful shutdown of Redis.
3.2 Manual Triggers
SAVE – synchronous execution.
BGSAVE – asynchronous execution.
3.3 Operation Process
1. Fork a child process; the fork operation does not duplicate data, improving speed and reducing memory usage (kernel‑level fork() ).
2. The child process receives pointers to all data.
3. If data changes during the fork, copy‑on‑write (COW) creates separate copies for the parent and child.
4. The child writes the pointed‑to data to the backup file.
5. Backup completes.
4. RDB Configuration
Configuration Item
Description
save
Save after N seconds or M changes (triggers BGSAVE).
stop-writes-on-bgsave-error
Disallow writes if snapshot fails.
rdbcompression
Enable/disable RDB file compression.
rdbchecksum
Enable/disable RC64 checksum.
dbfilenameRDB
Name of the RDB file.
dirRDB
Directory where the RDB file is stored.
5. Additional RDB Notes
5.1 Point‑in‑time Consistency
After a snapshot, any subsequent modifications are retained at their original timestamps, preventing temporal inconsistencies.
5.2 Size Recommendations
It is advisable to keep Redis memory usage under 10‑15 GB to avoid slowing down RDB writes.
5.3 Repairing Corrupted RDB Files
Use the redis-check-rdb tool provided in the Redis installation directory to repair damaged snapshots.
6. AOF (Append Only File) Overview
AOF persistence logs every write operation; on restart, Redis replays the logged commands to reconstruct the dataset. AOF files can be rewritten and compressed to control size.
6.1 AOF File Protocol Example
# Assuming the client executed SET KEY VALUE, the AOF content would be:
*3
$3
SET
$3
KEY
$5
VALUEThe asterisk (*) indicates the number of subsequent lines to read, while the dollar sign ($) specifies the length of the following line.
7. AOF Features
7.1 Advantages
Minimal data loss on crashes (typically ≤2 seconds).
7.2 Disadvantages
Slower data recovery compared to RDB.
AOF files are larger than RDB files.
Performance depends on the chosen appendfsync strategy; it can be slower than RDB.
8. AOF Strategies
8.1 Automatic Triggers (appendfsync)
no – never fsync; writes are flushed when the OS buffer fills (fast).
everysec – default; fsync every second.
always – fsync after every write (slow but safest).
8.2 Manual Trigger
BGREWRITEAOF – asynchronous rewrite of the AOF file.
9. AOF Configuration
Configuration Item
Description
appendonly
Enable or disable AOF.
AOFappendfilename
Name of the AOF file.
appendfsync
Persistence strategy for AOF.
no-appendfsync-on-rewrite
Delay fsync during AOF rewrite.
auto-aof-rewrite-percentage
Growth percentage that triggers AOF rewrite.
auto-aof-rewrite-min-size
Minimum size to trigger AOF rewrite.
aof-load-truncated
Load truncated AOF files.
aof-use-rdb-preamble
Enable mixed RDB‑AOF persistence (available from Redis 4.0).
10. Additional AOF Information
10.1 AOF Rewrite Version Differences
Pre‑4.0: Remove canceling commands and merge duplicates.
Post‑4.0: Write a full RDB snapshot first, then continue logging new writes as AOF.
10.2 Repairing Corrupted AOF Files
Because AOF is an incremental backup, unexpected crashes can corrupt the file, preventing Redis from loading it on restart.
Remediation steps:
Regularly back up AOF files.
Use the redis-check-aof tool in the Redis installation directory to repair the file, then compare differences with diff -u .
Aikesheng Open Source Community
The Aikesheng Open Source Community provides stable, enterprise‑grade MySQL open‑source tools and services, releases a premium open‑source component each year (1024), and continuously operates and maintains them.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.