RDB, AOF and losing data
Redis keeps every key in memory, so a restart starts from whatever it wrote to disk. It has two ways to write it: a snapshot now and then, or a log of every write. Here is how each one works and exactly how many writes you lose on a crash.
01 What is on disk when Redis dies?
Why Redis is so fast explained that Redis serves every command from memory. Memory does not survive a restart, a crash or a power cut, so Redis copies data to disk in one of two ways, or both:
dump.rdb.The question to ask of each is not “is my data safe?” but “if the server dies now, which acknowledged writes are gone?”
02 RDB: a snapshot every so often
With no config file, Redis 7 and later take a snapshot once 3600 seconds have passed since the last one and at least 1 key has changed, or 300 seconds and 100 changes, or 60 seconds and 10,000 changes. You can see the rule and trigger one by hand:
BGSAVE calls fork(). The child gets a copy of the parent’s memory as it was at that instant and writes it out, while the parent carries on serving clients. The copy is cheap because of copy-on-write: parent and child share memory pages, and the kernel only duplicates a page when the parent writes to it during the save.
Anything written after the fork is not in the file. Here is a real run on Redis 8.4 with the default settings and no AOF. Two writes, a snapshot, one more write, then the process is killed (port and paths trimmed):
order:3 was acknowledged and is gone. With RDB alone, what you lose is everything since the last snapshot: on a quiet server with the default rules, up to an hour. A clean SHUTDOWN is different: Redis saves a final snapshot before it exits, so planned restarts lose nothing.
03 AOF: a log of every write
Turn on appendonly yes and Redis appends each write command to a file, in the same RESP format clients use on the wire. Since Redis 7 the AOF is a folder: a base file, an incr file that grows with new writes, and a manifest. This is the incr file after four writes, from a real run:
On start, Redis loads the base file, then replays every command in the incr file in order. The same kill as before, with AOF on, brought back all four keys. When AOF is enabled Redis loads it and ignores dump.rdb, because the log is the more complete record.
Appending is not the same as being on disk. write() hands bytes to the operating system, which keeps them in its page cache and writes them to the drive later. Only fsync() forces them onto the drive. appendfsync decides how often Redis calls it:
| Setting | When Redis fsyncs | Lost on power cut or kernel crash | Cost |
|---|---|---|---|
| always | after every batch of writes, before replying | nothing that was acknowledged | every write waits for the disk |
| everysec (default) | once a second, in a background thread | about the last second (up to 2 s if the disk stalls) | small |
| no | when the OS decides | whatever the OS had not flushed: typically up to about 30 s on Linux | none |
04 How much you lose, drawn
Five writes over three seconds, with both RDB and AOF (everysec) on. The chips show what each method would lose if the power failed at that moment.
- just arrived
- in page cache only
- held
- lost
- loaded on restart
- not written
Redis starts empty, with both kinds of persistence on: RDB snapshots and an append-only file with the default appendfsync everysec. Five writes, w1 to w5, will arrive over three seconds.
05 A crash is not a power cut
Redis normally writes to the AOF before it replies to the client, so every acknowledged write is at least in the page cache. If only the Redis process dies (a bug, kill -9, the out-of-memory killer), the operating system is still running and still writes its page cache to disk. Nothing acknowledged is lost, even with appendfsync no. That is why the kill -9 test above kept all four keys.
The fsync setting only matters when the machine stops: power loss, a kernel panic, a cloud VM that is suddenly gone. RDB is the same in both cases, because the snapshot only ever holds what was in memory when the last fork happened.
| Method | Redis process killed | Machine loses power |
|---|---|---|
| None | everything | everything |
| RDB, default save rules | everything since the last snapshot | everything since the last snapshot |
| AOF everysec | nothing acknowledged | about the last second |
| AOF always | nothing acknowledged | nothing acknowledged |
06 Keeping the log short
A log of every write grows forever. Increment one counter a million times and the AOF holds a million commands for one key. So Redis rewrites it: a forked child writes the current dataset as a new base file, and new writes go to a fresh incr file. Real listing, after BGREWRITEAOF:
The base file ends in .rdb because, with aof-use-rdb-preamble yes (the default), it is written in the compact RDB format. Redis rewrites on its own when the AOF has doubled since the last rewrite (auto-aof-rewrite-percentage 100) and is at least 64 MB (auto-aof-rewrite-min-size).
So the common production setup is both: AOF with everysec to bound the loss to about a second, plus RDB snapshots as compact files to copy off the machine for backups.
07 When it bites
A replica copies whatever its master holds. If a master with persistence off restarts, it comes back empty, and its replicas sync to that empty dataset and delete everything. Either keep persistence on the master, or stop it restarting automatically.
vm.overcommit_memory = 0Fork fails on a big instanceWith Linux’s default overcommit setting the kernel may refuse to fork a process that looks too large, and BGSAVE fails. Redis logs a warning at start-up; the fix it suggests is vm.overcommit_memory = 1.maxmemory 14gb # on a 16 GB machineNo room for copy-on-writeEvery page the parent changes during a save is copied. A write-heavy instance can need close to twice its memory while a snapshot or AOF rewrite runs. Leave headroom, and check rdb_last_cow_size in INFO persistence.INFO stats # latest_fork_usecFork pausesfork() copies the page tables, and the main thread waits while it does. On a large dataset that pause grows with memory size and shows up as a latency spike on every save. latest_fork_usec tells you how long the last one took.docker run redis # no volumeFiles that are not where you thinkdump.rdb and appendonlydir live in dir (/data in the official image). If that directory is not on durable storage, persistence works perfectly and still loses everything when the container is replaced.appendfsync alwaysAlways is not freeEach event-loop batch waits for the disk before clients get replies. On slow or network-attached disks that can cost far more throughput than the second of data it saves. Measure before choosing it.