Skip to content

Redis persistence — RDB and AOF, configured for the workload

Understand RDB snapshots vs AOF append-only files, the durability trade-offs, the fsync policies, and the production combination that gets you both fast restore and minimal data loss.

Redis offers two persistence mechanisms — RDB (point-in-time snapshots) and AOF (append-only command log) — that solve different problems. The recommended production configuration is both, with the AOF tuned to balance durability against latency. This article explains what each one does, the failure semantics, the fsync trade-offs that determine how much data you can lose, and the operating practice that makes recovery reliable.

How to verify

# Current persistence config
redis-cli CONFIG GET save
redis-cli CONFIG GET appendonly
redis-cli CONFIG GET appendfsync
redis-cli CONFIG GET dir
redis-cli CONFIG GET dbfilename
redis-cli CONFIG GET appendfilename

# Persistence state
redis-cli INFO persistence

# Disk usage
ls -lh /var/lib/redis/

Key INFO fields to read: rdb_last_save_time (Unix timestamp of last successful snapshot), rdb_changes_since_last_save (writes since last snapshot), aof_enabled, aof_last_rewrite_status, aof_current_size.

What’s happening

RDB is a binary snapshot of the entire dataset, written periodically. The save directive in redis.conf defines triggers: save 60 10000 means “snapshot if 10000+ keys changed in the last 60 seconds.” Redis forks a child process to do the snapshot — the parent keeps serving requests while the child writes to disk. The result is a single dump.rdb file; recovery is just loading that file into memory, which is fast (10-30 seconds per GB on SSD).

AOF is an append-only log of every write command. Recovery replays the log to reconstruct state. The AOF can be tuned with appendfsync:

  • always — fsync after every write. Strongest durability (zero data loss). Slowest.
  • everysec — fsync once per second. At most one second of data loss on crash. The recommended default.
  • no — let the OS flush whenever it wants. Most data loss on crash. Fastest.

AOF files grow with every write, so Redis periodically rewrites them — compacts the log into the minimum sequence of commands that produces the same state. Configure with auto-aof-rewrite-percentage and auto-aof-rewrite-min-size.

The recommended production setting is both RDB and AOF enabled, with appendfsync everysec. RDB gives fast startup and easy off-site backup; AOF gives sub-minute RPO on crash. Disk usage is roughly 2x the dataset size during steady state.

The procedure

A working /etc/redis/redis.conf snippet for production:

# --- RDB snapshots ---
save 900 1                # snapshot if 1+ key changed in 15 minutes
save 300 10               # snapshot if 10+ keys changed in 5 minutes
save 60 10000             # snapshot if 10000+ keys changed in 1 minute
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis

# --- AOF ---
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec       # 'always' for stronger durability, 'no' for speed
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100   # rewrite when AOF is 2x last-rewrite size
auto-aof-rewrite-min-size 64mb    # but only if at least 64 MiB
aof-load-truncated yes
aof-use-rdb-preamble yes          # hybrid format: RDB header + AOF tail (fastest recovery)
  1. Reload the daemon to apply persistence changes:

    sudo systemctl restart redis-server
  2. Trigger a manual RDB snapshot to confirm forking and fsync work:

    redis-cli BGSAVE
    redis-cli LASTSAVE   # should be a recent Unix timestamp
    redis-cli INFO persistence | grep rdb
  3. Trigger an AOF rewrite manually to ensure it works:

    redis-cli BGREWRITEAOF
    redis-cli INFO persistence | grep aof_rewrite
  4. Ship RDB and AOF off-host to object storage. Redis writes to /var/lib/redis/; sync to a remote with rclone or restic.

    # Cron a sync after each successful BGSAVE
    rclone copy /var/lib/redis/dump.rdb remote:my-backups/redis/$(hostname)/
  5. Practice the restore drill. On a sandbox host, drop in the latest dump.rdb and appendonly.aof, restart, and verify the data is back.

    sudo systemctl stop redis-server
    sudo rm /var/lib/redis/dump.rdb /var/lib/redis/appendonly.aof
    sudo cp /tmp/restore/dump.rdb /tmp/restore/appendonly.aof /var/lib/redis/
    sudo chown redis:redis /var/lib/redis/dump.rdb /var/lib/redis/appendonly.aof
    sudo systemctl start redis-server
    redis-cli DBSIZE   # should match the source dataset size
  6. Disable AOF only if your workload truly does not need durability — a pure cache where data can be repopulated from upstream on miss. Set appendonly no and rely on RDB alone.

  7. Set appendfsync always only for workloads where every write must survive a crash — financial sequence numbers, token issuance, anything with no replay path. Be aware the latency cost can be 10-100x.

Choosing the right combination

WorkloadRDBAOFappendfsyncWhy
Cacheyes (hourly)non/aSource of truth is elsewhere
Session storeyesyeseverysecLose at most 1 second
Job queueyesyesalwaysEvery job must survive
Pub/Sub brokernonon/aNothing to persist
Primary KV storeyesyeseverysecThe recommended default

Common pitfalls

  • A small or full disk during BGSAVE causes the child to die mid-write; stop-writes-on-bgsave-error yes then halts all writes to prevent silent data loss. Monitor disk and the RDB child exit status.
  • appendfsync always on a busy host can collapse throughput by 100x — most workloads do not need it. Use everysec and the AOF rewrite mechanism.
  • AOF rewrites need RAM equal to the dataset because Redis forks; on a host with high memory pressure the fork itself can fail. Provision RAM or enable vm.overcommit_memory = 1.
  • Restoring without removing the old appendonly.aof first causes Redis to load the OLD AOF on top of the new RDB — corrupting state. Always clean the data directory before restore.
  • Hybrid format (aof-use-rdb-preamble yes) was added in Redis 4.0 and is the modern default — it lets recovery use the RDB header for speed and the AOF tail for the most recent changes.

In the engagements we run, Redis persistence is configured per workload — pure caches use RDB-only for fast restore, primary stores use the RDB+AOF hybrid with everysec, and queues serving financial workloads use always. We monitor rdb_last_save_time and alert if no successful snapshot in the last 6 hours. Operated under Managed Operations — and the HA layer is covered in redis-sentinel-ha.