Skip to content

Redis persistence — RDB vs AOF tradeoffs

Choose the right Redis persistence setup by weighing how much data you can lose in a crash against how fast Redis must restart. For a primary data store the answer is usually to run RDB snapshots and the AOF log together, and the article shows how to configure, test and back up that setup.

Turn on both Redis persistence modes, RDB snapshots and the AOF log, whenever Redis holds data you cannot rebuild. Persistence is how Redis saves its in-memory data to disk so it survives a restart or a crash. An RDB snapshot is a compact copy of the whole dataset written every few minutes, so it loads fast but loses whatever changed since the last one. The AOF (append-only file) records every write as it happens, so a crash loses at most about one second of writes with the default setting, but replaying it on restart is slower. Running both gives you the AOF’s durability with a fast restart, and if Redis is only a cache you can refill, snapshots alone are enough.

How to verify

# Current persistence settings
redis-cli CONFIG GET save
redis-cli CONFIG GET appendonly
redis-cli CONFIG GET appendfsync
redis-cli CONFIG GET dir

# Last successful persistence
redis-cli INFO persistence | grep -E 'rdb_last|aof|aof_last|aof_current_size|loading'

# Files on disk
sudo ls -lh /var/lib/redis/

What’s happening

RDB is a binary dump of the entire dataset taken at points in time. The default save policy is “if at least N keys changed in the last M seconds, write a snapshot”. Snapshots are produced via BGSAVE, which forks the Redis process so the parent serves clients while the child writes the dump to disk. The resulting file is compact (around the size of the dataset), loads quickly on restart, and is easy to ship as a backup. The catch: between snapshots, anything written is lost on a crash.

AOF is an append-only log of every write command. With appendfsync everysec (the default), the OS flushes the log to disk once per second — meaning at most one second of writes is lost on a crash. With appendfsync always, every write is flushed before acknowledgment — full durability at the cost of latency. With appendfsync no, the OS decides — fastest but loses data on crash.

The AOF file grows continuously, so Redis periodically rewrites it: scan the live dataset, write the smallest set of commands that reproduces it, replace the old file. This is BGREWRITEAOF, which (like BGSAVE) forks. After rewrite, the AOF is compact again.

The combined mode that most production Redis uses: appendonly yes plus the default save snapshot triggers. With aof-use-rdb-preamble yes (default on modern Redis), the rewritten AOF file starts with an RDB snapshot section then appends incremental commands. Loading on restart reads the RDB section (fast) and replays the trailing commands (durability up to last fsync). Best of both.

The decision

ConcernRDBAOFCombined
Max data loss on crashUp to save interval (minutes)1 second (everysec)1 second
Recovery timeFastest (binary load)Slower (replay commands)Fast (RDB preamble + tail)
File sizeCompactLarger (grows then rewrites)Compact after rewrite
Write amplificationNone during snapshotsEvery write goes to diskEvery write goes to disk
Fork costYes during BGSAVEYes during BGREWRITEAOFYes for both
Backup easeExcellent (single file)Less convenientExcellent (RDB preamble)

Use RDB only when:

  • The data is reconstructable (cache) and minutes of loss are acceptable.
  • You need the smallest fork-time memory pressure (only one fork per snapshot).

Use AOF only when:

  • You want sub-second durability and explicitly do not want snapshot files.
  • The dataset is small enough that AOF replay on restart is fast.

Use both (recommended for primary stores) when:

  • Sub-second durability matters AND fast restart matters.
  • You want backups that are easy to ship and restore.

The procedure

  1. Enable AOF and snapshots in /etc/redis/redis.conf:

    # AOF
    appendonly yes
    appendfilename "appendonly.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-use-rdb-preamble yes
    
    # RDB
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump.rdb
  2. Provision a dedicated disk for dir. Persistence on the same disk as the OS or as application logs creates fsync contention:

    sudo mkfs.xfs /dev/vdb
    sudo mkdir -p /var/lib/redis
    sudo mount /dev/vdb /var/lib/redis
    sudo chown redis:redis /var/lib/redis
    echo "/dev/vdb /var/lib/redis xfs defaults 0 0" | sudo tee -a /etc/fstab
  3. Restart and verify:

    sudo systemctl restart redis-server
    redis-cli INFO persistence
    ls -lh /var/lib/redis/
  4. Test a restart recovery by writing data, then restarting:

    redis-cli SET test1 "value1"
    redis-cli BGSAVE
    redis-cli LASTSAVE
    sudo systemctl restart redis-server
    redis-cli GET test1
    # Expected: "value1"
  5. Trigger a manual AOF rewrite during off-peak to keep the file compact:

    redis-cli BGREWRITEAOF
  6. Ship snapshots off-host. A copy of dump.rdb to S3 every hour is cheap insurance:

    cp /var/lib/redis/dump.rdb /backup/redis-$(date +%FT%H).rdb
    aws s3 cp /backup/redis-$(date +%FT%H).rdb s3://backups/redis/
  7. Restore. Stop the daemon, copy the desired RDB or AOF into dir, start the daemon. The file Redis loads first is the AOF if appendonly yes; otherwise the RDB.

Common pitfalls

  • appendfsync always looks safer but adds millisecond fsync latency to every write — on a busy master this kills throughput. everysec is the right choice for almost every workload.
  • stop-writes-on-bgsave-error yes (default) is correct — it pauses writes if BGSAVE fails, alerting you to disk issues before data is silently lost. Do not disable.
  • AOF replay on restart is single-threaded and proportional to AOF size. A 20 GB AOF takes minutes to load — for fast recovery, rewrite often (lower auto-aof-rewrite-percentage).
  • A fork during peak load can take seconds on a CPU-bound host, during which Redis appears to hang. Provision CPU headroom or schedule heavy persistence operations.
  • Persistence to the same disk as the OS competes for IOPS with logging and syslogd. Dedicate a disk.

In the engagements we run, every primary-store Redis comes up with appendonly yes, aof-use-rdb-preamble yes, a dedicated data volume, and dump.rdb shipped to S3 hourly. We operate that under Managed Operations — and for cache-only deployments where RDB-only suffices, the lighter config is documented in the same runbook.