Mastering Redis Persistence: RDB, AOF, and Hybrid Strategies Explained
This article explains Redis’s persistence mechanisms—including RDB snapshots, AOF append‑only logs, and the hybrid approach—detailing how to enable each method, their commands, configuration options, performance trade‑offs, rewrite processes, and recovery procedures to help you choose the right strategy for reliable data durability.
RDB Snapshot (Redis Database)
RDB is a snapshot‑based persistence method that saves the in‑memory data of Redis at a specific moment to a disk file, defaulting to dump.rdb. When the Redis server starts, it reloads the dump.rdb file to restore data into memory.
Enabling RDB Persistence
You can trigger RDB generation by sending the SAVE or BGSAVE command from a client, or by configuring trigger conditions in the Redis configuration file. Each execution creates a new RDB file that overwrites the previous snapshot.
Method 1: SAVE command
# 同步数据到磁盘上
> saveWhen a client sends the SAVE command, the server blocks subsequent client requests until the data sync completes. If the dataset is large, the block can be lengthy, so SAVE is not recommended in production.
Method 2: BGSAVE command
# 异步保存数据集到磁盘上
> bgsaveWhen a client issues BGSAVE, the Redis master process forks a child process to write data to an RDB file; the child exits after completion.
Compared with SAVE, the child process handles I/O while the master continues to accept other requests, but the fork operation itself is synchronous, so a lengthy fork can still cause temporary blocking.
Controlling the maximum memory of a Redis instance, reducing automatic trigger frequency, or using manual triggers can mitigate fork overhead.
Method 3: Automatic Trigger via Configuration File
Automatic triggers occur in the following scenarios:
Based on the save m n rules defined in the config.
During full replication to a slave, the master sends the RDB file and triggers BGSAVE.
When executing DEBUG RELOAD.
During SHUTDOWN if AOF is not enabled.
Configuration example for automatic triggers:
# 时间策略
# 关闭RDB 只需要将所有的save保存策略注释掉即可
save 900 1 # 900s 内如果有 1 条数据写入,就产生 RDB 快照
save 300 10 # 300s 内有 10 条数据写入,就产生 RDB 快照
save 60 10000 # 60s 内如果有 10000 条数据写入,就产生 RDB 快照
# 文件名称
dbfilename dump.rdb
# 文件保存路径
dir /var/lib/redis/6379
# 持久化出错时是否停止写入
stop-writes-on-bgsave-error yes
# 是否压缩(建议关闭)
rdbcompression no
# 导入时是否检查校验
rdbchecksum yesComparison of SAVE and BGSAVE
The automatic generation of RDB files via the configuration file uses the BGSAVE method.
RDB File Creation Process
The three ways to generate an RDB file follow the same steps:
Generate a temporary RDB file and write data into it.
After data writing completes, replace the official RDB file with the temporary one.
Delete the old DB file.
Copy‑On‑Write (COW)
When Redis forks a child process, the child shares the parent’s memory pages. If the child does not modify a page, the physical memory is not duplicated, making the fork operation fast.
When the parent modifies data, it creates a new copy of the affected pages (COW). The child continues to see the old data until the next BGSAVE, when a new child is forked.
AOF (Append‑Only File)
Unlike RDB snapshots, AOF records every write operation issued by clients, appending them to an .aof file. On restart, Redis replays the AOF commands to restore data.
Enabling AOF Persistence
Method 1: BGREWRITEAOF command
> bgrewriteaofMethod 2: Configuration File
By default AOF is disabled; you can enable it and fine‑tune its behavior in redis.conf:
# 开启 aof
appendonly yes
# 文件名称
appendfilename "appendonly.aof"
# 同步方式(默认每秒一次)
appendfsync everysec
# appendfsync always # 每次写入都同步,极慢但最安全
# appendfsync no # 从不同步,最快但最不安全Rewrite
AOF stores every write command, so the file can grow large. Redis provides a rewrite operation that creates a compacted AOF file.
127.0.0.1:6379> INCR readcount (integer) 1
127.0.0.1:6379> INCR readcount (integer) 2
127.0.0.1:6379> INCR readcount (integer) 3
127.0.0.1:6379> INCR readcount (integer) 4
127.0.0.1:6379> INCR readcount (integer) 5The above lines are RESP protocol entries; the asterisk indicates the number of arguments and the dollar sign indicates the length of each argument.
[root@redis 6379]# cat appendonly.aof
*2
$6
SELECT
$1
0
*2
$4
INCR
$9
readcount
*2
$4
INCR
$9
readcount
*2
$4
INCR
$9
readcount
*2
$4
INCR
$9
readcount
*2
$4
INCR
$9
readcountManual rewrite command:
127.0.0.1:6379> BGREWRITEAOF
Background append only file rewriting startedAfter rewrite, multiple INCR commands are merged into a single SET:
[root@redis 6379]# cat appendonly.aof
*2
$6
SELECT
$1
0
*3
$3
SET
$9
readcount
$1
5Rewrite Configuration Parameters
Redis forks a child process for AOF rewrite, minimizing impact on normal command processing:
auto-aof-rewrite-min-size 64mb # Only rewrite when AOF >= 64 MB
auto-aof-rewrite-percentage 100 # Rewrite when size grows 100% since last rewriteAOF Rewrite Flowchart
During rewrite, the parent continues to serve commands; writes are also appended to the old AOF to guarantee completeness.
The child keeps a buffer for writes that occur during rewrite, ensuring no data loss.
The rewrite generates commands directly from the current in‑memory data without parsing the old AOF.
Both RDB and AOF first write to a temporary file and then rename it to replace the original.
Hybrid Persistence
When Redis restarts, using only RDB can lose recent data, while replaying a full AOF can be slow for large datasets. Redis 4.0 introduces hybrid persistence, which combines an RDB preamble with incremental AOF writes.
aof-use-rdb-preamble yes # Enable hybrid persistenceWith hybrid persistence, the AOF rewrite writes an RDB snapshot of the memory state up to the rewrite point, followed by incremental AOF commands. The resulting file is first a temporary AOF; after rewrite it replaces the original, allowing fast startup by loading the RDB portion first and then replaying the remaining AOF commands.
127.0.0.1:6379> set k 1
OK
127.0.0.1:6379> set k 2
OK
127.0.0.1:6379> BGREWRITEAOF
Background append only file rewriting startedViewing the hybrid AOF file after rewrite shows the RDB content followed by the new commands:
[root@redis 6379]# cat appendonly.aof
REDIS0009…redis-ver5.0.7…
… (binary RDB preamble) …
*2
$6
SELECT
$1
0
*3
$3
SET
$1
k
$1
3Appending new data after the rewrite adds regular RESP commands to the end of the file.
127.0.0.1:6379> set k 3
OK [root@redis 6379]# cat appendonly.aof
REDIS0009…redis-ver5.0.7…
… (binary RDB preamble) …
*2
$6
SELECT
$1
0
*3
$3
SET
$1
k
$1
3Hybrid AOF file structure:
Recovering Data from Persistence
To restore data, simply restart Redis. If both RDB and AOF files exist, Redis checks for the AOF first; if present, it loads the AOF because it provides a more complete data set (typically losing at most one second of writes).
RDB vs AOF Comparison
RDB does not support incremental backups; it only produces a single dump.rdb file.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
