3 · The append-only file and fsync
In one sentence: with the append-only file (AOF) on, Redis writes every write command to a log before it replies, so killing Redis loses nothing it acknowledged;
appendfsyncdecides how much a dead machine would lose.
The problem it solves
In Practical 0’s example run, save 10 1 lost 467 acknowledged writes on a kill: everything since the last snapshot. Snapshotting
more often means forking more often, and a snapshot writes the whole dataset to save a few changes. The writes
themselves are small. Write them down, one after another, and replay them on restart: that’s the log from
lesson 1’s bank, the list of every transaction.
The log, on disk
Start a Redis with the log on, and snapshots off so only the log is at work:
docker run -d --name demo -v demo-data:/data redis:8.10.3 redis-server --appendonly yes --save ""
docker exec demo redis-cli SET order:1 paid
docker exec demo redis-cli INCR writes
docker exec demo redis-cli INCR writes
docker exec demo ls -l /data/appendonlydir
-rw------- 1 redis redis 89 Nov 26 10:38 appendonly.aof.1.base.rdb
-rw------- 1 redis redis 111 Nov 26 10:39 appendonly.aof.1.incr.aof
-rw------- 1 redis redis 102 Nov 26 10:38 appendonly.aof.manifest
Three files, in a directory of their own (appenddirname, default appendonlydir):
| File | Holds |
|---|---|
…base.rdb | the dataset as of the last rewrite (below); empty here, 89 bytes of header |
…incr.aof | every write since then, appended as it happens |
…manifest | which base and incr files make up the log, in order |
The incr file is the commands themselves, in the protocol clients use to talk to Redis (RESP: *2 = a command
of 2 parts, $4 = the next part is 4 bytes):
*2
$6
SELECT
$1
0
*3
$3
SET
$7
order:1
$4
paid
*2
$4
INCR
$6
writes
The two INCRs are logged as INCR, not as their results. On restart Redis runs the commands again, in order.
What a kill loses now
The writer from Practical 0, at 1,000 writes a second, against appendonly yes with each appendfsync policy,
killed after 10 seconds, twice each:
appendfsync | Last acknowledged | After the restart | Lost |
|---|---|---|---|
always | 9215 · 10027 | 9216 · 10027 | −1 · 0 |
everysec | 10019 · 10000 | 10019 · 10000 | 0 · 0 |
no | 10042 · 10082 | 10042 · 10082 | 0 · 0 |
Nothing lost, under any policy. Redis appends each write to a buffer, and writes the buffer to the file “before
replying to the client” (aof.c). The bytes are then in the kernel’s page cache, and a kill doesn’t touch the
kernel. appendfsync no never calls fsync() and still lost nothing, so the reason is the write(), not the
fsync().
And −1, 9216 for 9215? That last INCR reached the file; its reply never reached the writer, which saw EOF. A failed
write is unknown, not undone: retry only writes that are safe to repeat.
appendfsync: what a power cut adds
A dead machine takes the page cache with it (lesson 1), so what survives is what was fsynced. docker kill
can’t show this (it never kills the kernel), so the losses come from the Redis documentation; the costs are
measured, with redis-benchmark doing SETs inside the container:
appendfsync | fsync() | A power cut loses | SETs/s, 1 client | SETs/s, 50 clients |
|---|---|---|---|---|
always | before every reply | nothing acknowledged (“very very slow, very safe”) | 3,296 | 77,882 |
everysec (default) | once a second, in a background thread | about the last second (“you may lose 1 second of data”) | 26,116 | 318,471 |
no | never: the kernel writes when it likes | what the kernel hadn’t written (“normally Linux will flush data every 30 seconds”) | 25,813 | 341,297 |
| AOF off | 26,976 | 342,466 |
everysec costs almost nothing here. always makes one client about 8 times slower, because every write waits for
the disk; 50 clients lose less (about 4 times), because one fsync() covers all the writes Redis gathered in one pass.
The 30 seconds is this kernel’s dirty_expire_centisecs (3000, in hundredths of a second).
These numbers are from Docker Desktop’s virtual disk on a laptop. The cost of fsync() is the disk’s, so it differs
from disk to disk: measure yours (Practical 1 does).
Rewrite: keeping the log short
The log only grows. 100,000 INCR writes later:
-rw------- 1 redis redis 89 Nov 26 10:38 appendonly.aof.1.base.rdb
-rw------- 1 redis redis 2600111 Nov 26 10:39 appendonly.aof.1.incr.aof
2.6 MB, 26 bytes per INCR, to hold one number. BGREWRITEAOF rewrites it:
-rw------- 1 redis redis 121 Nov 26 10:39 appendonly.aof.2.base.rdb
-rw------- 1 redis redis 0 Nov 26 10:39 appendonly.aof.2.incr.aof
-rw------- 1 redis redis 107 Nov 26 10:39 appendonly.aof.manifest
Like BGSAVE, it forks: the child writes the current dataset as a new base file (in RDB format, since
aof-use-rdb-preamble yes is the default), while the parent sends new writes to a new incr file. When the
child is done, Redis switches the manifest to the new pair and deletes the old files. The log says each step:
1:M 26 Nov 2026 10:39:13.438 * Creating AOF incr file appendonly.aof.2.incr.aof on background rewrite
1:M 26 Nov 2026 10:39:13.438 * Background append only file rewriting started by pid 105
1:M 26 Nov 2026 10:39:13.501 * Successfully renamed the temporary AOF base file temp-rewriteaof-bg-105.aof into appendonly.aof.2.base.rdb
1:M 26 Nov 2026 10:39:13.503 * Removing the history file appendonly.aof.1.incr.aof in the background
Redis rewrites by itself when the log has grown by auto-aof-rewrite-percentage (100: doubled) since the last
rewrite and is at least auto-aof-rewrite-min-size (64 MB). A rewrite costs a fork, like a snapshot (lesson 2).
The fields to watch, in INFO persistence:
| Field | Means |
|---|---|
aof_last_write_status | ok or err: can Redis write the log? |
aof_rewrite_in_progress | 1 while a rewrite’s child runs |
aof_current_size / aof_base_size | the log now / right after the last rewrite |
aof_delayed_fsync | times everysec had to wait for a slow disk (Go deeper) |
Try it
Practical 1, after lesson 4, has you write a Go program that crashes Redis under load, and run it against
snapshots, everysec and always on a dataset you size yourself.
Go deeper: everysec when the disk is slow
With everysec, the fsync() runs in a background thread, so the main thread never waits for it. But on Linux a
write() to a file that’s being fsynced can block. So if the previous fsync() is still running, Redis
postpones its write(), for up to 2 seconds. After that it writes anyway,
logs Asynchronous AOF fsync is taking too long (disk is busy?), and adds 1 to aof_delayed_fsync.
So on a struggling disk, everysec can lose up to about 2 seconds in a power cut, not 1, and aof_delayed_fsync
growing is your sign. (aof.c, flushAppendOnlyFile)
A related setting, no-appendfsync-on-rewrite yes, skips fsyncs while a rewrite or snapshot child is writing, to
avoid that contention; redis.conf warns it can lose up to 30 seconds of log in the worst case. The default is no.
(redis.conf,
persistence docs)
Common misunderstandings
- “
alwaysprotects against a crash better thaneverysec.” Against a crash of Redis, both lose nothing. The difference is a crash of the machine. - “A rewrite reads the old log.” It writes the dataset from memory, like a snapshot.