RDB Snapshots and AOF
RDB — Periodic Snapshots
Section titled “RDB — Periodic Snapshots”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.
How snapshots are triggered
Section titled “How snapshots are triggered”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 1save 300 100save 60 10000dbfilename dump.rdbdir /var/lib/redisThe 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 trade-offs
Section titled “RDB trade-offs”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 — Append-Only File
Section titled “AOF — Append-Only File”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 yesappendfilename "appendonly.aof"appendfsync everysecThe 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.
Checking Persistence State with redis-cli
Section titled “Checking Persistence State with redis-cli”127.0.0.1:6379> BGSAVEBackground saving started127.0.0.1:6379> LASTSAVE(integer) 1700000000127.0.0.1:6379> CONFIG GET save1) "save"2) "3600 1 300 100 60 10000"127.0.0.1:6379> BGREWRITEAOFBackground append only file rewriting startedLASTSAVE 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
BGREWRITEAOFHybrid Persistence
Section titled “Hybrid Persistence”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 yesHybrid 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.