Databases 20 min read

Redis Persistence Explained: RDB vs AOF and Hybrid Strategies

This article explores Redis persistence mechanisms, detailing the purpose of data durability, the two primary methods—RDB snapshots and AOF logs—their operation, strategies, advantages, drawbacks, and the newer hybrid persistence mode, while also addressing common questions and best‑practice recommendations for reliable deployment.

Sanyou's Java Diary
Sanyou's Java Diary
Sanyou's Java Diary
Redis Persistence Explained: RDB vs AOF and Hybrid Strategies

Prerequisite Concepts

Purpose of Persistence

What is persistence? It is the act of saving in‑memory data to disk so that it survives process crashes.

Redis stores all data in memory; if the instance crashes, the memory is released and all data is lost.

Persistence writes the data to disk to prevent loss.

Redis Persistence Methods

Redis provides two main persistence mechanisms:

Data snapshot (RDB) – similar to a MySQL dump; creates a point‑in‑time binary snapshot of the entire dataset.

Append‑only file (AOF) – logs every write command; on recovery the log is replayed to reconstruct the dataset.

RDB Persistence Strategy

What is RDB?

RDB is a binary snapshot file stored on disk. It contains a compact representation of the dataset at a specific moment.

Two RDB Strategies

save – synchronous snapshot; blocks the client while the RDB file is written. Time complexity O(n).

bgsave – asynchronous snapshot; Redis forks a child process (using copy‑on‑write) to generate the RDB file without blocking the main thread. Time complexity O(n) as well.

How to Trigger RDB Snapshots

RDB can be triggered manually with save or bgsave , or automatically via the save configuration directive, e.g.:

save 900 1 – snapshot if at least one key changes within 900 seconds.

save 300 10 – snapshot if at least ten keys change within 300 seconds.

save 60 10000 – snapshot if at least ten thousand keys change within 60 seconds.

Configuration commands to locate the RDB file:

config get dir – directory where the file is stored.

config get dbfilename – name of the RDB file.

When Does Redis Trigger an RDB Snapshot?

During full synchronization of a replica.

When the FLUSHALL command is executed.

When the server is shut down.

RDB Working Principle

Redis iterates over all in‑memory data, serializes it, and writes the binary snapshot to disk.

Because Redis is single‑threaded, the snapshot generation would block client requests. To avoid this, Redis forks a child process; the child writes the RDB file while the parent continues serving clients, using the operating‑system copy‑on‑write (COW) mechanism.

AOF Persistence Strategy

What is AOF?

AOF is a log file that records every write command executed by Redis. The log can be replayed to rebuild the dataset. Only commands that modify the dataset are logged, and they are recorded after the command succeeds.

Three AOF Write Strategies

always – every command is flushed to disk immediately (real‑time sync).

everysec – the buffer is flushed to disk once per second (default, balances durability and performance).

no – the operating system decides when to flush the buffer (least safe).

Pros and Cons of Each Strategy

always : guarantees no data loss but incurs heavy synchronous I/O, reducing write performance.

everysec : offers a good trade‑off; at most one second of data may be lost.

no : may lose an indeterminate amount of data; not recommended for production.

AOF Rewrite

When the AOF file grows large, Redis rewrites it by creating a new file that contains a minimal set of commands representing the current dataset, thereby shrinking the log.

Choosing Between RDB and AOF

RDB Advantages

Compact snapshot files; smaller disk usage.

Fast recovery.

RDB Disadvantages

Snapshot generation can be resource‑intensive and may take several minutes.

Data modified during snapshot creation is not captured, potentially losing minutes of updates.

Binary format is not human‑readable.

AOF Advantages

Provides finer‑grained durability; can be configured for near‑real‑time persistence.

Log file is plain text and easy to inspect.

AOF Disadvantages

Log files are larger than RDB snapshots.

Recovery can be slower because the entire log must be replayed.

Decision Guidance

Use RDB when occasional data loss (up to a few minutes) is acceptable and fast backups are needed.

Use AOF when data integrity is critical and minimal loss is required.

Redis 4.0 Hybrid Persistence

Redis 4.0 introduces a hybrid mode that stores an RDB snapshot followed by incremental AOF entries in a single .aof file. This combines the fast recovery of RDB with the durability of AOF.

Hybrid Advantages

Fast restart: load the RDB part, then replay the small AOF tail.

Provides both compact storage and near‑real‑time durability.

Hybrid Disadvantages

Only supported from Redis 4.0 onward; older versions cannot read the file.

The mixed format reduces readability of the file.

Operational Best Practices

Do not rely solely on RDB snapshots; combine with AOF or hybrid mode for production.

Avoid the always AOF strategy and the no strategy; prefer everysec for a balance of safety and performance.

In clustered deployments, enable persistence on replica nodes rather than on the primary to reduce write load.

Reserve sufficient memory on the host for the forked child process during RDB snapshots.

Additional Questions

Does the child process copy the parent’s memory during RDB, increasing memory usage?

Redis uses copy‑on‑write: the child shares pages with the parent until a write occurs, at which point only the modified pages are duplicated. Memory growth is limited to the amount of data changed during the snapshot; only if the entire dataset is modified will memory roughly double.

Why does AOF log commands only after they succeed?

Logging only successful commands prevents erroneous or failed operations from polluting the log, keeping the AOF concise and reliable.

What if the AOF file becomes too large?

Redis performs an AOF rewrite: it creates a new compact log that contains a single command for each key’s current state, dramatically reducing file size.

DatabaseRedisPersistenceAOFRDBHybrid
Sanyou's Java Diary
Written by

Sanyou's Java Diary

Passionate about technology, though not great at solving problems; eager to share, never tire of learning!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.