Redis Persistence Deep Dive: RDB, AOF, and Hybrid Strategies for Interview Success
This article provides a comprehensive analysis of Redis persistence mechanisms—including RDB snapshots, AOF append‑only logs, and the hybrid RDB+AOF mode—covering their architectures, trigger methods, performance trade‑offs, rewrite processes, interview‑focused questions, and practical recommendations for production environments.
Redis Persistence Mechanisms
Redis provides two primary persistence mechanisms and a hybrid mode introduced in Redis 4.0:
RDB (Redis Database) : Periodic snapshot of the entire dataset saved as a binary file dump.rdb.
AOF (Append‑Only File) : Logs every write command to appendonly.aof for incremental recovery.
Hybrid persistence : Combines an RDB preamble with an AOF log, giving fast recovery and high data safety.
RDB Persistence
RDB is the default persistence method. When triggered, Redis forks a child process ( fork()) that uses copy‑on‑write (COW) to share memory pages with the parent. The child writes the in‑memory data to a temporary file and then atomically replaces the old dump.rdb. The main process continues serving client requests, with only a brief pause during the fork.
Trigger Methods
Automatic : Configured in redis.conf via save rules. Example:
save 900 1 # if at least 1 change within 900 s
save 300 10 # if at least 10 changes within 300 s
save 60 10000 # if at least 10000 changes within 60 sManual : SAVE (blocks the main process, not recommended) or BGSAVE (runs in background, recommended).
AOF Persistence
AOF records every write command in appendonly.aof. Commands are first placed in an in‑memory buffer aof_buf and flushed to disk according to the appendfsync policy.
Appendfsync Policies
always: fsync after every command – highest data safety, worst performance. everysec: fsync once per second – recommended balance; at most 1 second of data loss. no: OS decides when to flush – best performance, potential large data loss.
AOF Rewrite
Over time the AOF file grows. Redis rewrites it by reading the current in‑memory state and generating a compact AOF file without reading the old log.
Automatic trigger : When the file size grows auto-aof-rewrite-percentage 100 (100 % increase) and exceeds auto-aof-rewrite-min-size 64mb.
Manual trigger : Execute BGREWRITEAOF.
Hybrid Persistence (Redis 4.0+)
Hybrid persistence embeds an RDB snapshot at the beginning of the AOF file, allowing both mechanisms to work together.
# Enable AOF
appendonly yes
# Enable hybrid persistence
aof-use-rdb-preamble yesAdvantages:
Fast recovery : RDB part loads the full dataset quickly.
High data safety : AOF part records incremental changes.
Smaller file size : Binary RDB preamble is more compact than a pure AOF log.
Common Interview Follow‑up Questions
Why does fork() not block the main process? The fork creates a child using copy‑on‑write, sharing memory pages with the parent. Only when a page is modified does the OS copy it, so the fork itself takes only a few milliseconds. The parent continues handling requests, though a very large memory footprint can cause a slightly longer pause.
Can RDB and AOF be enabled simultaneously? Yes. Before Redis 4.0 they run independently. Since 4.0, hybrid persistence embeds the RDB snapshot in the AOF file, allowing both to operate together.
After a crash, which file does Redis load first? If both dump.rdb and appendonly.aof exist, Redis loads the AOF because it provides higher data integrity. If only the RDB exists, it loads the RDB. If the AOF is corrupted, the redis-check-aof tool can be used for repair.
Java Architect Handbook
Focused on Java interview questions and practical article sharing, covering algorithms, databases, Spring Boot, microservices, high concurrency, JVM, Docker containers, and ELK-related knowledge. Looking forward to progressing together with you.
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.
