Redis Persistence: RDB Snapshots, AOF Write‑Ahead Log, and Hybrid Persistence
Redis provides two primary persistence mechanisms—RDB snapshots that capture the dataset at specific moments and AOF write‑ahead logs that record every write command—along with a hybrid model combining both, each with distinct strategies, advantages, drawbacks, and configuration options to ensure data durability and fast recovery.
Redis Persistence Overview
Redis stores data in memory, so without persistence the data would be lost on restart or failure. Persistence ensures that data can be recovered after a crash, preventing a complete fallback to MySQL.
RDB (Redis Database) Snapshots
What is an RDB snapshot?
An RDB snapshot captures the state of all data in memory at a specific point in time and writes it to a binary, compressed file on disk.
Generating RDB Files
Redis does not create an RDB file on every write. It creates a snapshot only when instructed, using two commands:
SAVE : blocks the server until the RDB file is finished.
BGSAVE : forks a child process to create the RDB file while the parent continues to serve requests.
In the default configuration, BGSAVE is triggered automatically when certain conditions are met. Example configuration:
save 900 1 # after 900 seconds (15 minutes) if at least 1 key changed
save 300 10 # after 300 seconds (5 minutes) if at least 10 keys changed
save 60 10000 # after 60 seconds (1 minute) if at least 10000 keys changedRDB Implementation Details
During snapshot creation Redis uses the operating system's copy‑on‑write (COW) mechanism. The parent process forks a child; the child writes the snapshot while the parent continues handling client requests. Expired keys are omitted from the snapshot.
Advantages and Disadvantages of RDB
Advantages Compact binary file with compression; fast recovery. Minimal impact on the main process because persistence work is delegated to a child.
Disadvantages Potential data loss between the last snapshot and a crash. Forking a child can pause the main thread for several milliseconds or even seconds for large datasets. Frequent full snapshots increase disk I/O and can cause performance degradation.
AOF (Append‑Only File) Write‑Ahead Log
What is AOF?
AOF records every write command in the Redis protocol format to an append‑only file. Replaying the file on an empty instance restores the exact data state.
Write‑Behind Strategies
Redis buffers AOF writes in memory and flushes them to disk according to the appendfsync setting:
appendfsync always # flush to disk after every write
appendfsync everysec # flush once per second (default)
appendfsync no # let the OS decide when to flushChoosing a strategy balances durability against performance.
AOF Rewrite Mechanism
Because AOF grows continuously, Redis provides bgrewriteaof to create a compacted AOF file. A child process traverses the in‑memory dataset, writes a minimal set of commands to a new file, and then appends any incremental writes that occurred during the rewrite before atomically replacing the old file.
Advantages and Disadvantages of AOF
Advantages Higher durability; flexible fsync policies. Plain‑text log that is easy to read and modify. Automatic background rewrite when the file becomes large.
Disadvantages File size is typically larger than RDB. Depending on the fsync policy, write performance can be slower than RDB.
Hybrid Persistence Model
Redis 4.0 introduced a hybrid persistence option that stores an RDB snapshot together with the incremental AOF generated after the snapshot. On restart, Redis loads the RDB file first and then replays the small incremental AOF, achieving fast recovery while minimizing data loss.
Summary
Redis offers RDB snapshots for point‑in‑time persistence.
Copy‑on‑write (COW) and BGSAVE avoid blocking reads/writes during snapshot creation.
AOF records every write command, providing higher durability.
AOF rewrite compacts the log to prevent uncontrolled growth.
The hybrid model combines RDB speed with AOF reliability for robust recovery.
政采云技术
ZCY Technology Team (Zero), based in Hangzhou, is a growth-oriented team passionate about technology and craftsmanship. With around 500 members, we are building comprehensive engineering, project management, and talent development systems. We are committed to innovation and creating a cloud service ecosystem for government and enterprise procurement. We look forward to your joining us.
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.