Practical 0 · Your Redis, crashed
Goal: a Redis you control, a Go program that counts what Redis acknowledged, a crash that loses everything, snapshots that lose only what
INFOpredicted, and aMISCONFyou cause, diagnose and fix.Time: ~40 minutes. Where: your own computer: files in
~/redis-persistence/, commands in two terminals in that folder. Needs Docker, Go 1.21 or newer, andcurl. Read lesson 1 and lesson 2 first.
Task 1 · Get the lab
mkdir -p ~/redis-persistence
cd ~/redis-persistence
curl -fo lab http://localhost:8800/redis-persistence-deep/tools/lab
chmod +x lab
./lab setup p0
It prints one line per step:
✓ conf/redis.conf in your workspace: persistence off
starting Redis 8.10.3 (a few seconds; longer the first time, while Docker downloads redis:8.10.3)…
✓ Redis 8.10.3 in a container named ...
✓ Go ... (Go fetches the 1.26 toolchain go-redis needs by itself)
Your Redis is the container redis-persist, reachable from your computer at localhost:6390. Its config is your
file conf/redis.conf: open it, it’s three settings, and snapshots are off (save "").
What setup did
It downloaded conf/redis.conf, created a Docker volume redis-persist-data, and ran:
docker run -d --name redis-persist --label fromzero.course=redis-persistence-deep \
-p 127.0.0.1:6390:6379 -v redis-persist-data:/data \
-v "$HOME/redis-persistence/conf:/usr/local/etc/redis" \
redis:8.10.3 redis-server /usr/local/etc/redis/redis.conf
| Part | Why |
|---|---|
-p 127.0.0.1:6390:6379 | your Go program connects from your computer; 6390, not Redis’s usual 6379, so it can’t collide with a Redis you already run; 127.0.0.1 keeps it off your network |
-v redis-persist-data:/data | Redis writes dump.rdb in /data; a named volume keeps it across docker rm |
-v .../conf:/usr/local/etc/redis | your folder, mounted: edit conf/redis.conf on your computer, then docker restart redis-persist |
--label | marks it as this course’s, so ./lab reset removes only what setup made |
Port 6390 taken? REDIS_PORT=6400 ./lab setup p0, then run your writer with REDIS_ADDR=localhost:6400. Running
setup again keeps your container and your redis.conf. Every command is in lab.log.
Task 2 · Write the writer
Create a Go module in ~/redis-persistence/writer/ with a program that:
- connects to the address in
REDIS_ADDR, orlocalhost:6390when it’s not set, with go-redis v9.23.0 (newest release) - runs
INCR writesabout 100 times a second (a flag-rate, default 100), so each acknowledged write has a number - prints
acknowledged Neach time N is a multiple of the rate (once a second) - on the first error, prints
write failed: <the error>, thenlast acknowledged write: N, and exits with status 1 - does not retry: go-redis retries failed commands 3 times by default, which would hide the crash
Hint
go mod init writer, then go get github.com/redis/go-redis/v9@v9.23.0. redis.NewClient(&redis.Options{Addr: ..., MaxRetries: -1}) turns retries off. A time.NewTicker(time.Second / time.Duration(rate)) paces the loop;
rdb.Incr(ctx, "writes").Result() returns the new value.
Solution
mkdir -p ~/redis-persistence/writer && cd ~/redis-persistence/writer
go mod init writer
go get github.com/redis/go-redis/v9@v9.23.0
// writer: numbered writes to Redis at a steady rate; prints the last one Redis acknowledged.
package main
import (
"context"
"flag"
"fmt"
"os"
"time"
"github.com/redis/go-redis/v9"
)
func main() {
rate := flag.Int("rate", 100, "writes per second")
key := flag.String("key", "writes", "the counter to INCR")
flag.Parse()
addr := os.Getenv("REDIS_ADDR")
if addr == "" {
addr = "localhost:6390"
}
rdb := redis.NewClient(&redis.Options{Addr: addr, MaxRetries: -1})
ctx := context.Background()
var last int64
tick := time.NewTicker(time.Second / time.Duration(*rate))
for range tick.C {
n, err := rdb.Incr(ctx, *key).Result()
if err != nil {
fmt.Printf("write failed: %v\nlast acknowledged write: %d\n", err, last)
os.Exit(1)
}
last = n
if n%int64(*rate) == 0 {
fmt.Printf("acknowledged %d\n", n)
}
}
}
Run it from ~/redis-persistence/writer with go run . for a few seconds, then press Ctrl-C. You should see
acknowledged 100, acknowledged 200, …
With a Go older than 1.26, go get says switching to go1.26.11, and the first go run . pauses
while Go downloads the toolchain go.mod now asks for; that happens once.
Task 3 · Crash it with persistence off
Predict first: the writer runs for 20 seconds, then the container is killed. After a restart, what’s in
writes?
Terminal 1, in ~/redis-persistence/writer: go run . Terminal 2, about 20 seconds later:
docker kill redis-persist
Terminal 1 ends with write failed: EOF and the last acknowledged number. Now:
docker start redis-persist
docker exec redis-persist redis-cli GET writes
What you see
An empty line: the key doesn’t exist. Every acknowledged write is gone. Try docker restart instead of the kill
(start the writer again first): with save "" there’s no rule, so the clean shutdown skips its final save too, as
lesson 1’s Go deeper says, and the key is gone again.
Task 4 · Turn on snapshots and predict the loss
Change conf/redis.conf so Redis snapshots every 10 seconds when at least one key changed, then restart and
confirm Redis took it:
docker restart redis-persist
docker exec redis-persist redis-cli CONFIG GET save
Solution
Replace save "" with save 10 1. CONFIG GET save then prints save and 10 1.
Start the writer again in terminal 1 and let it run for 20 to 30 seconds.
Predict first: read the counter and kill Redis in one command, so as little time as possible passes between them. How many writes will be lost?
docker exec redis-persist redis-cli INFO persistence | grep rdb_changes_since_last_save; docker kill redis-persist
docker start redis-persist
docker exec redis-persist redis-cli GET writes
What you see
Lost = the writer’s last acknowledged write minus GET writes, and it’s the rdb_changes_since_last_save you read,
plus the few writes acknowledged between that read and the kill. In the run this course was written from:
rdb_changes_since_last_save:462
...
last acknowledged write: 2539
...
2072
2539 − 2072 = 467 lost, 462 predicted. Yours depends on where in the 10-second cycle the kill landed: anything from
a handful to about 1,000. The counter now continues from what survived: the next go run . starts from there.
Task 5 · Break it: a disk Redis can’t write to
Start the writer in terminal 1, then in terminal 2 take away Redis’s permission to write in /data (-u root
runs the command as root in the container):
docker exec -u root redis-persist chmod 555 /data
Within 10 seconds or so (as soon as the next snapshot is due) the writer stops:
write failed: MISCONF Redis is configured to save RDB snapshots, but it's currently unable to persist to disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.
Investigate in this order, as you would in production:
-
Is it persistence?
docker exec redis-persist redis-cli INFO persistence | grep -E 'bgsave_status|failures'showsrdb_last_bgsave_status:errandrdb_saves_consecutive_failures:1. -
Why did the save fail? The log says:
docker logs redis-persist 2>&1 | grep -E 'Failed|error' | tail -256:C 26 Nov 2026 07:54:32.064 # Failed opening the temp RDB file temp-56.rdb (in server root dir /data) for saving: Permission denied 1:M 26 Nov 2026 07:54:32.165 # Background saving error -
Fix the cause, not the symptom:
docker exec -u root redis-persist chmod 755 /data. Wait 5 to 10 seconds (Redis retries the save by itself), then checkrdb_last_bgsave_statusisokagain and start the writer: it works, with no restart.
Why not just turn the check off?
CONFIG SET stop-writes-on-bgsave-error no makes the error go away, and with it the only sign that nothing is
being saved: the next crash would lose everything since the last good snapshot, however long ago. Turn it off only
when something else (monitoring of rdb_last_bgsave_status) would page you.
Check your work
Your redis.conf should still say save 10 1, and Redis should be running with it.
./lab check p0
All 15 checks should pass. The checker reads your container, conf/redis.conf, writer/ (it builds it), the
writes counter, and whether Redis’s last start loaded your keys from a snapshot (Task 4); it changes nothing.