Databases 20 min read

Redis Persistence Mechanisms: AOF, RDB, and Hybrid Persistence

Redis offers three persistence options—AOF, which logs every write command; RDB, which creates periodic snapshots; and hybrid persistence, which combines both—to balance data safety, recovery speed, file size, and performance, with configurable settings for sync policies, rewrite processes, and compression.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Redis Persistence Mechanisms: AOF, RDB, and Hybrid Persistence

This article introduces the three major persistence mechanisms provided by Redis: Append Only File (AOF) logs, RDB snapshots, and a hybrid persistence approach that combines AOF and RDB.

Why Redis Needs Persistence

Redis stores all data in memory to achieve high performance, but this makes data vulnerable to loss if the server crashes. To avoid data loss, Redis can persist in‑memory data to disk so that it can be reloaded on restart.

1. AOF

1.1 Overview

AOF (Append Only File) records every write command in real time. When Redis restarts, it replays the commands sequentially to restore data. Unlike write‑ahead logging (WAL) used by MySQL, AOF uses a write‑behind approach: 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 parameters in redis.conf or by using the CONFIG SET command at runtime.

1.3 Configuration Items

Use appendfilename and appenddirname to specify the AOF file name and directory.

appendfsync controls the write‑back policy (Always, Everysec, No).

no-appendfsync-on-rewrite determines whether AOF is flushed during rewrite or RDB saving.

auto-aof-rewrite-percentage and auto-aof-rewrite-min-size trigger automatic AOF rewrite.

aof-load-truncated decides whether Redis can start when the AOF file ends with an incomplete command.

aof-use-rdb-preamble enables hybrid persistence.

1.4 AOF File Content

Each command is stored in the Redis protocol format. For example, the command SET testkey testvalue is stored as *3 $3 SET $7 testkey $9 testvalue , where the numbers indicate the byte length of each part.

1.5 Write‑Back Strategies

Always : Synchronous write after each command (high reliability, high performance cost).

Everysec : Write to the OS buffer and flush to disk every second (balanced performance, may lose up to 1 second of data).

No : Let the OS decide when to flush (best performance, lowest reliability).

1.6 AOF Rewrite

Because AOF grows continuously, Redis can rewrite the AOF file by creating a new file that contains only the minimal set of commands needed to reconstruct the current dataset. The rewrite is performed by a child process BGREWRITEAOF so the main thread remains responsive.

1.7 Detailed AOF Persistence Process

Redis receives a write command, executes it, and writes the command to the AOF buffer.

When the configured write‑back condition is met, the buffer is flushed to the AOF file.

If the AOF file size reaches the rewrite threshold, the main process forks a bgrewriteaof child.

During rewrite, new write commands are recorded both in the AOF buffer and a rewrite buffer.

After the child finishes, the rewrite buffer is appended to the temporary AOF file, which then replaces the old file.

1.8 AOF Pros and Cons

Pros: higher data safety, ability to recover from accidental deletions, and configurable write‑back policies. Cons: larger file size compared to RDB and reduced write QPS.

2. RDB

2.1 Overview

RDB creates point‑in‑time snapshots of the dataset at configured intervals. The snapshot file (RDB file) can be loaded quickly on restart.

2.2 Trigger Methods

SAVE : Synchronous, blocks the server during snapshot creation.

BGSAVE : Asynchronous, forks a child process to write the snapshot while the server continues to serve clients (fork phase may cause a short pause).

Automatic triggers defined by the save directive in redis.conf (e.g., “save 900 1”).

2.3 Configuration Items

stop-writes-on-bgsave-error to stop writes if a snapshot fails.

rdbcompression to enable LZF compression.

rdbchecksum to enable/disable CRC64 checksum.

dbfilename and dir to set file name and directory.

2.4 Copy‑On‑Write (COW)

During BGSAVE , the child process shares the parent’s memory pages. When the parent modifies a page, the OS copies it (COW), allowing the child to write a consistent snapshot without blocking writes.

2.5 RDB File Format

Header “REDIS” (5 bytes).

Version number (4 bytes).

One or more databases, each starting with a SODB byte, followed by db_number and key‑value pairs.

EOF marker (1 byte).

Checksum.

2.6 RDB Pros and Cons

Pros: fast recovery, low memory overhead, minimal impact on performance (fork only). Cons: possible data loss for writes after the last snapshot and potential pause during large forks.

3. Hybrid Persistence

Hybrid persistence combines AOF and RDB: AOF records incremental changes between snapshots, while periodic RDB snapshots provide a compact base. The feature is enabled via aof-use-rdb-preamble in redis.conf .

4. Choosing a Persistence Strategy

If data safety is critical, choose AOF or hybrid persistence.

If fast recovery is needed, choose RDB or hybrid persistence.

If file size is a concern, choose RDB with compression.

If performance is paramount, choose RDB or hybrid persistence.

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

DatabaseRedisPersistenceAOFRDBHybrid Persistence
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

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.