Redis Persistence Mechanisms: AOF, RDB, and Hybrid Persistence
Redis offers three persistence options—Append‑Only File (AOF) which logs every write, RDB snapshots that capture point‑in‑time data, and a hybrid mode combining both—each configurable with trade‑offs in durability, recovery speed, file size, and performance to suit different application needs.
This article introduces the three major persistence mechanisms provided by Redis: Append Only File (AOF) logs, RDB snapshots, and the hybrid persistence mechanism that combines both.
Why Redis Needs Persistence
Redis stores all data in memory to achieve high performance, but this means that data will be lost if the server crashes. To avoid data loss, Redis can persist in‑memory data to disk and reload it on restart.
1. AOF (Append Only File)
1.1 Overview
AOF records every write command in real time. When Redis restarts, it replays the commands to restore data. It follows a write‑behind model: the command is executed in memory first, then appended to the AOF file.
1.2 Enabling AOF
AOF is disabled by default because it impacts performance. It can be enabled by setting the appropriate options in redis.conf or by using the CONFIG SET command.
1.3 Configuration Items
Specify AOF file name and directory with appendfilename and appenddirname .
Control fsync policy with appendfsync (Always, Everysec, No).
Set no-appendfsync-on-rewrite to decide whether to fsync during AOF rewrite or RDB save.
Configure automatic rewrite thresholds with auto-aof-rewrite-percentage and auto-aof-rewrite-min-size .
Handle truncated AOF files on restart with aof-load-truncated .
Enable hybrid persistence with aof-use-rdb-preamble .
1.4 AOF File Content
Each command is stored in the Redis protocol format, e.g., *3\r\n$3\r\nset\r\n$7\r\ntestkey\r\n$9\r\ntestvalue\r\n .
1.5 Write‑Back Strategies
Redis provides three fsync policies:
Configuration
Write‑Back Strategy
Advantages
Disadvantages
Always
Synchronous write‑back
High reliability, almost no data loss
Significant performance impact
Everysec
Write‑back every second
Moderate performance impact
Possible loss of up to 1 second of data
No
OS‑controlled write‑back
Best performance
Poor reliability, possible large data loss
1.6 AOF Rewrite
To prevent unlimited growth, Redis rewrites the AOF file by creating a new file that contains a minimal set of commands representing the current dataset. The rewrite is performed by a child process ( BGREWRITEAOF ) while the main thread continues handling new writes.
1.7 Advantages and Disadvantages of AOF
Advantages: higher data safety, can recover from accidental deletions, supports point‑in‑time recovery.
Disadvantages: larger file size compared to RDB, lower write QPS.
2. RDB (Redis Database Snapshots)
2.1 Overview
RDB creates point‑in‑time snapshots of the dataset at configured intervals. The snapshot file can be loaded quickly on restart.
2.2 Trigger Methods
SAVE : blocks the server while creating a snapshot.
BGSAVE : forks a child process to write the snapshot asynchronously; only the fork operation blocks briefly.
Automatic triggers defined by the save directive in redis.conf (e.g., “save 900 1”).
2.3 Configuration Items
Enable/disable writes after a failed BGSAVE with stop-writes-on-bgsave-error .
Compress snapshots using LZF via rdbcompression .
Enable/disable CRC64 checksum with rdbchecksum .
Set snapshot filename and directory with dbfilename and dir .
2.4 Copy‑On‑Write (COW) Technique
During BGSAVE , the child process shares the parent’s memory pages. When the parent modifies a page, a copy is made, allowing the child to write the original data to the snapshot without blocking the main thread.
2.5 RDB File Format
Header “REDIS”.
4‑byte version number.
Database entries (DB number, key‑value pairs).
EOF marker.
Checksum.
2.6 Advantages and Disadvantages of RDB
Advantages: low memory impact, fast recovery, suitable for cold backups.
Disadvantages: possible data loss between snapshots, brief blocking during fork for large datasets.
3. Hybrid Persistence
Redis 4.0 introduced a hybrid mode that writes incremental changes to AOF while periodically taking RDB snapshots. The configuration item aof-use-rdb-preamble enables this mode, combining the fast recovery of RDB with the durability of AOF.
4. Choosing a Persistence Strategy
If data safety is critical, choose AOF or hybrid persistence.
If fast recovery is needed, choose RDB or hybrid.
If file size matters, choose RDB with compression.
If performance is paramount, choose RDB or hybrid.
For cache‑only use cases, persistence can be disabled.
5. References
https://www.tkcnn.com/redis/manual/persistence.html
https://zhuanlan.zhihu.com/p/684331393
https://blog.csdn.net/weixin_43412762/article/details/134795648
https://developer.aliyun.com/article/1419933?spm=5176.26934562.main.1.1732527dM6Snpw
vivo Internet Technology
Sharing practical vivo Internet technology insights and salon events, plus the latest industry news and hot conferences.
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.