Redis Persistence Explained: RDB Snapshots vs AOF Logging
The article narrates how Redis, personified as a character, discovers data loss risks, implements RDB snapshotting and configurable save intervals, learns from MySQL's binary log, adopts Append‑Only File logging with flush policies, and finally adds AOF rewrite to keep storage efficient.
RDB Persistence
Redis realizes that without persistence its in‑memory data disappears after a crash, so it adopts the RDB (Redis Database) snapshot method: periodically traversing all data in memory and writing it to a binary RDB file. To save space, Redis defines a custom binary format. Because full snapshots are costly, Redis introduces configurable save parameters that trigger a snapshot only when certain write activity thresholds are met.
save 900 1 # at least one write within 900 seconds save 300 10 # at least ten writes within 300 seconds save 60 10000 # at least ten thousand writes within 60 seconds
When any condition is satisfied, Redis forks a child process to perform the snapshot, avoiding blocking the main server.
MySQL Binlog Inspiration
Redis observes MySQL’s binary log files (e.g., mysql-bin.000001) that record every data‑modifying operation such as INSERT, UPDATE, and DELETE. These logs enable point‑in‑time recovery, prompting Redis to consider a similar logging approach.
AOF Persistence
Redis implements Append‑Only File (AOF) persistence, which records every write command to a dedicated file. To avoid performance loss, Redis buffers commands in an in‑memory aof_buf and flushes them to disk based on the appendfsync setting:
appendfsync parameter, three possible values: always – flush to disk after every write event everysec – flush once per second no – let the operating system decide when to write
This gives users control over durability versus latency.
AOF Rewrite
Over time the AOF file grows large, consuming disk space and slowing loading. Redis introduces an AOF rewrite process that compacts the file by creating a new, minimal representation of the current data state. The rewrite runs in a forked child process; meanwhile, new write commands are copied into a separate rewrite buffer. After the child finishes, the buffered commands are appended to the new AOF file, which is then atomically renamed to replace the old one.
Example of command consolidation during rewrite:
RPUSH name_list '编程技术宇宙' RPUSH name_list '帅地玩编程' RPUSH name_list '后端技术学堂' These can be merged into a single command: RPUSH name_list '编程技术宇宙' '帅地玩编程' '后端技术学堂'
Conclusion
With both RDB snapshots and AOF logging, Redis balances fast recovery and minimal data loss. The story ends with MySQL asking whether RDB can be dropped, leaving Redis to reflect on the trade‑offs between the two persistence strategies.
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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
