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

1 · Why a restart empties Redis

In one sentence: Redis answers OK once a write is in its memory, and memory dies with the process, so anything Redis hasn’t copied to disk is lost when it’s killed.

The problem

Start Redis 8.10.3 from the official image with no config of your own, write 50 keys, and kill the container:

docker run -d --name demo redis:8.10.3
docker exec demo sh -c 'for i in $(seq 1 50); do redis-cli SET order:$i paid >/dev/null; done; redis-cli DBSIZE'
docker kill demo
docker start demo
docker exec demo redis-cli DBSIZE
50
...
0

All 50 are gone. Every one of them got an OK, so the program that wrote them believes they’re safe.

Now do the same, but stop the container with docker stop instead of docker kill. This time all 50 come back, and Redis’s log says why:

1:signal-handler (1795677253) Received SIGTERM scheduling shutdown...
1:M 26 Nov 2026 07:14:13.558 * User requested shutdown...
1:M 26 Nov 2026 07:14:13.558 * Saving the final RDB snapshot before exiting.
1:M 26 Nov 2026 07:14:13.560 * DB saved on disk

So Redis can write its data to disk. The kill just never gave it the chance.

Why the kill lost everything

The two commands send different signals:

SignalWhat Redis can do
docker stopSIGTERM, then SIGKILL after 10 s if it’s still runningcatch it, save a final snapshot, exit
docker killSIGKILLnothing: the kernel removes the process at once; SIGKILL can’t be caught

A crash, the kernel’s out-of-memory killer and a power cut are all like the kill: no warning. So the only data that survives them is what Redis had already copied to disk while it was running.

By default it does copy, on a schedule. Ask the fresh container:

docker exec demo redis-cli CONFIG GET save
save
3600 1 300 100 60 10000

These are three save rules, as pairs of seconds and changes: save after an hour if at least 1 key changed, after 5 minutes if at least 100 changed, after 60 seconds if at least 10,000 changed. Fifty writes in a few seconds match none of them yet. Just before the kill, Redis’s own counters said so:

rdb_changes_since_last_save:50
rdb_saves:0

No save had happened, and ls /data after the restart showed an empty directory. Lesson 2 is about these rules.

Two ways to keep a copy

There are only two ideas, and Redis has both:

Snapshot (RDB)Log (AOF, the append-only file)
What’s writtenthe whole dataset as it is at one moment, into dump.rdbevery write command, appended as it happens
Whenby the save rules, or when you ask (BGSAVE)continuously
A crash loseseverything since the last snapshotonly what hadn’t reached the file yet
Restartload one compact filereplay the log

Think of a bank: a snapshot is the end-of-day balance sheet; a log is the list of every transaction. The balance sheet is small and quick to read, but a fire at 4 pm loses the day. The list loses almost nothing, but it grows and takes longer to replay. Lessons 2 to 4 measure both.

What both rest on: the page cache and fsync

Writing a file is two steps, and the gap between them decides what a power cut takes:

 Redis's memory ──write()──▶ page cache (kernel memory) ──fsync() or later──▶ disk
  1. write() hands bytes to the kernel. It returns as soon as they’re in the kernel’s page cache, memory the kernel uses to hold file data before it reaches the disk. The kernel writes them out a little later, on its own schedule.
  2. fsync() makes the program wait until the file’s data is actually on the device.

This gives two kinds of crash:

  • Redis dies (a kill, a bug, the out-of-memory killer): the kernel is still running, so whatever Redis had passed to write() is safe in the page cache and still reaches the disk.
  • The machine dies (a power cut, a kernel panic): the page cache is lost too. Only what was fsynced survives.

docker kill kills a process, never the kernel, so it’s always the first kind. This course measures that one, and lesson 3 shows what the second would add.

One more building block, fork(), gets its own explanation in lesson 2: it’s how Redis writes a snapshot without pausing for it.

Try it

Practical 0, after lesson 2, has you start your own Redis with persistence off, write to it from a Go program, kill it, and count what’s gone; then turn snapshots on and count again.

Go deeper: what Redis does on SIGTERM

SIGTERM (and SIGINT, Ctrl-C) start the same sequence as the SHUTDOWN command: wait up to shutdown-timeout (10 s) for lagging replicas, stop all clients, run a blocking save if at least one save rule is configured, flush the append-only file if it’s on, then exit.

A Redis with no save rules and no AOF (a pure cache) skips the save on purpose, so shutting down doesn’t block. SHUTDOWN NOSAVE skips it on demand; SHUTDOWN SAVE forces one even with no rules. (SHUTDOWN, signal handling)

Two consequences: a big dataset makes docker stop slow (the save is blocking), and if the save takes longer than Docker’s 10 s grace period, SIGKILL arrives mid-save. docker stop -t 60 gives it longer; stop_grace_period in Compose and terminationGracePeriodSeconds in Kubernetes do the same.

Common misunderstandings

  • “OK means it’s on disk.” OK means it’s in Redis’s memory. Whether it’s on disk depends on the persistence settings, which is what this course is about.
  • “The default Redis doesn’t persist.” It does: the default has three save rules. It just saves rarely when there are few writes, and never between the last save and a kill.
  • “A restart is a restart.” docker restart and docker stop send SIGTERM first and get a final snapshot if a save rule is set (and an AOF flush if the AOF is on); a crash gets neither. Test with a kill.

Check yourself

1. A Redis with the default rules has had 80 writes since its last save, 10 minutes ago. The kernel's out-of-memory killer kills it. How many of those writes come back? None. 80 is below the 100 of the 5-minute rule, and the 1-hour rule hasn't come due, so no snapshot holds them, and an out-of-memory kill is a SIGKILL: no final save.
2. Redis passed a write to write() but hadn't fsynced it, and then the Redis process crashed. Is the write lost? No. It's in the kernel's page cache, and the kernel is still running, so it still reaches the disk. It would be lost if the whole machine had lost power before the kernel wrote it out.
3. Your Redis is a cache you can rebuild from the database. It has no save rules and no AOF. What does docker stop save? Nothing: with no save rule and no AOF, shutdown skips the save on purpose. For a cache that's the point: it restarts empty and quickly.

Next: lesson 2 · RDB snapshots.