Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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; appendfsync decides 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):

FileHolds
…base.rdbthe dataset as of the last rewrite (below); empty here, 89 bytes of header
…incr.aofevery write since then, appended as it happens
…manifestwhich 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:

appendfsyncLast acknowledgedAfter the restartLost
always9215 · 100279216 · 10027−1 · 0
everysec10019 · 1000010019 · 100000 · 0
no10042 · 1008210042 · 100820 · 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:

appendfsyncfsync()A power cut losesSETs/s, 1 clientSETs/s, 50 clients
alwaysbefore every replynothing acknowledged (“very very slow, very safe”)3,29677,882
everysec (default)once a second, in a background threadabout the last second (“you may lose 1 second of data”)26,116318,471
nonever: the kernel writes when it likeswhat the kernel hadn’t written (“normally Linux will flush data every 30 seconds”)25,813341,297
AOF off26,976342,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:

FieldMeans
aof_last_write_statusok or err: can Redis write the log?
aof_rewrite_in_progress1 while a rewrite’s child runs
aof_current_size / aof_base_sizethe log now / right after the last rewrite
aof_delayed_fsynctimes 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

  • “always protects against a crash better than everysec.” 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.

Check yourself

1. appendonly yes, appendfsync no. The out-of-memory killer kills Redis. How many acknowledged writes are lost? None. Every acknowledged write was passed to `write()` before its reply, so it's in the page cache, and the kernel (still running) writes it out. `fsync()` would only matter if the machine had died.
2. Your writer gets EOF on an INCR during a kill. After the restart, has that INCR happened? You can't tell from the client: it may be in the log (the reply was lost) or not (it never arrived). Read the value after the restart, or make the write safe to repeat.
3. aof_current_size is 130 MB, aof_base_size is 60 MB, the defaults are in place. Will Redis rewrite by itself? Yes: 130 MB is more than double 60 MB (100 % growth) and over the 64 MB minimum.

Next: lesson 4 · Persistence in production.