You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
By default, Redis stores all data in memory. When the server stops, the data is gone. Redis offers two persistence mechanisms — RDB (Redis Database) snapshots and AOF (Append-Only File) — so that data survives restarts. You can use either or both.
RDB persistence saves the entire in-memory dataset to a binary snapshot file (usually named dump.rdb) at configured intervals.
Configure snapshot rules in redis.conf:
# Save after 900 seconds if at least 1 key changed
save 900 1
# Save after 300 seconds if at least 10 keys changed
save 300 10
# Save after 60 seconds if at least 10000 keys changed
save 60 10000
Trigger a manual snapshot:
# Blocking save — blocks the server until complete
127.0.0.1:6379> SAVE
# Non-blocking save — forks a child process to save in background
127.0.0.1:6379> BGSAVE
Advantages of RDB: Compact single-file backups, fast restart (loading binary snapshot is fast), minimal performance impact (background fork).
Disadvantage: You can lose data written since the last snapshot (up to several minutes of writes).
AOF logs every write command to a file. On restart, Redis replays the AOF to rebuild the dataset.
Enable AOF in redis.conf:
appendonly yes
appendfilename "appendonly.aof"
Control how often AOF is fsynced to disk:
# always — safest, slowest (fsync every write)
appendfsync always
# everysec — default, good balance (fsync every second)
appendfsync everysec
# no — fastest, least safe (OS decides when to flush)
appendfsync no
Advantages of AOF: Far less data loss risk (everysec loses at most 1 second of writes), human-readable command log.
Disadvantage: AOF files are larger than RDB files; replay can be slower on large datasets.
Over time, the AOF grows. Redis can compact it by rewriting only the minimal set of commands needed to reconstruct the current state:
127.0.0.1:6379> BGREWRITEAOF
You can enable both mechanisms simultaneously. Redis recommends this for the best durability: RDB for fast backups, AOF for minimal data loss.
For a pure cache where data loss is acceptable, disable all persistence:
# In redis.conf
save ""
appendonly no
Choosing the right persistence strategy depends on your tolerance for data loss versus your performance and storage requirements.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.