4 · Persistence in production
In one sentence: with the AOF on, the AOF is what Redis loads, so you switch to it on the live server; a restart takes as long as reading the files; the machine must let Redis fork; and a damaged log is checked, backed up, then fixed.
The problem
Lessons 2 and 3 measured what each mechanism loses. Running them for real raises other questions: which file
does Redis load when both are on? How long is a restart’s outage? What does fork() need from the machine? What if
the log is damaged? Which setting should you choose?
Both on: the AOF wins
With save rules and appendonly yes, Redis loads only the AOF on start, “since it is guaranteed to be the
most complete” (persistence docs). Snapshots still make dump.rdb, handy for backups.
That rule has a trap. A Redis with snapshots only, holding 1000 keys, restarted with appendonly yes added to its
config:
$ docker exec demo redis-cli DBSIZE
0
$ docker exec demo ls -l /data/dump.rdb
-rw------- 1 redis redis 22095 Nov 26 11:10 /data/dump.rdb
There was no AOF, so Redis created an empty one and loaded it. The old dump.rdb sat there, ignored, until one
new write made the next snapshot replace it: 22,095 bytes became 109. All 1000 keys gone.
The safe way, on the running server:
docker exec demo redis-cli CONFIG SET appendonly yes
docker exec demo redis-cli INFO persistence | grep -E 'aof_rewrite_in_progress|aof_rewrite_scheduled|aof_last_bgrewrite_status'
CONFIG SET makes Redis rewrite its memory into a new AOF. When both counters are 0 and the status is ok, put
appendonly yes in redis.conf too (or the next restart goes back to the old config), then restart: 1000 keys.
How long a restart takes
Until it has loaded everything, Redis answers every command with LOADING Redis is loading the dataset in memory: for your app, a restart is that long an outage. The same 3,160,831 keys (506 MB in memory, written by
5,000,000 SETs of 100-byte values on random keys), loaded four ways, two restarts each:
| What Redis loads | Size on disk | Loaded in |
|---|---|---|
dump.rdb (AOF off) | 379 MB | 1.49 s, 1.53 s |
| AOF after a rewrite: base in RDB format (the default) | 379 MB | 1.54 s, 1.37 s |
AOF after a rewrite with aof-use-rdb-preamble no: base as commands | 455 MB | 2.26 s, 2.15 s |
AOF before any rewrite: the 5,000,000 SETs | 720 MB | 4.16 s, 3.85 s |
An RDB file loads in about the same time, whether it’s dump.rdb or the AOF’s base. Commands are slower, because each is run again:
3,160,831 (one per key) took about 2.2 s, 5,000,000 for the same keys about 4 s. So the rewrite (lesson 3) also
decides restart time: it turns the commands into an RDB base.
These are small strings on a laptop: measure yours (Practical 1 does).
What the machine needs for fork
Snapshots and rewrites both fork (lesson 2), and the Redis documentation asks for two Linux settings on the host:
vm.overcommit_memory = 1. The child could in theory need a copy of every page, so with the default (0) Linux refuses the fork “unless there is as much free RAM as required to really duplicate all the parent memory pages” (FAQ), and the failed save becomesMISCONF(lesson 2). Set it withsysctl vm.overcommit_memory=1, and in/etc/sysctl.conf.- Transparent huge pages (THP) set to
never(ormadvise, which Redis’s own check accepts). With huge pages, a few writes after a fork can copy “almost the whole process memory”: “big latency and big memory usage” (latency docs).
Redis checks both for you:
docker run --rm --entrypoint redis-server redis:8.10.3 --check-system
[overcommit]...OK
[THP]...WARNING:
You have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never').
That’s Docker Desktop’s virtual machine. Redis 8.10.3 turns THP off for its own process anyway (disable-thp yes;
/proc/1/status shows THP_enabled: 0), but on your servers, run the check and fix what it reports.
(admin docs,
FAQ,
latency docs)
A damaged log
The end cut short (a crash in the middle of a write(), a full disk): Redis drops the half command and starts.
After 1000 INCRs with the last 3 bytes cut off:
# !!! Warning: short read while loading the AOF file appendonly.aof.1.incr.aof!!!
# !!! Truncating the AOF appendonly.aof.1.incr.aof at offset 25997 !!!
# AOF appendonlydir/appendonly.aof.1.incr.aof loaded anyway because aof-load-truncated is enabled
GET writes said 999. That’s aof-load-truncated yes, the default.
Damage in the middle (here, 2 bytes overwritten at offset 10007): Redis refuses to start, and the container exits:
# Bad file format reading the append only file appendonlydir/appendonly.aof.1.incr.aof at offset 10007. make a backup of your AOF file, then use ./redis-check-aof --fix <filename.manifest>. Alternatively you can set the 'aof-load-corrupt-tail-max-size' configuration option to 16016 and restart the server.
redis-check-aof (in the image) finds the same place: ok_up_to=10007, diff=16016. With --fix it cuts the file
there, and everything after the damage goes with it: GET writes said 384, not 1000. That’s why the message
says back up first: once cut, the 616 commands after the damage can’t be recovered, even by hand.
Backups
dump.rdbcan be copied at any time: Redis writes a temp file and renames it (lesson 2), so the file you copy is always complete.- The AOF is several files: copy the whole
appendonlydir, but not during a rewrite. The docs’ recipe:CONFIG SET auto-aof-rewrite-percentage 0, wait foraof_rewrite_in_progress:0, copy, then set it back.
Keeping save rules next to the AOF gives you the simple one. Redis 8.10 also adds a BACKUP command family
(persistence docs, “Backing up Redis data”).
Choosing
| You need | Setting | You pay |
|---|---|---|
| a cache you can rebuild | save "", appendonly no | an empty Redis after any restart |
| to lose at most minutes | save rules only | everything since the last finished snapshot |
| to lose nothing when Redis crashes, and about 1 s in a power cut | appendonly yes, appendfsync everysec, plus save rules for backups | a log to rewrite; almost no speed (lesson 3) |
| to lose nothing acknowledged, even in a power cut | appendfsync always | 7 to 8 times fewer writes a second per client (lesson 3) |
Try it
Practical 1 · Prove your setting: write a Go program that crashes Redis under load, run it against three settings on a dataset you size, and choose one with your own numbers.
Go deeper: what restarts and loads cost in a container platform
Orchestrators stop Redis with SIGTERM, then SIGKILL after a grace period: give it room for the final save (lesson 1’s Go deeper).
A readiness check that sends PING sees LOADING until the dataset is in memory, so traffic only comes back once
Redis can serve it. A liveness check that also fails on LOADING can kill Redis mid-load, again and again, when the
dataset takes longer to load than the check allows.
(SHUTDOWN)
Common misunderstandings
- “With both on, Redis loads whichever is newer.” It loads the AOF, always.
- “
redis-check-aof --fixrepairs the file.” It cuts it at the first damage.