Course contentsShow
← System Design
Lesson 351 of 1,91910. NoSQL: Key-Value StoresFree lesson

Redis Persistence: AOF Logs

Append-Only File logging for durability, fsync policies, and log rewriting to manage file size.

Redis Persistence: AOF Logs

What You'll Learn

You'll learn how Redis's Append-Only File (AOF) logging provides an alternative durability strategy by recording every write operation, offering stronger data guarantees than RDB snapshots.

What is AOF?

While RDB snapshots capture periodic point-in-time copies of your data, AOF (Append-Only File) works like a detailed diary. Every write command Redis executes (SET, INCR, LPUSH, etc.) gets appended to a log file on disk. If Redis crashes, it replays this log on restart to reconstruct the exact dataset.

Think of it like recording every move in a chess game—you can replay all moves from the beginning to reach the current board state.

How AOF Works

When a client executes a write command:

  1. Redis processes the command in memory
  2. The command is appended to the AOF buffer
  3. Based on the fsync policy, Redis flushes the buffer to disk

fsync Policies

Redis offers three fsync strategies that balance durability and performance:

  • always: fsync after every write (safest, slowest)
  • everysec: fsync once per second (good balance, at most 1 second data loss)
  • no: Let the OS decide when to flush (fastest, riskiest)

AOF Rewriting

Since AOF logs every command, the file grows continuously. If you SET the same key 1,000 times, all 1,000 commands are logged—but you only need the final value!

AOF rewriting compacts the log by creating a minimal command set that produces the same final state. Redis does this automatically in the background, replacing the old AOF with a much smaller file while continuing to accept writes.

Trade-offs

AOF provides better durability than RDB but costs more disk I/O and storage. Most production systems use both: AOF for durability, RDB for faster restarts.

Key Takeaway: AOF logs provide stronger durability by recording every write command, with configurable fsync policies and automatic log rewriting to manage file growth.