6. Persistence and Durability
Redis keeps the whole dataset in memory, so a restart, a crash or a power cut would empty it unless the data is also written to disk. This chapter covers the two persistence mechanisms, RDB snapshots and the append-only file (AOF): how each one reaches the disk without stalling the single-threaded server, and how to combine them, turn them off, back them up and restore them. These settings decide how many acknowledged writes you can lose, how long a restart takes, and how much extra memory and disk I/O the server needs.
<a id="61-rdb-snapshots"></a>
6.1 RDB Snapshots
This subchapter covers point-in-time snapshots. It explains what an RDB file is and which commands create one. It shows how fork and copy-on-write let a child process write a consistent snapshot while the server keeps accepting writes, how to schedule snapshots automatically, and what the file contains.
How Snapshotting Works
The simplest way to make in-memory data survive a restart is to write all of it to a file from time to time and read the file back at startup. That is what an RDB snapshot does. RDB stands for Redis Database. It is a compact binary file, dump.rdb by default, that holds every key, value and expiration time as they were at one instant.
Think of a whiteboard that the cleaning staff wipe every night. If you photograph it every hour, you can always rebuild it from the latest photo. You only lose what was written since that photo was taken. A snapshot is that photo. It is cheap to take, easy to copy and fast to restore, but it is always a little out of date.
Point-in-time consistency
A snapshot holds the state at the moment it started. It never mixes the states before and after a write. For example, a transfer that moves 10 from balance:a to balance:b in a MULTI/EXEC block appears in the snapshot either completely or not at all, because transactions are atomic (chapter 4.2) and the snapshot freezes the data between two commands (see "Fork and Copy-on-Write").
Lifecycle of a snapshot
trigger (save point, BGSAVE, SHUTDOWN, full sync of a replica, FLUSHALL with save points)
│
▼
fork() ──▶ child process sees a frozen copy of the dataset
│ │
│ ├─ write every DB, key, value and TTL to <dir>/temp-<pid>.rdb
│ ├─ fsync the temp file
│ └─ rename(temp-<pid>.rdb, dump.rdb) atomic: the old file is replaced in one step
│
▼
parent keeps serving clients, then records the result
(rdb_last_save_time, rdb_last_bgsave_status, rdb_changes_since_last_save = 0)
Because the new file replaces the old one with an atomic rename, dump.rdb is always a complete snapshot, either the previous one or the new one, never a half-written file. This is why you can copy it at any time.
What triggers a snapshot
| Trigger | Blocking? | Notes |
|---|---|---|
A save rule is met (see "Snapshot Configuration") | No (fork) | The usual, automatic way |
BGSAVE | No (fork) | Manual, for example before a backup |
SAVE | Yes | Runs on the main thread. A last resort |
SHUTDOWN / SIGTERM with save points configured | Yes | The server is stopping anyway, so it saves synchronously |
| A replica needs a full resynchronization (chapter 7.1) | No (fork) | Streamed directly to the replica when diskless sync is on (the default since 7.0) |
FLUSHALL with save points configured | Yes | Overwrites dump.rdb with the now-empty dataset, so the previous snapshot cannot rescue you |
Loading at startup
When the AOF is disabled, Redis reads <dir>/<dbfilename> at startup and serves commands only once the file is fully loaded. Clients that connect during loading get -LOADING Redis is loading the dataset in memory. Loading an RDB file is fast because the data is already serialized in its final form and the compact encodings (chapter 5.2) are stored as ready-made blobs. Expect seconds for a few gigabytes on fast storage.
The data loss window
time ─────────────────────────────────────────────────────────────▶
snapshot #1 snapshot #2 crash
│ │ │
▼ ▼ ▼
────────●─────────────────────────●──────── writes ─────────✖
└──── lost on restart ────┘
Every write after the start of the last successful snapshot is lost when the server dies. With snapshots every few minutes, that can be minutes of data. RDB alone suits caches, analytics that can be rebuilt, and backups. It does not suit data where each write matters.
Example: taking and inspecting a snapshot
127.0.0.1:6379> CONFIG GET dir
1) "dir"
2) "/var/lib/redis"
127.0.0.1:6379> CONFIG GET dbfilename
1) "dbfilename"
2) "dump.rdb"
127.0.0.1:6379> SET user:1 alice
OK
127.0.0.1:6379> BGSAVE
Background saving started
127.0.0.1:6379> LASTSAVE # Unix time of the last successful save
(integer) 1762770000
127.0.0.1:6379> INFO persistence
# Persistence
loading:0
rdb_changes_since_last_save:0 # writes not yet in any snapshot
rdb_bgsave_in_progress:0
rdb_last_save_time:1762770000
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_last_cow_size:0
...
Example: proving that a snapshot survives a restart
mkdir -p /tmp/rdb-demo
# A throwaway server on port 6390 that writes its RDB file into /tmp/rdb-demo
redis-server --port 6390 --dir /tmp/rdb-demo --save "" --appendonly no --daemonize yes
redis-cli -p 6390 SET greeting "hello"
redis-cli -p 6390 BGSAVE
sleep 1 # let the tiny snapshot finish
redis-cli -p 6390 SET lost "written after the snapshot" # will NOT survive
# SIGKILL: no chance to save on exit, just like a crash
kill -9 "$(redis-cli -p 6390 INFO server | awk -F: '/^process_id/ {print $2}' | tr -d '\r')"
redis-server --port 6390 --dir /tmp/rdb-demo --save "" --appendonly no --daemonize yes
redis-cli -p 6390 GET greeting # "hello" (it was in the snapshot)
redis-cli -p 6390 GET lost # (nil) (written after the snapshot)
redis-cli -p 6390 SHUTDOWN NOSAVE
Key Takeaways
- An RDB file is a compact binary image of the entire dataset at one instant.
- Snapshots are written to a temp file and atomically renamed, so
dump.rdbis always complete and safe to copy. - Everything written since the start of the last successful snapshot is lost on a crash.
FLUSHALLwith save points configured immediately overwritesdump.rdbwith an empty dataset.- RDB loads quickly at startup, which makes restarts and replica full syncs fast.
Practice
- (Beginner) Run the "survives a restart" example. Then repeat it with
SHUTDOWN(noNOSAVE) instead ofkill -9and a save point configured (--save "60 1"), and explain the different result.- (Intermediate) Fill a test instance with 1 million keys (
DEBUG POPULATE 1000000on a test server withenable-debug-command local, or a pipelined script). Measure the size ofdump.rdb, the duration ofBGSAVE(rdb_last_bgsave_time_sec), and the startup time from the log lineDB loaded from disk.- (Interview) "Our Redis takes a snapshot every 5 minutes. A product manager says, 'so we lose at most 5 minutes of data'. Is that right?" Hint: consider when a snapshot's content is frozen versus when it finishes, and what happens when a
BGSAVEfails.
SAVE vs BGSAVE
Writing a multi-gigabyte file takes seconds or minutes. Redis executes all commands on one thread (chapter 5.1), so if that thread writes the file itself, no client is served until the file is done. Redis therefore offers two commands: one that writes the snapshot on the main thread, and one that hands the work to a child process.
SAVE
SAVE serializes the dataset on the main thread and replies OK when the file is on disk. During that time, every other client waits: reads, writes, health checks and replication pings. Its only advantages are that it needs no fork, so there is no copy-on-write memory overhead, and that the snapshot is guaranteed to be finished when the command returns. Use it only when BGSAVE cannot work, for example when the fork keeps failing for lack of memory and you need a snapshot before stopping the server.
BGSAVE
BGSAVE forks a child process, replies Background saving started immediately, and the child writes the file while the parent keeps serving clients. The main thread pays only for the fork() call itself (see the next topic).
- Only one background save can run at a time. A second
BGSAVEreturns an error while the first is still running. - While an AOF rewrite (6.2) is running,
BGSAVEfails.BGSAVE SCHEDULEinstead repliesBackground saving scheduledand starts the save as soon as the rewrite ends. - The reply means "started", not "done". To know when the snapshot is finished, watch
rdb_bgsave_in_progressandrdb_last_bgsave_statusinINFO persistence, or wait forLASTSAVEto change.
Saving on shutdown
| Command | Effect on RDB |
|---|---|
SHUTDOWN (or SIGTERM) | Runs a blocking save if any save point is configured. With the AOF on, the AOF is also flushed and fsynced |
SHUTDOWN SAVE | Forces a save even if no save point is configured |
SHUTDOWN NOSAVE | Never saves, even with save points (writes since the last snapshot are lost) |
kill -9 / crash / OOM kill | Nothing is saved |
If the save on shutdown fails, for example because the disk is full, Redis does not exit. It logs the error and keeps running so the data is not lost. SHUTDOWN NOSAVE or SHUTDOWN FORCE (Redis 7.0+) overrides this.
SAVE | BGSAVE | |
|---|---|---|
| Runs in | Main thread | Forked child |
| Blocks clients | For the whole save | Only for the fork() call |
| Extra memory | None | Copy-on-write pages (up to the dataset size in the worst case) |
| Reply means | Snapshot is on disk | Snapshot has started |
| Typical use | Last resort; shutdown | Routine snapshots, backups |
Example: waiting for a background save from Python
import time
import redis
r = redis.Redis(decode_responses=True)
def snapshot(timeout_s: float = 600.0) -> None:
"""Trigger BGSAVE and block until it finishes successfully."""
before = r.info("persistence")["rdb_saves"] # counts snapshots since startup (Redis 7.0+)
r.bgsave(schedule=True) # sends BGSAVE SCHEDULE: never fails because of a running AOF rewrite
deadline = time.monotonic() + timeout_s
while time.monotonic() < deadline:
info = r.info("persistence")
# Done when no save is running AND the counter moved past our starting point
if info["rdb_bgsave_in_progress"] == 0 and info["rdb_saves"] > before:
if info["rdb_last_bgsave_status"] != "ok":
raise RuntimeError("BGSAVE failed, check the Redis log")
return
time.sleep(0.2)
raise TimeoutError("BGSAVE did not finish in time")
snapshot()
The loop uses the rdb_saves counter rather than LASTSAVE. LASTSAVE has a resolution of one second, so two saves that finish within the same second look identical.
Key Takeaways
SAVEblocks every client for the whole save.BGSAVEblocks only for thefork().BGSAVEreplies when the save starts. Confirm completion withINFO persistenceorLASTSAVE.BGSAVE SCHEDULEavoids errors when an AOF rewrite is already running.SHUTDOWNsaves synchronously when save points exist, andNOSAVEandSAVEoverride that.kill -9saves nothing.
Practice
- (Beginner) On a test instance with a few hundred MB of data, run
redis-cli --latencyin one terminal and alternateSAVEandBGSAVEin another. Record the maximum latency for each.- (Intermediate) Write a shell function that runs
BGSAVE SCHEDULEand exits non-zero if the save fails or takes longer than 5 minutes, using onlyredis-cliandINFO persistence.- (Interview) "Why does Redis still offer a blocking
SAVEcommand at all?" Hint: think about whatBGSAVErequires from the operating system, and when that requirement cannot be met.
Fork and Copy-on-Write
A snapshot must show the data at one instant, but the main thread keeps changing it while the file is being written. There are three ways to get a consistent view:
- Stop the writes until the file is written. This is
SAVE, and it means downtime. - Copy the whole dataset in memory and write the copy. This doubles memory and takes time to copy.
- Let the operating system copy lazily. This is what
fork()with copy-on-write does.
How fork and copy-on-write work
fork() creates a child process that is an exact copy of the parent. The kernel does not copy the memory itself. It copies only the page tables, the maps from virtual addresses to physical 4 KB memory pages, and marks every page read-only and shared. As long as nobody writes, parent and child read the same physical pages. When the parent modifies a page, the CPU traps the write, the kernel copies that single page, and the parent continues on its private copy. The child still sees the original. This is copy-on-write (CoW).
An analogy: two people share one printed manual. Nobody photocopies the whole manual up front. If one of them wants to scribble on page 37, they photocopy page 37 first and scribble on the copy. The other reader keeps the untouched original.
right after fork() parent then runs SET user:1 bob (on page B)
parent ─┐ parent ──▶ A B' C D (B' = private copy, modified)
├──▶ [A] [B] [C] [D] │ │ │
child ─┘ shared, read-only child ──▶ A B C D (still sees the old B)
└ shared ┴───┘
The child serializes its frozen view at leisure. Memory grows only by the pages the parent modifies during the save.
Demonstration of the principle
import os
import time
data = {"balance": 100}
pid = os.fork() # Unix only
if pid == 0: # child: plays the role of the RDB writer
time.sleep(1) # parent modifies the data in the meantime
print("child sees", data["balance"]) # 100: the state at fork time
os._exit(0)
else: # parent: plays the role of the Redis main thread
data["balance"] = 42 # this write triggers a copy of the page
os.waitpid(pid, 0)
print("parent sees", data["balance"]) # 42
The three costs of fork
| Cost | Cause | Symptom | Mitigation |
|---|---|---|---|
| Fork latency | Copying page tables is done synchronously on the main thread, proportional to memory size (roughly 10-20 ms per GB on bare metal and modern VMs; old Xen VMs were an order of magnitude slower) | A latency spike for all clients at every snapshot or rewrite | Smaller instances (shard instead of one huge node), fewer forks, avoid old hypervisors |
| CoW memory | Every page the parent writes during the save is duplicated | RSS grows during the save, up to 2x with heavy writes | Leave memory headroom (chapter 5.3), save during quiet periods, keep transparent huge pages off |
| Fork failure | With vm.overcommit_memory=0, the kernel may refuse to fork a large process when free memory looks insufficient | Can't save in background: fork: Cannot allocate memory, then MISCONF errors | Set vm.overcommit_memory = 1 |
Transparent huge pages (THP)
With THP, the kernel backs memory with 2 MB pages instead of 4 KB pages. A one-byte write then copies 2 MB instead of 4 KB, which multiplies CoW memory and latency. Redis warns about this at startup. Recent versions disable THP for their own process (disable-thp yes, the default), but checking the host setting is still part of a standard deployment checklist.
Measuring the costs
redis-cli INFO stats | grep -E 'latest_fork_usec|total_forks'
# latest_fork_usec:48211 <- the main thread was blocked 48 ms by the last fork
# total_forks:312
redis-cli INFO persistence | grep -E 'cow|fork_perc'
# current_cow_size:0 <- CoW bytes of the save in progress (0 when idle)
# current_fork_perc:0.00 <- progress of the running fork child, in percent
# rdb_last_cow_size:734003200 <- the last snapshot duplicated ~700 MB of pages
# aof_last_cow_size:0
# The log reports the same figure at the end of each save:
# Background saving started by pid 4242
# Fork CoW for RDB: current 3 MB, peak 3 MB, average 2 MB
# RDB: 700 MB of memory used by copy-on-write
Host settings that persistence relies on (Linux)
# Let fork() succeed even when the child could, in theory, double memory use
sudo sysctl vm.overcommit_memory=1
echo 'vm.overcommit_memory = 1' | sudo tee /etc/sysctl.d/99-redis.conf
# Check transparent huge pages: "[never]" or "[madvise]" is fine, "[always]" is not
cat /sys/kernel/mm/transparent_hugepage/enabled
Redis cooperates with CoW in other ways too. It postpones hash table resizing during a fork (chapter 5.2) and avoids touching pages it does not need, so that fewer pages get copied.
Key Takeaways
fork()gives the child a frozen, consistent view of memory without copying it up front.- Only pages the parent modifies during the save are duplicated, so the extra memory depends on the write rate.
- Fork itself blocks the main thread for a time proportional to memory size. Monitor
latest_fork_usec. - Set
vm.overcommit_memory = 1, keep transparent huge pages off, and leave memory headroom for CoW.
Practice
- (Beginner) On a test instance, run
BGSAVEat three dataset sizes (100 MB, 500 MB, 1 GB) and recordlatest_fork_usec. Compute the milliseconds per GB.- (Intermediate) Start a
BGSAVEon a 1 GB dataset while aredis-benchmark -t set -r 1000000 -n 5000000run overwrites random keys. Comparerdb_last_cow_sizewith aBGSAVEtaken while idle, and explain the difference.- (Interview) "Redis was killed by the OOM killer every night at 3 a.m., always during a snapshot.
used_memorywas only 60% of RAM. What happened, and how do you fix it?" Hint: compareused_memorywith what the machine really needs during a save, and think about the write pattern at that hour.
Snapshot Configuration
Taking snapshots by hand is error-prone, so Redis can take them automatically. The rule is not "every N minutes". It is "after N seconds, if at least M keys have changed". An idle server never rewrites an identical file, and a busy server saves more often.
Save points
# save <seconds> <changes> [<seconds> <changes> ...]
# A BGSAVE starts when ANY rule is satisfied:
# at least 1 change and 3600 s since the last save, OR
# at least 100 changes and 300 s, OR
# at least 10000 changes and 60 s
save 3600 1 300 100 60 10000 # the default in Redis 7.0+
# Disable automatic snapshots (manual BGSAVE still works)
# save ""
Redis counts modifications in a "dirty" counter (rdb_changes_since_last_save) and checks the rules in serverCron (chapter 5.1). Under the default rules, a busy server saves at most about once a minute, and a server receiving one write per hour saves once per hour. After a failed save, Redis waits a few seconds before retrying automatically, so a broken disk does not cause a fork storm.
Other RDB directives
| Directive | Default | Purpose |
|---|---|---|
dir | ./ | Working directory for RDB and AOF files. Use an absolute path |
dbfilename | dump.rdb | RDB file name inside dir |
rdbcompression | yes | LZF-compress string values (longer than 20 bytes). Costs CPU in the child, saves disk |
rdbchecksum | yes | Append a CRC64 checksum, verified on load. About 10% slower save and load, but detects corruption |
stop-writes-on-bgsave-error | yes | Reject writes after a failed background save (see below) |
rdb-save-incremental-fsync | yes | fsync every 4 MB while saving, to avoid one huge flush at the end |
rdb-del-sync-files | no | Delete RDB files created only for replication when persistence is off |
stop-writes-on-bgsave-error
If the last background save failed (full disk, permissions, failed fork) and save points are configured, Redis refuses write commands:
127.0.0.1:6379> SET k v
(error) 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.
This is deliberate. It is better to fail loudly than to keep accepting writes that are silently not being persisted. Writes resume automatically after the next successful save. Fix the cause (free disk space, fix permissions on dir), then run BGSAVE. Setting the option to no hides the problem instead of fixing it. That is acceptable only on caches, where you never want writes to stop because of disk trouble.
Changing the configuration at runtime
127.0.0.1:6379> CONFIG SET save "900 1 300 10"
OK
127.0.0.1:6379> CONFIG SET dir /data/redis # must exist and be writable by the redis user
OK
127.0.0.1:6379> CONFIG REWRITE # persist the changes to redis.conf
OK
Choosing save points
| Workload | Suggested save | Reasoning |
|---|---|---|
| Pure cache, rebuildable | "" | No forks, no disk I/O |
| Cache that benefits from a warm restart | 3600 1 | One cheap snapshot per hour |
| General purpose with AOF also enabled | Default or 3600 1 | AOF provides durability, RDB provides backups |
| RDB only, data matters | 300 1 60 1000 | Smaller loss window, but more forks |
| Huge dataset (tens of GB) | Fewer, sparser rules | Each fork is expensive in latency and CoW memory |
Key Takeaways
save <seconds> <changes>rules triggerBGSAVEwhen any rule is met. The default is3600 1 300 100 60 10000.save ""disables automatic snapshots but not manualBGSAVEor replication syncs.- After a failed save,
stop-writes-on-bgsave-error yesturns writes intoMISCONFerrors until a save succeeds. - Keep
rdbchecksum yes. Use an absolutediron a disk with room for at least two copies of the RDB file.
Practice
- (Beginner) Configure
save 10 3on a test instance. Run twoSETs, wait 15 seconds, then a thirdSET, and userdb_changes_since_last_saveandLASTSAVEto explain exactly when the snapshot happened.- (Intermediate) Trigger a
MISCONFerror on purpose on a test server running as a non-root user: make itsdirread-only (chmod 500), runBGSAVE, then try a write. Restore the permissions and get writes accepted again.- (Interview) "Would you set
stop-writes-on-bgsave-error noin production?" Hint: the answer depends on whether the instance is a cache or a system of record, and on how you would otherwise learn that persistence is broken.
RDB File Format
You rarely need to read an RDB file byte by byte. Knowing its structure still explains several things: why it is compact, why it loads so fast, why an old server cannot read a new file, and what the redis-check-rdb tool reports.
Overall layout
┌──────────────────────────────────────────────────────────────────────────┐
│ "REDIS" + 4-digit version e.g. REDIS0011 (written by 7.2) │
├──────────────────────────────────────────────────────────────────────────┤
│ 0xFA AUX fields redis-ver, redis-bits, ctime, used-mem, aof-base, │
│ repl-id, repl-offset ... (metadata, key = value) │
├──────────────────────────────────────────────────────────────────────────┤
│ 0xFE SELECTDB 0 │
│ 0xFB RESIZEDB <number of keys> <number of keys with TTL> │
│ [0xFC <8-byte absolute expire time in ms>] optional │
│ [0xF8 <LRU idle> | 0xF9 <LFU freq>] optional, eviction info │
│ <1-byte value type> <key> <value> │
│ ... one entry per key ... │
│ 0xFE SELECTDB 3 │
│ ... │
├──────────────────────────────────────────────────────────────────────────┤
│ 0xFF EOF │
│ 8-byte CRC64 checksum (all zeros when rdbchecksum is no) │
└──────────────────────────────────────────────────────────────────────────┘
Design choices
- Opcodes are reserved high byte values (the classic ones are
0xF8-0xFF) that mark metadata. Any other byte at that position is a value type: string, list, set, hash, sorted set, stream, module type, and their compact encodings. - Length encoding: the two most significant bits of the first byte say how long the length is.
00means a 6-bit length in the same byte,01a 14-bit length over two bytes, and10a 32- or 64-bit length that follows.11marks a special format: a string stored as an 8-, 16- or 32-bit integer, or an LZF-compressed string. Small lengths, which are the most common, therefore cost a single byte. - Compact encodings are dumped as blobs: a listpack-encoded hash or an intset (chapter 5.2) is written as its raw memory bytes. Loading it is a memory copy, with no per-element parsing.
- Absolute expiration times: TTLs are stored as Unix timestamps in milliseconds, so time spent on disk counts against them. Keys already expired at load time are skipped on a primary.
- Versioning: the version number increases whenever the format changes in a way older readers cannot handle (new data types, new encodings). A newer Redis reads older files, but an older Redis refuses newer ones (
Can't handle RDB format version). Plan upgrades accordingly: you cannot downgrade by copyingdump.rdbback. Redis and Valkey formats diverged after 7.2, so check compatibility before moving files between them.
The same value serialization is used by the DUMP and RESTORE commands and by replication full syncs. DUMP adds a 2-byte RDB version and a CRC64 to a single value.
Example: the first bytes of a real file
$ xxd dump.rdb | head -3
00000000: 5245 4449 5330 3031 31fa 0972 6564 6973 REDIS0011..redis
00000010: 2d76 6572 0537 2e32 2e34 fa0a 7265 6469 -ver.7.2.4..redi
00000020: 732d 6269 7473 c040 fa05 6374 696d 65c2 s-bits.@..ctime.
# 52 45 44 49 53 "REDIS" magic
# 30 30 31 31 "0011" version
# fa 09 "redis-ver" AUX opcode, key of length 9
# 05 "7.2.4" value of length 5
# c0 40 special encoding 11|000000 = 8-bit integer, value 0x40 = 64 (redis-bits)
Example: checking a file with the bundled tool
$ redis-check-rdb /var/lib/redis/dump.rdb
[offset 0] Checking RDB file dump.rdb
[offset 26] AUX FIELD redis-ver = '7.2.4'
[offset 40] AUX FIELD redis-bits = '64'
...
[offset 1048231] Checksum OK
[offset 1048231] \o/ RDB looks OK! \o/
[info] 25000 keys read
[info] 1200 expires
[info] 3 already expired
Example: reading the header and checksum in Python
import os
import struct
import sys
def rdb_info(path: str) -> dict:
with open(path, "rb") as f:
header = f.read(9)
if header[:5] != b"REDIS":
raise ValueError("not an RDB file")
version = int(header[5:9]) # ASCII digits, e.g. b"0011" -> 11
f.seek(-9, os.SEEK_END) # last 9 bytes: EOF opcode + 8-byte checksum
eof, checksum = f.read(1), f.read(8)
if eof != b"\xff":
raise ValueError("missing EOF marker: truncated file?")
(crc,) = struct.unpack("<Q", checksum) # little-endian unsigned 64-bit
return {"version": version, "crc64": hex(crc), "size": os.path.getsize(path)}
print(rdb_info(sys.argv[1]))
# {'version': 11, 'crc64': '0x8f3e0c1a9b2d4e67', 'size': 1048240}
Key Takeaways
- An RDB file is a header with a version, auxiliary metadata, then a stream of opcodes and typed key/value entries per database, an EOF byte and a CRC64.
- Variable-length encodings, integer encoding of strings, LZF compression and raw compact encodings make files small and fast to load.
- TTLs are absolute timestamps in milliseconds. Keys already expired at load time are dropped.
- Newer servers read older RDB versions, but not the reverse.
redis-check-rdbvalidates a file offline.
Practice
- (Beginner) Create a few keys of different types, run
SAVEon a test instance, and look atdump.rdbwithxxd. Find the magic string, theredis-verAUX field, your key names and the final0xFF.- (Intermediate) Save the same dataset with
rdbcompression yesandno, using long repetitive string values ("abc" * 1000). Compare file sizes andrdb_last_bgsave_time_sec, then runredis-check-rdbon both.- (Interview) "Why does Redis store the raw bytes of a listpack in the RDB file instead of a list of elements, and what is the downside?" Hint: compare load time with portability and validation, and look up the
sanitize-dump-payloadoption.
<a id="62-append-only-file"></a>
6.2 Append-Only File
This subchapter covers the append-only file, Redis's log of write commands. It explains how commands reach the file, how the fsync policy trades speed for safety, how rewriting keeps the log from growing forever, how Redis 7.0+ splits the log into several files, and how to recover from a truncated or corrupted log.
AOF Mechanics
RDB loses everything since the last snapshot. To lose (almost) nothing, Redis needs to record every change as it happens. The append-only file (AOF) does exactly that. Every write command that modifies data is appended to a log file. At startup, Redis replays the log from the beginning to rebuild the dataset.
Compare an accountant's balance sheet with their ledger. The balance sheet (RDB) shows the totals at the end of the month. The ledger (AOF) records every single transaction. From the ledger you can always rebuild the totals, including today's.
Unlike the write-ahead log of most databases, the AOF is written after the command executes. A command that fails, such as a wrong type or a syntax error, is never logged, so replaying the log never hits errors that already happened.
From command to disk
client: SET user:1 alice
│
▼
1. execute the command in memory
2. append its RESP encoding to the in-memory aof_buf (only write commands that changed data)
│
▼ end of the event loop iteration (beforeSleep), BEFORE replies are sent
3. write(aof_buf) to the AOF file → data is in the OS page cache
4. fsync() according to appendfsync → data is on the physical disk
5. send the replies to the clients
Step 3 happens before the reply is sent, so if the Redis process crashes after replying, the write is already in the kernel and survives. Whether it survives a power cut or kernel panic depends on step 4 (see "fsync Policies").
What gets logged
- Only commands that changed the dataset. Reads, failed commands and no-op writes, such as
DELof a missing key, are not logged. - Commands are logged in deterministic, absolute form, so replaying them later gives the same result:
EXPIRE k 60becomesPEXPIREAT k <unix-ms>,SET k v EX 60becomesSET k v PXAT <unix-ms>,SPOPbecomesSREMof the popped member, and scripts are logged by their effects (chapter 4.4). MULTI/EXECblocks are logged as a block (chapter 4.2).SELECTis inserted whenever the target database changes.
Enabling the AOF
appendonly yes # default: no
appenddirname "appendonlydir" # Redis 7.0+: directory inside `dir` holding the AOF files
appendfilename "appendonly.aof" # base name for the files in that directory
appendfsync everysec # see the next topic
When appendonly yes, Redis loads data at startup from the AOF only and ignores dump.rdb, because the AOF is assumed to be more complete. This has a dangerous consequence when switching a server from RDB to AOF (see "Backup and Restore Procedures").
Example: watching commands land in the log
mkdir -p /tmp/aof-demo
redis-server --port 6391 --dir /tmp/aof-demo --appendonly yes --save "" --daemonize yes
redis-cli -p 6391 SET user:1 alice
redis-cli -p 6391 INCR visits
redis-cli -p 6391 EXPIRE user:1 60
redis-cli -p 6391 GET user:1 # reads are not logged
cat /tmp/aof-demo/appendonlydir/appendonly.aof.1.incr.aof
*2 ← RESP array with 2 elements: SELECT 0 (inserted automatically)
$6
SELECT
$1
0
*3 ← SET user:1 alice
$3
SET
$6
user:1
$5
alice
*2 ← INCR visits
$4
INCR
$6
visits
*3 ← EXPIRE was rewritten as an absolute PEXPIREAT
$9
PEXPIREAT
$6
user:1
$13
1762770060000
The format is the same RESP protocol that clients send (chapter 1.3), so the file is readable and can be edited with care.
Key Takeaways
- The AOF logs every successful write command in RESP format. Startup replays it to rebuild the dataset.
- Commands are logged after execution and in deterministic form: relative TTLs become absolute timestamps, and scripts are logged by their effects.
- The log is written to the OS before replies are sent. Its durability against power loss depends on the fsync policy.
- With
appendonly yes, startup loads the AOF and ignoresdump.rdb.
Practice
- (Beginner) Run the example above, then add
LPUSH,HSETand a failing command (INCR user:1). Check which ones appear in the.incr.aoffile and explain why.- (Intermediate) Run
SET k v EX 100, wait 60 seconds, then restart the server. UseTTL kto show that the remaining time is about 40 seconds, not 100, and explain how the AOF achieves that.- (Interview) "Most databases write their log before applying a change. Redis writes the AOF after executing the command. What does each approach gain and lose?" Hint: think about failed commands, crash windows between execution and logging, and when the client gets its reply.
fsync Policies
write() and durability are not the same thing. write() copies data into the kernel's page cache and returns immediately. The data survives a crash of the Redis process, but not a power cut or a kernel panic, because the kernel flushes dirty pages to disk only later (on Linux, after about 30 seconds by default). Only fsync() forces the data onto the physical device, and it is slow: from tens of microseconds on enterprise NVMe drives with power-loss protection to several milliseconds on consumer SSDs and network disks.
The appendfsync directive chooses how often Redis pays for fsync():
| Policy | When fsync runs | Lost on Redis crash | Lost on power loss / kernel crash | Throughput impact |
|---|---|---|---|---|
always | Before replying, on the main thread, once per event loop iteration | Nothing | Nothing acknowledged | Large: every iteration waits for the disk |
everysec (default) | Once per second, in the bio_aof background thread | Nearly nothing (a write postponed by a slow disk can be lost) | About 1 second (2 seconds in the worst case) | Small |
no | Never explicitly. The kernel decides | Nothing | Up to ~30 seconds (kernel flush interval) | None |
always and group commit
With always, Redis does not fsync once per command but once per event loop iteration. All the commands executed in that iteration, from all clients, are written and fsynced together before any of them is acknowledged. This group commit keeps always usable on fast disks, but each client still waits for one disk flush per round trip.
everysec and the 2-second rule
The background thread runs fsync() about once per second. If the disk is so slow that the previous fsync is still running when the next write() is due, Redis postpones the write for up to 2 seconds. After that it writes anyway, which can block the main thread on a congested disk, and logs:
Asynchronous AOF fsync is taking too long (disk is busy?). Writing the AOF buffer without
waiting for fsync to complete, this may slow down Redis.
Each occurrence increments aof_delayed_fsync in INFO persistence. A growing value means the disk cannot keep up.
no-appendfsync-on-rewrite
During a BGSAVE or AOF rewrite, the child process writes heavily, and an fsync() in the parent can stall behind it. With no-appendfsync-on-rewrite yes, Redis skips fsync while a child is saving. This avoids latency spikes but makes the policy behave like no during that time (up to ~30 seconds of loss on power failure). The default is no, which favours safety.
Durability per write with WAITAOF
Most data tolerates everysec, but some writes, like a payment or an order, need a stronger guarantee. WAITAOF numlocal numreplicas timeout (Redis 7.2+) blocks the calling client until all of its previous writes have been fsynced to the AOF on the local server (numlocal, 0 or 1) and on at least numreplicas replicas, or until timeout milliseconds have passed (0 = wait forever). It returns the number of local servers and replicas that confirmed. This gives always-level guarantees only for the writes that need them.
import redis
r = redis.Redis(decode_responses=True)
r.hset("order:1001", mapping={"status": "paid", "amount": "49.90"})
# Wait until this write is fsynced locally and on at least 1 replica, at most 500 ms
local, replicas = r.execute_command("WAITAOF", 1, 1, 500)
if local < 1 or replicas < 1:
# The write is in memory (and maybe on disk) but not confirmed durable as requested.
# Decide: retry, alert, or tell the user the order is "pending".
raise RuntimeError(f"durability not confirmed: local={local}, replicas={replicas}")
WAITAOF with numlocal = 1 returns an error if the AOF is disabled on the server.
Configuration
appendfsync everysec # always | everysec | no
no-appendfsync-on-rewrite no # yes: skip fsync while a BGSAVE or rewrite is running
# Measure how expensive fsync is on this disk before choosing "always"
redis-benchmark -p 6391 -t set -n 100000 -q # with appendfsync everysec
redis-cli -p 6391 CONFIG SET appendfsync always
redis-benchmark -p 6391 -t set -n 100000 -q # compare requests per second
redis-cli -p 6391 INFO persistence | grep aof_delayed_fsync
Key Takeaways
write()protects against a Redis crash. Onlyfsync()protects against power loss and kernel crashes.everysec(the default) risks about 1-2 seconds of writes on power loss with little overhead.alwaysrisks none but is limited by disk flush latency.noleaves flushing to the kernel.- A growing
aof_delayed_fsyncmeans the disk is too slow for the write load. WAITAOFmakes selected writes durable without paying foralwayson every write.
Practice
- (Beginner) Run the
redis-benchmarkcomparison above witheverysec,alwaysandnoon your machine and put the results in a table.- (Intermediate) Modify the
WAITAOFexample to callWAITAOFagain (up to two retries) before failing. Explain why calling it again is safe, and what the application should tell the user if durability is still not confirmed.- (Interview) "With
appendfsync everysec, how much data can you lose, and in which failure scenarios?" Hint: separate a crash of the Redis process from a crash of the machine, and remember the 2-second rule.
AOF Rewriting
A log that only grows becomes a problem. A counter incremented a million times leaves a million INCR entries for one key whose current value fits in one SET. The file wastes disk space, and every restart replays a million commands to rebuild one number. AOF rewriting solves this by replacing the log with the shortest one that produces the current dataset.
The rewrite never reads the old file. It walks the current in-memory dataset and writes it out from scratch. It is therefore always correct, however messy the old log was.
before the rewrite (old log) after the rewrite (new base)
───────────────────────────── ─────────────────────────────
SET stock 100 SET stock 97 (or one RDB entry)
DECR stock RPUSH queue c
DECR stock (session:1 deleted: nothing written)
DECR stock
RPUSH queue a b c
LPOP queue
LPOP queue
SET session:1 x
DEL session:1
With aof-use-rdb-preamble yes (the default), the new base is written in RDB format instead of commands, which is even smaller and faster to load (see "Hybrid Persistence").
How the rewrite runs (Redis 7.0+)
parent (main thread) child (forked)
──────────────────── ───────────────
1. open a NEW incremental file (incr #N+1),
keep appending new writes there
2. fork() ────────────────────────────────────────▶ write the frozen dataset to a temp
file, then fsync it
keep serving clients and writing to incr #N+1 exit
3. child finished: rename the temp file to base #M+1
4. write a new manifest {base #M+1, incr #N+1}
and swap it in atomically
5. mark the old base and old incr files as history and delete them
New writes go to a new incremental file from the moment the rewrite starts, so nothing needs to be buffered in memory or merged afterwards. Before 7.0, the parent kept a "rewrite buffer" of all writes made during the rewrite, sent it to the child through a pipe, and appended the remainder at the end. That cost extra memory and wrote every change twice.
If a rewrite fails (disk full, fork failure), Redis retries at increasing intervals so failed attempts do not pile up incremental files. aof_last_bgrewrite_status and aof_rewrites_consecutive_failures in INFO persistence show the state.
Triggering a rewrite
| Trigger | How |
|---|---|
| Manually | BGREWRITEAOF (if a BGSAVE is running, the rewrite is scheduled after it) |
| Automatically | When the AOF has grown by auto-aof-rewrite-percentage since the last rewrite and is larger than auto-aof-rewrite-min-size |
| Enabling the AOF at runtime | CONFIG SET appendonly yes runs a rewrite to create the initial files from the current data |
auto-aof-rewrite-percentage 100 # rewrite when the AOF is 2x its size after the last rewrite (0 disables)
auto-aof-rewrite-min-size 64mb # ...but never for an AOF smaller than this
aof-rewrite-incremental-fsync yes # fsync every 4 MB written by the child to avoid big flush spikes
The automatic condition, using the INFO persistence fields:
aof_current_size > auto-aof-rewrite-min-size
AND (aof_current_size - aof_base_size) / aof_base_size * 100 >= auto-aof-rewrite-percentage
Example: shrinking a log
import redis
r = redis.Redis(port=6391, decode_responses=True) # the AOF demo server from "AOF Mechanics"
pipe = r.pipeline(transaction=False)
for _ in range(100_000):
pipe.incr("page:views") # 100,000 log entries for one key
pipe.execute()
print("before:", r.info("persistence")["aof_current_size"]) # several MB of INCR commands
r.bgrewriteaof()
# ... wait until info("persistence")["aof_rewrite_in_progress"] == 0 ...
print("after:", r.info("persistence")["aof_current_size"]) # around a hundred bytes
Key Takeaways
- A rewrite produces a minimal log from the current in-memory data. It never reads the old log.
- It runs in a forked child, so it has the same fork latency and copy-on-write costs as
BGSAVE. - Since 7.0, new writes go to a fresh incremental file during the rewrite. No rewrite buffer is needed.
- Automatic rewrites are controlled by
auto-aof-rewrite-percentageandauto-aof-rewrite-min-size.
Practice
- (Beginner) Run the shrinking example and list the files in
appendonlydirbefore and afterBGREWRITEAOF. Note which sequence numbers changed.- (Intermediate) With
auto-aof-rewrite-min-size 1mbandauto-aof-rewrite-percentage 50, compute when the next automatic rewrite will happen from the currentaof_base_size, then write data until it triggers and check your prediction.- (Interview) "If the rewrite does not read the old AOF, how can it be sure that writes made during the rewrite are not lost?" Hint: follow what happens to a write that arrives one millisecond after
fork(), for the multi-part design and for the older rewrite-buffer design.
Multi-Part AOF
Before Redis 7.0, the AOF was one file that was rewritten and swapped as a whole. Since 7.0, the AOF is a directory of files described by a manifest. This change made rewrites simpler and cheaper: there is no rewrite buffer and no double writing. It also means that the AOF is now a set of files that must be handled together.
Directory layout
/var/lib/redis/ ← dir
├── dump.rdb ← RDB snapshot (independent of the AOF)
└── appendonlydir/ ← appenddirname
├── appendonly.aof.3.base.rdb ← base: full dataset at the last rewrite (RDB preamble)
├── appendonly.aof.5.incr.aof ← incremental: writes since then, in RESP
├── appendonly.aof.6.incr.aof ← a second incr file can exist (e.g. after a failed rewrite)
└── appendonly.aof.manifest ← the list of files above, in load order
| File type | Name pattern | Content | Count |
|---|---|---|---|
| Base | <appendfilename>.<seq>.base.rdb or .base.aof | Snapshot of the data at the last rewrite (RDB format if aof-use-rdb-preamble yes) | 0 or 1 |
| Incremental | <appendfilename>.<seq>.incr.aof | Write commands since the base, in RESP | 1 or more |
| History | (same names, type h in the manifest) | Files replaced by a rewrite, waiting to be deleted | Temporary |
| Manifest | <appendfilename>.manifest | One line per file: name, sequence, type | 1 |
The manifest
$ cat /var/lib/redis/appendonlydir/appendonly.aof.manifest
file appendonly.aof.3.base.rdb seq 3 type b
file appendonly.aof.5.incr.aof seq 5 type i
file appendonly.aof.6.incr.aof seq 6 type i
At startup Redis reads the manifest, loads the base, then replays the incremental files in sequence order. The manifest is the source of truth. A file in the directory that is not listed in the manifest is ignored, and a listed file that is missing prevents startup. The manifest itself is replaced atomically (write a temp file, then rename), so a crash during a rewrite leaves either the old or the new set of files, never a mix.
Timeline of a rewrite in terms of files
before rewrite during rewrite after rewrite
base 3 + incr 5 → base 3 + incr 5 + incr 6 (new) → base 4 + incr 6
child writes temp base base 3, incr 5 → history → deleted
Timestamp annotations
With aof-timestamp-enabled yes (default no), Redis adds lines like #TS:1762770000 to the incremental files, about once per second while writes are flowing. redis-check-aof --truncate-to-timestamp <unix-ts> can then cut the log back to a point in time (see "AOF Corruption Recovery"). Lines starting with # are annotations, not commands. Older Redis versions cannot read them.
Upgrading from a single-file AOF
If Redis 7.0+ finds an old-style appendonly.aof in dir and no appenddirname directory, it moves the file into a new directory, registers it as the base in a new manifest, and continues. The upgrade is one-way: once the directory exists, an older Redis version cannot use it.
Rules for operators
- Treat
appendonlydiras one unit. Copy, move and back up the whole directory, never single files. - Never delete an "old-looking" incr file by hand. If it is listed in the manifest, the server will not start without it.
- Edit only the last incremental file, and only to remove commands at its end (see the recovery topic).
Example: inspecting the AOF state
ls -l /var/lib/redis/appendonlydir/
cat /var/lib/redis/appendonlydir/appendonly.aof.manifest
redis-cli INFO persistence | grep -E '^aof_'
# aof_enabled:1
# aof_rewrite_in_progress:0
# aof_rewrite_scheduled:0
# aof_last_rewrite_time_sec:2
# aof_last_bgrewrite_status:ok
# aof_rewrites:14
# aof_last_write_status:ok
# aof_current_size:1847291 <- base + all incr files
# aof_base_size:1203411 <- size right after the last rewrite (used by auto-rewrite)
# aof_buffer_length:0
# aof_pending_bio_fsync:0
# aof_delayed_fsync:0
Key Takeaways
- Since 7.0, the AOF is a directory with one base file, one or more incremental files and a manifest.
- The manifest defines what is loaded and in which order, and it is swapped atomically.
- Rewrites start a new incremental file and produce a new base. Old files become history and are deleted.
- Back up, copy and restore the whole directory. Never delete or reorder files by hand.
Practice
- (Beginner) Enable the AOF on a test server, run a few writes and two
BGREWRITEAOFs, and after each step record the directory listing and the manifest.- (Intermediate) Stop the test server, rename one incr file listed in the manifest, and start the server again. Read the error in the log, then undo the change. Repeat by adding an unlisted file to the directory and observe that it is ignored.
- (Interview) "What problems of the pre-7.0 AOF rewrite does the multi-part design solve, and what new operational risks does it introduce?" Hint: consider memory, disk writes, CPU spent on the pipe, and what a backup script that copies only
appendonly.aofwould now miss.
AOF Corruption Recovery
A log written continuously can be damaged in two ways. Most often the end of the last file is incomplete, because the machine lost power or the disk filled up in the middle of a write(). More rarely, bytes are damaged in the middle of a file by a disk fault, a faulty copy or a bad manual edit. Redis treats the two cases very differently.
Truncated tail
With aof-load-truncated yes (the default), Redis loads everything up to the last complete command, discards the incomplete fragment, truncates the file, and starts. It logs:
# !!! Warning: short read while loading the AOF file appendonly.aof.6.incr.aof!!!
# !!! Truncating the AOF appendonly.aof.6.incr.aof at offset 1847201 !!!
# AOF appendonly.aof.6.incr.aof loaded anyway because aof-load-truncated is enabled
The lost fragment was never acknowledged as durable (it was being written when the crash happened), so in practice this costs nothing. With aof-load-truncated no, Redis refuses to start, and you must fix the file with redis-check-aof --fix. In the multi-part layout, this tolerance applies only to the last file in the manifest. A truncated base or earlier incr file means something else went wrong, and the server stops.
Corruption in the middle
If an invalid sequence appears before the end, Redis refuses to start:
Bad file format reading the append only file appendonly.aof.6.incr.aof:
make a backup of your AOF file, then use ./redis-check-aof --fix <filename.manifest>
--fix truncates the file at the first invalid byte and drops everything after it. If the corruption is near the beginning, that can be almost all the data. Follow this order:
- Stop and copy. Copy the whole
appendonlydirsomewhere safe before changing anything. - Analyze without fixing.
redis-check-aofwithout--fixreports where the damage starts. - Inspect the region. The AOF is text (RESP), so open it at the reported offset (
dd,xxd,less). A few damaged bytes can often be repaired by hand, for example a wrong$length. - Fix as a last resort. Use
--fixonly when manual repair is impossible, and compare the result (diff, key count) with the backup. - Consider other sources. A replica or the latest RDB backup may hold more data than a heavily truncated AOF.
# Stop the server, then:
cp -a /var/lib/redis/appendonlydir /var/backups/appendonlydir.$(date +%s)
redis-check-aof /var/lib/redis/appendonlydir/appendonly.aof.manifest # report only
# Start checking Multi Part AOF
# ...
# AOF analyzed: filename=appendonly.aof.6.incr.aof, size=1847291, ok_up_to=1203554,
# ok_up_to_line=48211, diff=643737
# AOF appendonly.aof.6.incr.aof is not valid. Use the --fix option to try fixing it.
# diff=643737 means "fixing" would discard ~630 KB of commands: inspect first
dd if=/var/lib/redis/appendonlydir/appendonly.aof.6.incr.aof bs=1 skip=1203500 count=200 | xxd
redis-check-aof --fix /var/lib/redis/appendonlydir/appendonly.aof.manifest # last resort, asks for confirmation
Recovering from a human mistake: an accidental FLUSHALL
The AOF is also a time machine. Suppose someone runs FLUSHALL on production. If no rewrite has happened since, all data is still in the log, followed by a FLUSHALL entry.
# 1. Prevent a rewrite, which would replace the log with the (now empty) dataset
redis-cli CONFIG SET auto-aof-rewrite-percentage 0
# 2. Stop without saving (FLUSHALL has already overwritten dump.rdb if save points exist)
redis-cli SHUTDOWN NOSAVE
# 3. Back up the directory, then remove the trailing FLUSHALL from the LAST incr file
cp -a /var/lib/redis/appendonlydir /var/backups/before-flushall-fix
tail -c 200 /var/lib/redis/appendonlydir/appendonly.aof.6.incr.aof | cat -A
# the end shows three lines: *1^M$ $8^M$ FLUSHALL^M$
# delete them (and anything after them) with an editor, or truncate the file just before them
# 4. Restart and verify
systemctl start redis && redis-cli DBSIZE
With timestamp annotations enabled, step 3 becomes redis-check-aof --truncate-to-timestamp <unix-ts-before-the-mistake> <manifest>.
Configuration
aof-load-truncated yes # start with a truncated last file (recommended)
aof-timestamp-enabled no # yes: add #TS annotations for point-in-time truncation
Key Takeaways
- A truncated tail is normal after a crash.
aof-load-truncated yesloads everything complete and continues. - Corruption in the middle stops the server. Back up, analyze with
redis-check-aof, inspect, and use--fixonly as a last resort because it drops everything after the damage. - Pass the manifest to
redis-check-aoffor multi-part AOFs. - Until the next rewrite, the AOF can undo a mistake such as
FLUSHALL. Disable rewrites immediately and edit the end of the last incr file.
Practice
- (Beginner) On a test server with the AOF enabled, write some keys, stop the server, cut the last 5 bytes of the last incr file with
truncate -s -5, and restart. Read the log and check which key was lost.- (Intermediate) Corrupt one byte in the middle of an incr file (change a
$5into$9). Runredis-check-aofwithout--fix, find the problem at the reported offset, repair it by hand, and start the server without losing data.- (Interview) "A developer ran
FLUSHALLon production ten minutes ago. AOF is enabled witheverysec. What do you do in the next 60 seconds, and what could make recovery impossible?" Hint: think about what automatic processes might replace the log or the RDB file, and about replicas that already applied theFLUSHALL.
<a id="63-persistence-strategies"></a>
6.3 Persistence Strategies
This subchapter turns the mechanisms into decisions. It compares RDB and AOF, explains the hybrid mode that is the recommended default for durable data, covers running Redis as a pure in-memory cache, and gives procedures for backups and restores.
RDB vs AOF Trade-offs
RDB and AOF answer different questions. RDB asks "how do I get a compact copy of everything?" AOF asks "how do I lose as little as possible?" Each one is strong where the other is weak.
| Aspect | RDB snapshots | AOF (everysec) |
|---|---|---|
| Data lost on crash | Everything since the last snapshot (minutes) | About 1 second (none with always) |
| Restart speed | Fast: load a binary image | Slower: replay commands (much faster with an RDB preamble) |
| File size | Compact, compressed | Larger, grows until rewritten |
| Steady-state disk I/O | None between snapshots, then a large burst | Continuous small appends plus fsync every second |
| Forks | One per snapshot | One per rewrite |
| Main-thread impact | Fork latency only | Fork latency plus occasional fsync stalls on slow disks |
| Backup friendliness | Excellent: one self-contained, atomically replaced file | Good: a directory that must be copied consistently |
| Human readability | Binary | RESP text, can be edited to undo mistakes |
Undo a FLUSHALL | Only from an older backup | Yes, until the next rewrite |
| Used for replication | Yes, always (full sync transfers an RDB) | No |
Replication always uses RDB for full synchronization (chapter 7.1), even when save "" is set. With diskless sync (repl-diskless-sync yes, the default since 7.0), the snapshot is streamed to the replica over the socket without touching the disk.
Choosing
Can the data be rebuilt from another source in acceptable time?
├── yes ── Is a warm restart worth a fork once in a while?
│ ├── no → no persistence (cache) 6.3 "Running Without Persistence"
│ └── yes → RDB only, sparse save points
└── no ─── Can you lose a few minutes of writes?
├── yes → RDB only, frequent save points (and replicas)
└── no → AOF everysec + RDB preamble (+ save points for backups) 6.3 "Hybrid Persistence"
└── specific writes must never be lost → add WAITAOF, or appendfsync always
Persistence protects against restarts and crashes of a single machine. It does not protect against losing the disk, the machine or the data center. That needs replicas (chapter 7) and off-host backups (see "Backup and Restore Procedures").
Example: comparing restart times on your own data
# Same dataset, three configurations; measure the "DB loaded from ..." line in the log
mkdir -p /tmp/cmp
redis-server --port 6392 --dir /tmp/cmp --appendonly no --daemonize yes --logfile /tmp/cmp/rdb.log \
--enable-debug-command local # DEBUG is disabled by default; test servers only
redis-cli -p 6392 DEBUG POPULATE 2000000 key 100 # 2 million keys with 100-byte values
redis-cli -p 6392 SAVE && redis-cli -p 6392 SHUTDOWN NOSAVE
redis-server --port 6392 --dir /tmp/cmp --appendonly no --daemonize yes --logfile /tmp/cmp/rdb.log
grep 'DB loaded from disk' /tmp/cmp/rdb.log # e.g. "DB loaded from disk: 1.214 seconds"
# Repeat with --appendonly yes --aof-use-rdb-preamble no, then yes, and compare
Key Takeaways
- RDB is compact, fast to load and ideal for backups, but loses minutes of data. AOF loses about one second but is larger and slower to replay.
- Both fork. AOF also adds continuous disk writes and fsync.
- Replication always uses RDB for full syncs, regardless of the persistence settings.
- Persistence is not a backup. It protects against restarts, not against losing the machine or a bad command that has already been rewritten into the log.
Practice
- (Beginner) For each of these systems, pick a persistence setup and justify it in one sentence: a session cache in front of a SQL database, a leaderboard rebuilt nightly from logs, a rate limiter, a queue of payment jobs.
- (Intermediate) Run the restart-time comparison for RDB, pure AOF (
aof-use-rdb-preamble no) and hybrid AOF on a 2-million-key dataset, and chart the results.- (Interview) "If AOF loses less data, why not always use AOF with
appendfsync always?" Hint: estimate requests per second when each event loop iteration waits for a 1 ms fsync, and consider what replicas already give you.
Hybrid Persistence
RDB and AOF complement each other. RDB loads quickly, and the AOF loses little. Since Redis 4.0, the AOF can combine the two: a rewrite writes the current dataset as an RDB image (the "preamble") and appends new writes after it as commands. Since Redis 5.0, this is the default (aof-use-rdb-preamble yes). In the 7.0+ multi-part layout, the RDB image is the .base.rdb file and the commands are the .incr.aof files.
appendonlydir/
├── appendonly.aof.4.base.rdb ██████████████████ dataset at the last rewrite, binary, compact, fast to load
└── appendonly.aof.6.incr.aof ▒▒▒▒ writes since then, RESP commands, ~1 s loss window
load order ─────▶
At startup, Redis loads the base like an RDB file, which is fast, then replays the short incremental tail. The log shows both steps:
* DB loaded from base file appendonly.aof.4.base.rdb: 1.302 seconds
* DB loaded from incr file appendonly.aof.6.incr.aof: 0.041 seconds
* DB loaded from append only file: 1.344 seconds
Hybrid AOF vs "both enabled"
These are two different things that are often confused:
- Hybrid AOF is one log whose base happens to be in RDB format. It is what Redis loads at startup when
appendonly yes. - RDB save points plus AOF means two independent mechanisms:
dump.rdbis still produced by thesaverules, but it is not used at startup while the AOF is enabled. It is still valuable as a self-contained file for backups, for seeding new servers and for analysis tools.
Redis never runs a BGSAVE and an AOF rewrite at the same time. Whichever comes second waits until the first finishes. With both enabled, expect more forks overall and plan memory headroom for copy-on-write accordingly.
A recommended configuration for durable data
dir /var/lib/redis
# AOF: main durability mechanism
appendonly yes
appenddirname "appendonlydir"
appendfsync everysec # ~1 s of loss on power failure; use WAITAOF for critical writes
aof-use-rdb-preamble yes # hybrid: RDB base + RESP incremental files (default)
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
# RDB: periodic self-contained snapshots for backups (not loaded while AOF is on)
save 3600 1
dbfilename dump.rdb
rdbchecksum yes
stop-writes-on-bgsave-error yes
Key Takeaways
- Hybrid persistence stores a compact RDB base plus RESP incremental files: RDB-speed restarts with AOF-level loss.
- It is the default (
aof-use-rdb-preamble yes) and the recommended setup for data you cannot lose. - With
appendonly yes, startup always loads the AOF, anddump.rdbis used only for backups and seeding. BGSAVEand AOF rewrites never overlap, but enabling both increases the total number of forks.
Practice
- (Beginner) Enable the AOF with the default preamble, run
BGREWRITEAOF, and confirm withhead -c 9that the base file starts withREDIS.- (Intermediate) On a 1-million-key dataset, run
BGREWRITEAOFwithaof-use-rdb-preamble noand withyes. Compare base file sizes, rewrite durations (aof_last_rewrite_time_sec) and startup times.- (Interview) "With AOF enabled, someone deletes
dump.rdbto save disk space. What breaks?" Hint: separate what the startup path needs from what backups, new replicas and analysis tools need.
Running Without Persistence
Not every Redis needs a disk. When Redis is a cache whose data can be rebuilt from a database, persistence brings costs (forks, copy-on-write memory, disk I/O) and very little benefit. Turning it off is a legitimate choice, but it changes what a restart means. The server comes back empty, and every safety mechanism that assumed data on disk now behaves differently.
save "" # no automatic RDB snapshots
appendonly no # no AOF
# The same on the command line or in a container
redis-server --save "" --appendonly no
docker run -d --name cache -p 127.0.0.1:6379:6379 redis:8 redis-server --save "" --appendonly no
The official container image runs redis-server with the built-in defaults, so without these flags it still takes RDB snapshots into /data.
What you gain
- No fork latency spikes and no copy-on-write memory, so
maxmemorycan be set closer to the machine's RAM (chapter 5.3). - No disk I/O, so no fsync stalls, and the server can run on machines with small or slow disks.
- A simpler setup with nothing to back up.
What you must handle
- Cold start. After a restart, every request is a cache miss and goes to the backing database. A large cache refilling at once can overload that database (a cache stampede, chapter 8.1). Plan for gradual warm-up or request coalescing.
- A stale
dump.rdbis still loaded. Disabling save points does not delete an existing file. If an olddump.rdbis left indir, the next restart silently loads weeks-old data. Remove it when you switch a server to non-persistent mode. - Replication still creates RDB data. Full syncs to replicas fork and produce an RDB (streamed with diskless sync, or written to disk otherwise).
rdb-del-sync-files yesdeletes such files when persistence is off.
The dangerous combination: a primary without persistence that restarts automatically
1. primary (no persistence) and replica both hold 10 GB of data
2. primary crashes; systemd/Kubernetes restarts it within seconds
3. primary comes back EMPTY, with a new replication ID
4. replica reconnects, full sync: it receives an empty dataset
5. replica deletes its 10 GB to match the primary → all copies are gone
Replication copies whatever the primary has, including "nothing". If a primary runs without persistence, either disable automatic restarts for it, so that Sentinel (chapter 7.2) promotes a replica instead, or enable persistence on it. The same reasoning applies to Sentinel setups: a primary that restarts empty faster than Sentinel can detect the failure wipes its replicas.
Key Takeaways
save ""plusappendonly nogives a pure in-memory server with no forks and no disk I/O, suitable for rebuildable caches.- A restart means an empty dataset. Plan for cache warm-up and protect the backing database.
- Delete stale
dump.rdbfiles, and remember that replication still creates RDB data. - Never let a non-persistent primary with replicas restart automatically. It comes back empty and the replicas copy the empty dataset.
Practice
- (Beginner) Start a container with persistence disabled, write keys, restart the container, and confirm that
DBSIZEis 0. Repeat without the flags and explain the difference.- (Intermediate) Reproduce the "empty primary wipes replica" scenario with two local servers (
REPLICAOF), a primary with--save "" --appendonly no, and a manual restart of the primary. Then show that starting the primary with persistence prevents the loss.- (Interview) "Our Redis is only a cache, so we disabled persistence. After a deploy that restarted all nodes, our PostgreSQL went down. Why, and how would you design the restart differently?" Hint: think about the hit ratio right after a restart, request coalescing, and rolling restarts.
Backup and Restore Procedures
Persistence keeps data across restarts of one machine. A backup is a copy stored elsewhere that lets you go back to an earlier point after a disk failure, a lost machine, a bad deploy or a destructive command. A backup you have never restored is only a hope, so a backup procedure always includes restore tests.
What to back up
| Setup | Back up | Why it is safe to copy |
|---|---|---|
| RDB snapshots | dump.rdb | Written to a temp file and renamed atomically: a copy is always a complete snapshot |
| AOF 7.0+ | The whole appendonlydir | Files are only appended or replaced. Avoid copying during a rewrite (procedure below) |
| AOF before 7.0 | appendonly.aof | A copy may lack the final part, which Redis loads anyway |
| Any | redis.conf, ACL file (chapter 11) | A restore also needs the configuration and the users |
Scheduled RDB backups
The standard approach: trigger or reuse a snapshot, copy it with a timestamp, verify it, keep hourly copies for two days and daily copies for one or two months, and copy at least one per day off the machine, encrypted if it contains personal data.
#!/usr/bin/env bash
# /usr/local/bin/redis-backup.sh - run hourly from cron: 5 * * * * redis /usr/local/bin/redis-backup.sh
set -euo pipefail
DATA_DIR=/var/lib/redis
DEST=/var/backups/redis
STAMP=$(date +%Y%m%d-%H%M)
mkdir -p "$DEST/hourly" "$DEST/daily"
# 1. Take a fresh snapshot and wait for it (max 10 minutes)
before=$(redis-cli LASTSAVE)
redis-cli BGSAVE SCHEDULE >/dev/null || true # a save already running is fine: we wait for it
for _ in $(seq 600); do
[ "$(redis-cli LASTSAVE)" != "$before" ] && break
sleep 1
done
[ "$(redis-cli LASTSAVE)" != "$before" ] || { echo "BGSAVE timed out or failed" >&2; exit 1; }
redis-cli INFO persistence | grep -q '^rdb_last_bgsave_status:ok' \
|| { echo "BGSAVE failed" >&2; exit 1; }
# 2. Copy and verify (the copy, not the original, is what you will restore)
cp "$DATA_DIR/dump.rdb" "$DEST/hourly/dump-$STAMP.rdb"
redis-check-rdb "$DEST/hourly/dump-$STAMP.rdb" >/dev/null
sha256sum "$DEST/hourly/dump-$STAMP.rdb" > "$DEST/hourly/dump-$STAMP.rdb.sha256"
# 3. Once a day: keep a daily copy and ship it off the host, encrypted
if [ "$(date +%H)" = "03" ]; then
cp "$DEST/hourly/dump-$STAMP.rdb" "$DEST/daily/"
gpg --batch --yes --symmetric --passphrase-file /etc/redis/backup.pass \
-o "$DEST/daily/dump-$STAMP.rdb.gpg" "$DEST/daily/dump-$STAMP.rdb"
aws s3 cp "$DEST/daily/dump-$STAMP.rdb.gpg" "s3://acme-redis-backups/$(hostname)/"
fi
# 4. Retention: 48 hours of hourly copies, 60 days of daily copies
find "$DEST/hourly" -type f -mmin +2880 -delete
find "$DEST/daily" -type f -mtime +60 -delete
Back up from a replica when possible. The fork and the disk I/O then happen on a machine that is not serving the main write traffic. Alert separately when backups stop arriving, because a backup job that fails silently looks exactly like one that works.
Copying the AOF directory safely (7.0+)
A rewrite replaces files in appendonlydir, so a copy taken during a rewrite may mix old and new files. Pause automatic rewrites for the duration of the copy:
#!/usr/bin/env bash
set -euo pipefail
AOF_DIR=/var/lib/redis/appendonlydir
SNAP=/var/lib/redis/aof-snapshot-$(date +%Y%m%d-%H%M) # same filesystem, required for hard links
old_pct=$(redis-cli CONFIG GET auto-aof-rewrite-percentage | tail -1)
redis-cli CONFIG SET auto-aof-rewrite-percentage 0 >/dev/null # no new automatic rewrites
trap 'redis-cli CONFIG SET auto-aof-rewrite-percentage "$old_pct" >/dev/null' EXIT
# Wait for a rewrite that may already be running or scheduled
while redis-cli INFO persistence | grep -qE '^aof_rewrite_(in_progress|scheduled):1'; do sleep 1; done
# Hard links are instant, so rewrites can be re-enabled right away
mkdir "$SNAP"
ln "$AOF_DIR"/* "$SNAP"/
redis-cli CONFIG SET auto-aof-rewrite-percentage "$old_pct" >/dev/null
# Archive the links at leisure (the last incr file may still grow: tar can warn, Redis loads it anyway)
tar -czf "/var/backups/redis/aof-$(date +%Y%m%d-%H%M).tar.gz" -C "$SNAP" . || [ $? -eq 1 ]
rm -rf "$SNAP"
This works because Redis only appends to these files or replaces them as a whole, so a hard link keeps pointing to a valid file. No one should run BGREWRITEAOF by hand while the links are being created.
Restoring
The startup rule decides the procedure: with appendonly yes, Redis loads the AOF and ignores dump.rdb.
Restore from an RDB file:
systemctl stop redis # may save current data on exit; moved aside below
mv /var/lib/redis/dump.rdb /var/lib/redis/dump.rdb.old 2>/dev/null || true
mv /var/lib/redis/appendonlydir /var/lib/redis/appendonlydir.old 2>/dev/null || true # or it wins
cp /var/backups/redis/hourly/dump-20261110-0905.rdb /var/lib/redis/dump.rdb
chown redis:redis /var/lib/redis/dump.rdb # wrong ownership: Redis cannot read the file
# Start with the AOF off so the RDB file is loaded...
sed -i 's/^appendonly yes/appendonly no/' /etc/redis/redis.conf
systemctl start redis
redis-cli DBSIZE # compare with the expected key count
# ...then turn the AOF back on ONLINE: Redis writes a new AOF from the loaded data
redis-cli CONFIG SET appendonly yes
# wait until INFO persistence shows aof_rewrite_in_progress:0 and aof_last_bgrewrite_status:ok
redis-cli CONFIG REWRITE # persist appendonly yes to redis.conf
Restore from an AOF backup: stop the server, replace appendonlydir with the backed-up directory (all files plus the manifest), fix ownership, start, and verify.
Switching an existing server from RDB to AOF
The same rule creates the most famous persistence trap. If you only edit redis.conf to say appendonly yes and restart, Redis looks for an AOF, finds none, and starts with an empty dataset. The Redis documentation warns that skipping the correct procedure can result in data loss. The correct procedure is online:
- Back up
dump.rdb. redis-cli CONFIG SET appendonly yeson the running server. Redis writes the initial AOF from memory.- Optionally
CONFIG SET save ""to stop RDB snapshots. - Wait until
aof_rewrite_in_progress:0,aof_rewrite_scheduled:0andaof_last_bgrewrite_status:ok. CONFIG REWRITE(or editredis.confto match), so the next restart keeps the AOF enabled.- After the next restart, check that the key count matches.
Other backup options
- Managed services (ElastiCache, Azure Cache, Redis Cloud and others) offer scheduled snapshots and point-in-time restore. Learn their restore times before you need them.
- Starting with Redis 8.10, a
BACKUPcommand family (BACKUP START,STATUS,SEAL,LIST,CLEANUP,ABORT) produces a consistent base RDB plus incremental AOF set without pausing writes. On older versions, use the procedures above.
Key Takeaways
dump.rdbcan be copied at any time.appendonlydirmust be copied as a whole, with rewrites paused or via hard links.- Automate snapshots with timestamps, retention and off-host encrypted copies. Verify each file and alert when backups stop.
- With
appendonly yes, Redis loads only the AOF. To restore from RDB, start with the AOF off, then enable it online. - Switch from RDB to AOF with
CONFIG SET appendonly yeson the live server, never by editing the config and restarting. - Test restores regularly. An untested backup is not a backup.
Practice
- (Beginner) Run the RDB backup script against a test server every 5 minutes for an hour (adjust the retention values), then restore the oldest copy into a second server on another port and compare
DBSIZE.- (Intermediate) On a test server with RDB data, reproduce the trap: set
appendonly yesin the config file and restart. Observe the empty dataset, recover it from your backup, then perform the switch correctly withCONFIG SET appendonly yes.- (Interview) "Design a backup and restore strategy for a 50 GB Redis primary with two replicas that stores user carts. State your RPO and RTO, where backups run, and how you prove they work." Hint: consider which node takes the snapshots, fork costs at 50 GB, off-site copies, encryption, alerting, and a scheduled restore drill.
<a id="7-replication-high-availability-and-scaling"></a>