Redis Tutorial 0/42 lessons ~6 min read Lesson 25

    AOF Persistence

    Append-Only File (AOF) logs every write command — on restart Redis replays the log to rebuild state.

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Append-Only File (AOF) logs every write command — on restart Redis replays the log to rebuild state. appendfsync everysec (default) balances durability (~1s loss window) with throughput; always fsyncs every write but kills perf.

    BGREWRITEAOF compacts AOF by rewriting current memory state — without rewrite the file grows forever. Hybrid RDB+AOF preamble in Redis 7 speeds recovery.

    Choose AOF when losing last minute of RDB snapshot is unacceptable.

    Understanding the topic

    Key concepts

    • appendonly yes — enable AOF.
    • appendfsync always | everysec | no.
    • AOF rewrite — BGREWRITEAOF background compact.
    • auto-aof-rewrite-percentage triggers rewrite.
    • redis-check-aof validates file before start.
    • Multi-part AOF (Redis 7) for faster recovery.
    text
    flowchart LR
    Writes --> Memory
    Memory -->|fork| RDB
    Memory -->|append| AOF

    Step-by-step explanation

    1. Write command succeeds in memory first.
    2. Command appended to AOF buffer.
    3. fsync policy flushes buffer to disk.
    4. Rewrite fork child writes minimal new AOF.
    5. Startup replays AOF commands in order.

    Syntax reference

    Common commands

    • everysec — industry default.
    • Rewrite during low traffic.
    • Monitor aof_current_size growth.
    bash
    CONFIG SET appendonly yes
    CONFIG GET appendfsync
    BGREWRITEAOF
    INFO persistence

    Informative example

    Enable AOF on staging to match production durability:

    bash
    redis-cli CONFIG SET appendonly yes
    redis-cli CONFIG SET appendfsync everysec
    redis-cli CONFIG REWRITE
    redis-cli INFO persistence | grep aof

    CONFIG REWRITE persists to redis.conf if writable. Managed Redis sets this in console — don't fight provider defaults.

    Real-world use

    Real-world use cases

    • Session store needing <1 min RPO.
    • Primary ephemeral DB with replay recovery.
    • Audit-sensitive write log (supplement not replace audit DB).
    • Pair RDB backup + AOF live durability.
    • Regulatory require durable write trail.

    Best practices

    • appendfsync everysec default for most apps.
    • Monitor rewrite duration and failures.
    • Disk SSD for AOF — HDD struggles on always.
    • auto-aof-rewrite-min-size avoid tiny rewrites.
    • Test full AOF reload in staging.
    • Hybrid snapshot preamble when available.

    Common mistakes

    • appendfsync no — OS cache only, big loss on crash.
    • Never rewriting — disk fills.
    • AOF on same disk as logs without IOPS headroom.
    • Assuming AOF replaces off-site RDB backups.

    Advanced interview questions

    Q1BeginnerAOF purpose?
    Log every write for rebuild on restart.
    Q2Beginnerappendfsync options?
    always safest slowest; everysec default; no fastest risky.
    Q3IntermediateAOF rewrite why?
    Compact log to minimal command set representing current data.
    Q4IntermediateRPO with everysec?
    Up to ~1 second of writes lost on catastrophic failure.
    Q5AdvancedAOF corrupted on disk?
    redis-check-aof --fix truncates tail; restore from replica or RDB backup.

    Summary

    AOF logs writes — finer RPO than RDB alone.

    Ready to mark this lesson complete?Track your journey across the entire course.