Databases 13 min read

Understanding Redis Persistence: AOF and RDB Mechanisms

This article explains how Redis ensures data durability by using two persistence strategies—Append Only File (AOF) and Redis Database (RDB) snapshots—detailing their operation, configuration options, trade‑offs, rewrite processes, and how they handle expired keys and recovery.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Understanding Redis Persistence: AOF and RDB Mechanisms

Introduction

Redis stores data in memory, so a power loss can cause data loss; persistence mechanisms (RDB and AOF) are introduced to prevent this.

AOF Persistence

What is AOF?

Append Only File (AOF) records every write command executed by the server, preserving the database state by replaying these commands.

Why Log After Execution?

Logging after a command succeeds avoids recording failed commands and prevents blocking the current write operation.

Potential Risks

If the server crashes after a command succeeds but before the log is written, data can be lost.

Since AOF writes are performed by the main thread, heavy disk pressure can slow down subsequent operations.

AOF Write and Sync Strategies

The appendfsync setting controls how often data is flushed to disk:

Always : sync after every write.

Everysec : sync at most once per second.

No : let the operating system decide.

AOF Rewrite Mechanism

Because the AOF file grows with each command, Redis periodically rewrites it by creating a new file that contains only the minimal set of commands needed to reconstruct the current dataset.

192.168.56.118:6379> set name "xiaoming"
OK
192.168.56.118:6379> get name
"xiaoming"
192.168.56.118:6379> set name "xiaozhang"
OK
192.168.56.118:6379> set name "xiaoli"
OK
# After rewrite
192.168.56.118:6379> set name "xiaoli"

The rewrite runs in a child process, so the main thread continues handling client commands without blocking.

AOF Data Restoration

During startup, Redis reads the AOF file and re‑executes every command, fully restoring the database to its pre‑shutdown state.

RDB Persistence

What is RDB?

RDB creates point‑in‑time snapshots of the in‑memory dataset and writes them to a binary file on disk.

Snapshot Creation Methods

save : performed in the main thread (blocks the server).

bgsave : forks a child process to write the snapshot without blocking.

Configuration Example

save 900 1
save 300 10
save 60 10000

These directives trigger a snapshot when the specified number of seconds have passed *and* the given number of write operations have occurred.

Copy‑On‑Write (COW)

During a bgsave , the child process shares the parent’s memory; if the parent modifies data, the changed pages are copied, allowing the snapshot to be written while writes continue.

Snapshot Frequency Considerations

Too frequent snapshots increase disk I/O and may cause contention; too infrequent snapshots increase potential data loss on failure.

Hybrid Persistence (Redis 4.0)

Redis 4.0 combines AOF (for incremental changes) with periodic RDB snapshots, offering fast recovery and minimal data loss.

Expiration Handling

Expired keys are omitted from RDB files; during AOF rewrite, expired keys are also excluded. In replication, the master sends DEL commands for expired keys, while slaves apply them only after receiving the command.

Summary

AOF : configurable durability (Always/Everysec/No), larger file size, slower recovery, but can achieve near‑zero data loss with the Everysec setting.

RDB : fast recovery, smaller files, but data loss depends on snapshot interval; suitable when minute‑level data loss is acceptable.

Using the hybrid approach (AOF + RDB) provides a balanced solution for most production environments.

databaseRedisPersistenceData RecoveryAOFRDB
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

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.