Skip to content

RDB Snapshots and AOF

RDB (Redis Database) persistence saves a point-in-time snapshot of the entire dataset to a compact binary file. On startup, Redis loads this file to restore state — making RDB the fastest way to get back up after a restart.

Manual commands:

  • SAVE — blocks the server while writing the snapshot. Avoid in production.
  • BGSAVE — forks a child process to write the snapshot in the background. The parent keeps serving requests.

Automatic thresholds are configured in redis.conf using the save directive. The format is save <seconds> <changed-keys> — Redis takes a snapshot if at least <changed-keys> keys were modified within the last <seconds> seconds.

save 3600 1
save 300 100
save 60 10000
dbfilename dump.rdb
dir /var/lib/redis

The three save lines above mean: snapshot every hour if at least 1 key changed, every 5 minutes if at least 100 changed, or every minute if at least 10 000 changed. To disable automatic snapshots entirely, remove all save lines or set save "".

RDB files are compact and fast to load. The downside is potential data loss: any writes made after the last snapshot are lost if Redis crashes before the next one.

AOF logs every write command that modifies the dataset, appending it to a file in the same text format Redis uses on the wire. On restart, Redis replays the file from top to bottom to reconstruct the dataset.

Enable AOF in redis.conf:

appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec

The appendfsync option controls how often Redis calls fsync() to flush the OS buffer to disk:

  • always — flush after every command. Maximum durability, lowest throughput.
  • everysec — flush once per second. Lose at most one second of writes. Recommended for most workloads.
  • no — let the OS decide when to flush. Fastest, least durable.

Over time the AOF file grows. Redis compacts it with BGREWRITEAOF, which forks a child to rewrite the log using the minimal set of commands needed to recreate the current dataset.

127.0.0.1:6379> BGSAVE
Background saving started
127.0.0.1:6379> LASTSAVE
(integer) 1700000000
127.0.0.1:6379> CONFIG GET save
1) "save"
2) "3600 1 300 100 60 10000"
127.0.0.1:6379> BGREWRITEAOF
Background append only file rewriting started

LASTSAVE returns a Unix timestamp of the last successful BGSAVE. Use it to confirm that automatic snapshots are running on schedule.

BGSAVE
LASTSAVE
CONFIG GET save
BGREWRITEAOF

Redis 4 introduced hybrid mode and Redis 7 refined it. In hybrid mode the AOF file starts with an embedded RDB snapshot (for fast loading) followed by the incremental AOF command log (for durability). You get the fast restart of RDB and the low data-loss of AOF in a single file.

Enable hybrid mode in redis.conf:

aof-use-rdb-preamble yes

Hybrid mode is the default in Redis 7 when both RDB and AOF are enabled. It is the recommended persistence setup for most production workloads that need both speed and durability.

Which command triggers a background RDB snapshot without blocking the server?
What does setting `appendonly yes` in redis.conf enable?
What is the default filename Redis uses for RDB snapshots?
Which `appendfsync` value flushes the AOF buffer to disk exactly once per second?