AOF Persistence
Append-Only File (AOF) logs every write command — on restart Redis replays the log to rebuild state.
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.
flowchart LRWrites --> MemoryMemory -->|fork| RDBMemory -->|append| AOF
Step-by-step explanation
- Write command succeeds in memory first.
- Command appended to AOF buffer.
- fsync policy flushes buffer to disk.
- Rewrite fork child writes minimal new AOF.
- Startup replays AOF commands in order.
Syntax reference
Common commands
- everysec — industry default.
- Rewrite during low traffic.
- Monitor aof_current_size growth.
CONFIG SET appendonly yesCONFIG GET appendfsyncBGREWRITEAOFINFO persistence
Informative example
Enable AOF on staging to match production durability:
redis-cli CONFIG SET appendonly yesredis-cli CONFIG SET appendfsync everysecredis-cli CONFIG REWRITEredis-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?
Q2Beginnerappendfsync options?
Q3IntermediateAOF rewrite why?
Q4IntermediateRPO with everysec?
Q5AdvancedAOF corrupted on disk?
Summary
AOF logs writes — finer RPO than RDB alone.