What is Redis persistence and how do RDB snapshots work?
Redis keeps its entire dataset in memory, so a restart, crash, or host failure erases everything unless the data was also written to disk. Persistence is the mechanism that writes it out and loads it back on startup. Teams turn it on when rebuilding the dataset from a primary database would be slow, expensive, or impossible. Examples include session stores, queues, rate-limit counters, and any case where Redis is the system of record.
How RDB snapshots work
RDB writes a point-in-time snapshot of the whole dataset to a single binary file (dump.rdb by default). Snapshots are triggered in two ways:
- Save rules such as
save 900 1, which means "snapshot if at least 1 key changed in the last 900 seconds." - An explicit
BGSAVEcommand.
On BGSAVE, Redis forks a child process. The child sees a frozen view of memory through copy-on-write and serializes it to a temporary file, then atomically renames that file over the old one. The parent keeps serving traffic. A failed or half-written snapshot never replaces the last good file. SAVE also exists, but it blocks the server and is rarely appropriate in production.
The durability trade-off is direct: you lose every write made since the last successful snapshot. As an illustrative estimate, if snapshots succeed every five minutes and Redis crashes four minutes after the last one, those four minutes of writes are gone. If snapshots have been silently failing, the loss window grows without any warning.
In return, RDB files are compact. They are easy to copy offsite and portable between hosts, and they restore quickly because Redis loads a serialized image instead of replaying a log.
#!/usr/bin/env bash
# Inspect RDB settings, trigger a background snapshot, and confirm it succeeded.
set -euo pipefail
R="redis-cli -h 127.0.0.1 -p 6379" # placeholder host/port
# Snapshot if >=1 change in 900s, or >=10 changes in 300s.
# (Persist this in redis.conf too; CONFIG SET only changes the running server.)
$R CONFIG SET save "900 1 300 10"
$R CONFIG GET dir # directory where the snapshot file is written
$R CONFIG GET dbfilename # snapshot filename, dump.rdb by default
# Read one field from INFO persistence (output lines end in \r).
field() { $R INFO persistence | tr -d '\r' | awk -F: -v k="$1" '$1==k {print $2}'; }
before=$($R LASTSAVE) # Unix time of the last successful save
$R BGSAVE # fork a child; parent keeps serving clients
# Wait for the forked child to finish writing and renaming the file.
while [ "$(field rdb_bgsave_in_progress)" = "1" ]; do sleep 1; done
after=$($R LASTSAVE)
status=$(field rdb_last_bgsave_status)
echo "last bgsave status: $status"
echo "writes not yet on disk: $(field rdb_changes_since_last_save)"
# A failed snapshot leaves the old file in place, so LASTSAVE does not move.
# Alert on this: the data-loss window keeps growing until a save succeeds.
if [ "$status" != "ok" ] || [ "$after" -le "$before" ]; then
echo "RDB snapshot did not succeed; last good save at $after" >&2
exit 1
fi
echo "snapshot ok; loss window reset at $(date -d @"$after")"
Configures save rules, triggers BGSAVE, and uses LASTSAVE and INFO persistence to verify the snapshot succeeded so a silently growing data-loss window gets caught.
How does AOF work, and what does each fsync policy actually protect against?
With AOF enabled, Redis appends every write command to a log and rebuilds the dataset on startup by replaying it. Before replying to clients, Redis issues a write() that hands the data to the kernel page cache. That means a Redis process crash loses almost nothing under any policy, because the kernel still holds the data. The appendfsync setting matters for an OS crash or power loss, because it controls when the page cache is forced to stable storage:
alwaysfsyncs after each write. It gives the smallest loss window, but every write waits on disk latency, so throughput is limited by how fast the device completes fsync.everysecfsyncs once per second on a background thread. It exposes roughly the last second of writes, somewhat more if fsync is running slowly.noleaves flushing to the kernel. The loss window is whatever the OS has not yet written back, which can be tens of seconds.
The background thread does not fully isolate Redis from disk problems. If the disk stalls and an fsync is still pending, later writes from the main thread can block, which stalls command processing for every client.
The log grows because it records history rather than state: incrementing a counter a million times produces a million entries. A background rewrite forks the process and writes the shortest command sequence that reproduces the current data. auto-aof-rewrite-min-size keeps small files from triggering rewrites. auto-aof-rewrite-percentage starts a rewrite when the file has grown by that percentage since the last rewrite.
A crash during an append can leave a truncated or corrupted tail. redis-check-aof can repair the file, but its fix mode truncates at the first invalid entry. Every command after that point is discarded, so inspect the damage and keep a copy of the original before you accept the repair.
# Enable AOF at runtime and pick the fsync policy that matches the data-loss budget.
# everysec: about 1s of writes at risk on OS crash or power loss; a process crash loses almost nothing.
redis-cli CONFIG SET appendonly yes
redis-cli CONFIG SET appendfsync everysec
# Rewrite when the AOF has doubled since the last rewrite, but never below 64mb.
redis-cli CONFIG SET auto-aof-rewrite-percentage 100
redis-cli CONFIG SET auto-aof-rewrite-min-size 64mb
# Save the running config back to redis.conf so it survives a restart.
redis-cli CONFIG REWRITE
# Compact the log now instead of waiting for the automatic trigger.
redis-cli BGREWRITEAOF
# Check rewrite status and count slow fsyncs (aof_delayed_fsync grows when the disk stalls).
redis-cli INFO persistence | grep -E 'aof_enabled|aof_rewrite_in_progress|aof_last_bgrewrite_status|aof_delayed_fsync'
# --- Repairing a damaged AOF (with Redis stopped) ---
# Placeholder path. On Redis 7+ the AOF is split into several files inside appenddirname.
AOF_DIR=/var/lib/redis/appendonlydir
AOF_FILE="$AOF_DIR/appendonly.aof.1.incr.aof"
# Copy the files first: --fix truncates at the first invalid entry, and everything after it is lost.
cp -a "$AOF_DIR" "${AOF_DIR}.bak-$(date +%Y%m%d%H%M%S)"
# Run a read-only check to see where the corruption starts.
redis-check-aof "$AOF_FILE"
# Truncate only after reviewing what will be discarded. --fix asks for confirmation.
redis-check-aof --fix "$AOF_FILE"
Turns on AOF with everysec fsync, sets the automatic rewrite thresholds, starts a manual rewrite, watches for delayed fsyncs, and backs up the AOF before running a check and then a truncating redis-check-aof repair.
Redis RDB vs AOF: how do they compare on durability, recovery time, and cost?
Side-by-side comparison
| Option | Loss window on crash | Write latency impact | File size | Restart time | Fork behavior | Operational complexity |
|---|---|---|---|---|---|---|
| RDB only | Everything since the last snapshot | None on the write path | Compact, binary | Fast | Fork per snapshot | Low |
AOF, appendfsync always |
Close to zero | Highest: fsync before replying | Large until rewritten | Slow | Fork per rewrite | Medium |
AOF, everysec |
About one second, more if the disk stalls | Low | Large until rewritten | Slow | Fork per rewrite | Medium |
AOF, no |
Whatever the OS hasn't flushed | Lowest | Large until rewritten | Slow | Fork per rewrite | Medium |
| RDB + AOF | AOF's window (AOF is loaded on restart) | Same as chosen fsync policy | Both on disk | AOF-bound | Forks for both | Highest |
The restart gap comes from how each file is loaded. Redis deserializes an RDB file directly into memory. An AOF has to be replayed command by command, and between rewrites it also contains superseded writes, such as a key that was overwritten a thousand times. Restart time is downtime for that node, so it needs to be in the plan. It matters most when a cluster cold-starts all at once and there is no warm replica to promote.
Illustrative estimate (assumed numbers, not a benchmark): restart time ≈ file size ÷ load throughput.
- RDB: assume a 4 GB file loading at 200 MB/s. 4,000 ÷ 200 = 20 s.
- AOF: assume a 12 GB file replaying at 50 MB/s. 12,000 ÷ 50 = 240 s.
Measure both inputs on your own hardware before relying on the result.
Writing down each option's loss window and restart time keeps cost predictable. Incident reviews then confirm behavior the team already expected instead of uncovering it.
How do you turn a recovery point objective into Redis configuration?
Start with a number the team agrees on, such as "we can lose at most 5 seconds of writes." Once that number exists, the configuration follows from arithmetic.
Illustrative estimate. Assume a steady 2,000 writes per second. The rate is an assumption, so substitute your own measured rate.
- With a single rule
save 300 10, a crash just before the next snapshot loses up to 300 seconds of writes. That is about 600,000 writes, plus any snapshot that fails or is still in progress. - With
appendfsync everysec, the loss window is about one second, or roughly 2,000 writes. It can stretch toward two seconds if the disk stalls fsync. - With
appendfsync always, loss approaches zero. Every write then waits on the disk, so throughput is limited by fsync latency.
For a 5-second budget, snapshots alone fail by two orders of magnitude, everysec fits with margin, and always costs latency the budget doesn't require. For a budget of 15 minutes, RDB alone may be enough, and the AOF write overhead is unnecessary.
Combined setup. Enable both:
appendonly yeswithappendfsync everysecfor the tight loss window.- A few
saverules, for example hourly, to produce compact RDB files.
When AOF is enabled, Redis loads the AOF on startup even if an RDB file is present, because the AOF is the more complete record. The RDB files still have a job: they are single, portable point-in-time files that are easy to copy off-host, keep for retention, and restore into a separate instance.
The number also settles later disagreements. When someone proposes always or dropping AOF, compare the change against the agreed loss budget.
# redis.conf excerpt: agreed loss budget = 5 seconds of writes
# Illustrative estimate, assumed rate 2,000 writes/s:
# RDB only, save 300 10 -> up to ~300 s, ~600,000 writes lost
# AOF everysec -> ~1 s (up to ~2 s on fsync stall), ~2,000-4,000 writes
# AOF always -> ~0 writes lost, every write waits on fsync
# everysec fits the 5 s budget without paying for always.
dir /var/lib/redis
# --- AOF: provides the tight loss window ---
appendonly yes
appendfsync everysec
# Keep fsyncing during rewrites. Setting this to yes widens
# the loss window while a rewrite runs.
no-appendfsync-on-rewrite no
# Compact the AOF when it doubles in size, once it is past 64mb
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
# Rewritten AOF starts with an RDB preamble for faster loading
aof-use-rdb-preamble yes
# --- RDB: portable point-in-time files for backups ---
# Snapshot hourly if at least 1 key changed. This rule is for
# backups, not for meeting the loss budget.
save 3600 1
dbfilename dump.rdb
rdbcompression yes
rdbchecksum yes
# Reject writes if a snapshot fails, so backup failures get noticed
stop-writes-on-bgsave-error yes
# On startup with appendonly yes, Redis loads the AOF, not dump.rdb.
# Copy dump.rdb off-host on a schedule to keep retention and to
# restore into a separate instance.
A redis.conf excerpt that meets a 5-second loss budget with AOF everysec and keeps hourly RDB snapshots as portable backups, with the illustrative arithmetic in comments.
Why can forking for BGSAVE or AOF rewrite spike memory and latency?
BGSAVE and AOF rewrite both call fork(). The child gets a snapshot of the parent's memory without copying it up front: both processes share the same physical pages, marked copy-on-write. The cost appears when the parent keeps serving writes. The kernel duplicates each page the parent modifies, so the child can keep its frozen view. On a write-heavy instance, those duplicated pages pile up for as long as the child runs. If writes touch most pages before the child finishes, resident memory approaches twice the dataset. When that exceeds available RAM, the OOM killer may pick the largest process, which is usually the Redis parent. The dataset in memory is lost, and recovery falls back to the last completed file.
Illustrative estimate (assumed numbers, not a benchmark): peak ≈ dataset + (dataset × fraction of pages dirtied during the fork)
With 24 GB of data and a child that runs long enough for writes to dirty 40% of pages:
24 + (24 × 0.4) ≈ 33.6 GB
That figure is before allocator fragmentation and client buffers. Size maxmemory from this formula against host RAM, not by subtracting a small fixed margin.
Two Linux settings matter here:
vm.overcommit_memory = 1. Under the default heuristic mode, the kernel can refuse a fork of a large process even though copy-on-write means it won't need the full amount. The snapshot or rewrite then fails. Redis logs a warning at startup if this setting is wrong.- Transparent huge pages. With THP enabled, a one-byte write can force a 2 MB copy instead of a 4 KB one. This inflates both memory use and latency. Set it to
neverormadvise.
The fork call itself also has a cost. It copies page tables, which takes longer as memory grows, and it blocks the main thread while it runs. latest_fork_usec in INFO stats shows that pause for the most recent fork.
#!/usr/bin/env bash
# Pre-flight checks for fork-safe Redis persistence. Run as root on the Redis host.
set -euo pipefail
# 1. Allow fork() of a large process. Copy-on-write means the child rarely
# needs a full copy, but the default heuristic mode can still refuse it.
sysctl -w vm.overcommit_memory=1
echo 'vm.overcommit_memory = 1' > /etc/sysctl.d/90-redis.conf # persist across reboots
# 2. Disable transparent huge pages so a small write copies a 4 KB page,
# not a 2 MB one. This file resets on reboot; persist it with your
# boot tooling (systemd unit, kernel cmdline transparent_hugepage=never).
cat /sys/kernel/mm/transparent_hugepage/enabled # active value shown in [brackets]
echo never > /sys/kernel/mm/transparent_hugepage/enabled
# 3. Restart Redis afterwards so it re-checks these settings at startup.
# 4. After the next BGSAVE or AOF rewrite, measure what the fork actually cost.
redis-cli INFO stats | grep latest_fork_usec # main-thread block, microseconds
redis-cli INFO persistence | grep -E 'rdb_last_cow_size|aof_last_cow_size' # bytes copied by COW
redis-cli INFO memory | grep -E 'used_memory_human|used_memory_rss_human'
redis-cli CONFIG GET maxmemory
# Headroom rule of thumb (illustrative): dataset + observed COW bytes +
# fragmentation + client buffers must fit in host RAM with margin to spare.
A shell pre-flight that sets vm.overcommit_memory and disables transparent huge pages. It then reads Redis INFO fields that show how long the last fork blocked the main thread and how much memory copy-on-write duplicated.
How should you back up, monitor, and verify Redis persistence?
A persistence file on the same host as Redis is not a backup. A failed disk, a lost instance, or a deleted volume takes it out along with the data. Copy RDB files to separate storage on a schedule. This is safe while Redis is running, because Redis writes each snapshot to a temporary file and renames it into place only when it is complete. Then restore those copies into a scratch instance regularly and check key counts and a few known keys. A backup you have never restored is an assumption.
Replication does not replace this. A FLUSHALL, a bad migration, or an application bug that overwrites keys reaches every replica within milliseconds. Replication is also asynchronous. The primary acknowledges a write before any replica has it, so a failover can lose acknowledged writes no matter how the primary's appendfsync is set. WAIT narrows that window by blocking the client until a given number of replicas confirm receipt or a timeout expires. It does not make replicas fsync. A failover can also still promote a replica that missed the write, so treat WAIT as a partial mitigation and not a guarantee.
For monitoring, poll INFO persistence and alert on:
rdb_last_bgsave_statusnotok. A failedBGSAVEmeans the last good snapshot is getting older and your effective RPO is growing.aof_last_write_statusnotok, which signals write errors such as a full disk.aof_rewrite_in_progressstuck at1for an unusually long time.aof_delayed_fsyncincreasing, which means the disk cannot keep up witheverysecand fsyncs are being postponed.
When should you avoid relying on Redis persistence, and what are the alternatives?
Redis persistence fits workloads where losing a bounded window of writes is acceptable and the data can be recovered or tolerated. Examples include caches, session stores where users can log in again, job queues whose producers can re-enqueue, and rate limiters where a brief reset only loosens limits for a moment. In each case you can state the cost of losing the last second or the last few minutes.
Avoid it when Redis would be the system of record. Even appendfsync always protects only a single node. Replication is asynchronous, so a failover can promote a replica that never received acknowledged writes. MULTI/EXEC guarantees isolation but has no rollback, which falls short of relational transactions with constraints and multi-key atomic durability. For ledgers, orders, or anything with a zero-loss requirement, durability belongs in a database built for it.
| Pattern | When to use | Recovery after data loss |
|---|---|---|
Persistence disabled (save "", appendonly no) |
Pure caches | Cold start, refill on cache miss |
| RDB and/or AOF | Bounded loss budget | Reload from disk, accept the gap |
| Primary DB with Redis as a derived store | Data of record | Rebuild from the database |
The derived-store pattern is usually the safest. Writes commit to the primary database first, and Redis is populated from that data, either on read or from a change feed. Rebuilding Redis is then a routine operation: replay from the source of truth. Treat Redis as a fast store with bounded durability, and size that bound on purpose.
How OneEnv can help
Disclosure: OneEnv is the product behind this blog.
OneEnv (oneenv.io) is a workspace-based governance platform for the things services share — environment variables, database structure, API routes and validation rules. A change to a shared value is staged on a branch, reviewed once, and then every affected service explicitly approves or rejects it before it goes live, so you can see who a change touches before it ships instead of finding out in production.
Applied to redis rdb vs aof, this is where it helps:
Saves time
- Every service that inherits a shared value is listed in one place, so nobody searches repos and chats to find out who is affected.
- One review per branch replaces coordinating each team separately; each service still keeps its own approve/reject.
- Changing a shared variable is one edit and one review instead of editing every service's
.envby hand.
Saves cost
- Fewer production incidents and rollbacks caused by a shared value, route or column changing under a consumer unannounced.
- Less duplicated configuration to maintain: one shared definition inherited by many services.
- A wrong or stale value is caught at review time by the services it affects, not by a failed deploy.
- OneEnv has a free plan (1 workspace, unlimited services and members, full governance), so trying it costs nothing.
These are qualitative benefits, not measured results — how much you save depends on how many services share the value.
Want to try it? Start at oneenv.io.