Master Redis 4.0 Persistence: RDB, AOF, and Hybrid Strategies Explained
Redis 4.0 offers three persistence options—RDB snapshotting, AOF command logging, and a hybrid RDB‑AOF mode—each with distinct configurations, trigger conditions, and trade‑offs, and this guide walks through their principles, practical commands, performance impacts, and recovery procedures with detailed examples.
RDB Persistence
Overview
RDB persistence creates a binary snapshot of the entire dataset when certain conditions are met. On restart, if AOF is disabled, Redis loads the snapshot file (default dump.rdb) to restore data.
Snapshot File Location
The directory is defined by the dir configuration, and the filename by dbfilename.
Snapshot Trigger Conditions
Client executes SAVE or BGSAVE.
Automatic snapshot based on save m n rules.
During master‑slave full synchronization, the master runs BGSAVE.
Client runs FLUSHALL.
Client runs SHUTDOWN.
SAVE Command
The SAVE command forces a synchronous snapshot; Redis blocks all client requests until the snapshot completes.
Practical Steps
Check the last persistence time with INFO Persistence, then run SAVE and verify the updated timestamp.
BGSAVE Command
BGSAVEforks a child process to write the snapshot in the background, allowing the parent to continue serving clients after the fork.
save m n Rule
The rule triggers a snapshot when at least n keys change within m seconds (e.g., save 900 1).
FLUSHALL Trigger
Executing FLUSHALL clears the database and forces a snapshot.
SHUTDOWN Trigger
When Redis shuts down, it saves all data to ensure recovery on next start.
Master‑Slave Trigger
During full replication, the master runs BGSAVE and sends the RDB file to the replica.
Failure Recovery
If Redis crashes and AOF is disabled, it restores data from the latest RDB snapshot.
RDB Configuration
save m n # Snapshot trigger rule
# Example: save 900 1 (snapshot if at least 1 key changes within 900 seconds)
dbfilename dump.rdb # Snapshot file name
stop-write-on-bgsave-error yes # Stop writes if BGSAVE fails
rdbchecksum yes # Enable checksum verification
dir "/etc/redis" # Directory for RDB and AOF files
rdbcompression yes # Enable RDB compressionAOF Persistence
Overview
AOF (Append‑Only File) logs every write command to a file ( appendonly.aof) and is the default recovery method when enabled, offering finer data durability at a performance cost.
Enabling AOF
Set appendonly yes in the configuration or via CONFIG SET appendonly yes to enable without restarting.
AOF Process
Write commands are first buffered in aof_buf, then flushed to disk according to the appendfsync policy: no: No explicit fsync; OS writes every ~30 seconds. always: Fsync after every write (high durability, lower performance). everysec: Write immediately, fsync once per second (default).
AOF Rewrite
When the AOF file grows, Redis rewrites it using BGREWRITEAOF, removing redundant commands, expired data, and merging compatible operations.
Rewrite Trigger Conditions
Manual: client runs BGREWRITEAOF.
Automatic: file size exceeds auto-aof-rewrite-min-size (default 64 MB) and growth percentage exceeds auto-aof-rewrite-percentage (default 100%).
Rewrite Process
Redis forks a child; the child writes a compacted AOF while the parent continues serving clients, buffering new writes in aof_rewrite_buf. After the child finishes, the parent appends the buffered data and replaces the old file.
AOF Implementation Details
AOF stores commands in Redis protocol format (e.g., +, -, *, $, : prefixes). Example snippet:
Data Recovery
When Redis starts with AOF enabled, it first loads the AOF file; if the file is truncated, the aof-load-truncated setting controls whether to load it or abort.
AOF Configuration Parameters
auto-aof-rewrite-min-size 64mb # Minimum size to consider rewrite
auto-aof-rewrite-percentage 100 # Trigger when size doubles
appendfsync everysec # Default sync strategy
aof-load-truncated yes # Load truncated AOF with warning
appendonly no # Enable/disable AOF
appendfilename appendonly.aof # AOF file name
dir /etc/redis # Directory for RDB and AOF filesHybrid RDB‑AOF Persistence (Redis 4.0)
Overview
Hybrid persistence combines an RDB‑style preamble with subsequent AOF entries, offering fast loading (RDB) and minimal data loss (AOF).
Enabling Hybrid Persistence
Set aof-use-rdb-preamble yes via CONFIG SET. It is disabled by default.
Hybrid Process
During a BGREWRITEAOF, the child first writes a full RDB snapshot into the AOF file, then appends incremental commands in AOF format. The resulting file starts with RDB data followed by AOF logs.
Data Recovery
On startup, Redis checks the AOF file: if it begins with an RDB header, it loads the RDB portion first, then processes the remaining AOF commands.
Practical Demonstration
After enabling hybrid persistence, a write operation shows the AOF file containing both RDB and AOF sections.
Advantages and Disadvantages
RDB
Compact file size, fast transfer and recovery.
Minimal impact on Redis performance during snapshot (forked child handles I/O).
Recovery speed is higher for large datasets.
Potential data loss between snapshots; CPU‑intensive for large datasets.
Binary format is not human‑readable.
AOF
Higher data durability (seconds‑level loss depending on sync policy).
Human‑readable command log, compatible across Redis versions.
Larger file size and slower recovery compared to RDB.
Performance overhead due to frequent fsync operations.
Hybrid
Combines fast RDB loading with AOF incremental safety.
Reduced data loss while keeping load times short.
Incompatible with Redis versions prior to 4.0; the RDB preamble reduces readability.
Related Commands
redis-check-aof /etc/redis/appendonly.aof redis-check-rdb /etc/redis/dump.rdb INFO Persistence INFO statsHope this guide helps you master Redis persistence mechanisms!
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.
