Databases 11 min read

Understanding Redis Persistence: RDB, AOF, and Hybrid Strategies

Redis offers three persistence mechanisms—RDB snapshots, AOF command logging, and a hybrid approach—each with distinct trigger methods, performance impacts, and trade‑offs in recovery speed, data safety, and file size, helping you choose the optimal strategy for your workload.

Xuanwu Backend Tech Stack
Xuanwu Backend Tech Stack
Xuanwu Backend Tech Stack
Understanding Redis Persistence: RDB, AOF, and Hybrid Strategies

Redis, a high‑performance key‑value store, provides three main persistence mechanisms: RDB (Redis Database Backup), AOF (Append‑Only File), and a hybrid RDB + AOF mode.

RDB Persistence

The RDB mechanism creates a binary snapshot of the in‑memory data and saves it to a file (default dump.rdb). It can be triggered manually with save (blocking) or bgsave (forks a child process), automatically via configuration (e.g., save m n), during master‑slave replication, or on normal shutdown.

Trigger Mechanisms

Manual : save blocks the server until the snapshot is written; bgsave forks a child so the main process continues handling requests.

Automatic : Configured with save m n to run bgsave after m seconds with n changes.

Replication : Master generates an RDB file for a slave during full synchronization.

Shutdown : shutdown triggers an RDB save before exiting.

Advantages

Fast recovery : Loading a binary snapshot is much quicker than replaying commands.

Compact file : Binary format reduces disk usage and speeds up transfer during replication.

Disadvantages

Data loss risk : Changes made after the last snapshot are not persisted.

Performance overhead : Frequent bgsave incurs fork overhead and can increase system load.

AOF Persistence

AOF logs every write command received by the server. On restart, Redis replays the logged commands to reconstruct the dataset.

Working Mechanism

Command appending : Write commands (e.g., SET, DEL) are appended to an in‑memory buffer aof_buf and later flushed to disk.

Sync strategies : always: Every command is flushed to disk immediately (highest safety, lowest performance). everysec: Default; commands are flushed once per second (balanced safety and performance). no: OS decides when to flush (best performance, highest risk).

File rewriting : When the AOF grows large, Redis rewrites it by creating a new compact file that contains only the minimal set of commands needed to rebuild the current state.

Advantages

High data safety : Near‑real‑time persistence when using always or everysec.

Readability : Text format makes the log easy to inspect for debugging.

Disadvantages

Slow recovery : Replaying all commands can take considerable time.

Large file size : Continuous logging increases disk usage.

Hybrid Persistence

The hybrid mode combines RDB and AOF: Redis first writes a binary RDB snapshot into the AOF file, then appends subsequent write commands in the usual AOF format.

Advantages

Fast recovery : The RDB part loads most data quickly, while the AOF part replays recent changes.

High data integrity : AOF logs ensure that updates after the snapshot are not lost.

RedisPersistenceAOFRDBHybrid
Xuanwu Backend Tech Stack
Written by

Xuanwu Backend Tech Stack

Primarily covers fundamental Java concepts, mainstream frameworks, deep dives into underlying principles, and JVM internals.

0 followers
Reader feedback

How this landed with the community

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.