What is Redis persistence and how do RDB and AOF work?

Redis keeps its dataset in memory. Persistence is how that dataset survives a restart. Neither mechanism provides transactional durability: Redis acknowledges a write before it is guaranteed to be on disk. What you choose is a bound on how much acknowledged data a crash can lose.

RDB: point-in-time snapshots

BGSAVE forks a child process. The child shares the parent's memory pages through copy-on-write and serializes them to a temporary file, then renames it into place. The main process keeps serving writes. Each page the main process modifies is copied by the kernel, so the child still sees a frozen view of the data.

Snapshots run when a save rule fires (N seconds elapsed and at least M changes) or when you trigger one manually. On restart, Redis loads the last complete file. The loss window is everything written since the last successful snapshot, so a failed or stalled BGSAVE quietly widens it.

AOF: an append-only command log

Each write command is appended to the AOF file through write(), which places it in the OS page cache. The appendfsync policy decides when the page cache is flushed to disk. On startup, Redis replays the log. The log grows without limit, so BGREWRITEAOF forks a child that writes a compact log from the current dataset. New writes are still recorded while the rewrite runs.

How data reaches disk on each path

Step RDB path AOF path
1. Main process Forks a child, keeps serving writes Appends each write to the AOF buffer
2. Copy-on-write The kernel copies each page the main process modifies Applies only during a rewrite fork
3. Forked child Serializes a frozen view to a temp file Writes the compacted log during a rewrite
4. Page cache Holds the file until it is flushed Holds appended data until fsync
5. Disk Temp file is renamed into place Data is durable after fsync completes
+----------------------------------------------------------------------+
|                Redis main process (dataset in memory)                |
|             acks writes before they are durable on disk              |
+----------------------------------------------------------------------+
   | BGSAVE / save rule       | each write cmd         | BGREWRITEAOF
   | fork()                   v                        | fork()
   v                   +---------------+               v
+------------------+   | AOF buffer    |       +------------------+
| CoW shared pages |   +---------------+       | CoW shared pages |
| parent writes a  |           |               +------------------+
| page -> kernel   |           |                        |
| copies it        |           |                        v
+------------------+           |               +------------------+
   |                           |               | forked child     |
   v                           |               | writes compact   |
+------------------+           |               | log from dataset |
| forked child     |           |               +------------------+
| serializes frozen|           |                        |
| view to temp file|           |                        |
| then renames     |           |                        |
+------------------+           |                        |
   | write()                   | write()                | write()
   v                           v                        v
+----------------------------------------------------------------------+
|        OS page cache  (AOF flush timing: appendfsync policy)         |
+----------------------------------------------------------------------+
   | fsync / kernel writeback
   v
+----------------------------------------------------------------------+
|     disk:  RDB file (last good snapshot)   |   AOF file (log)        |
+----------------------------------------------------------------------+

RDB snapshots and AOF rewrites both fork a child that shares memory through copy-on-write. All writes pass through the page cache before reaching disk, so either path bounds data loss but does not make it transactional.

Redis RDB vs AOF: how do durability, performance, and recovery compare?

Dimension RDB AOF
Worst-case loss window Everything written since the last successful snapshot, often minutes Depends on appendfsync: roughly one second with everysec, near zero with always
Write-path overhead None per command; cost arrives in bursts at snapshot time Every write command is appended to a buffer and then flushed to the file
Disk I/O pattern Large sequential writes, periodically Continuous small appends, plus fsync calls
File size Compact binary representation of current state Grows with write history until a rewrite compacts it
Startup time Fast: deserialize a snapshot Slower: replay commands, proportional to log length
Fork frequency Once per snapshot Once per rewrite
Typical failure mode A background save fails and no new snapshot is produced A truncated or corrupted tail after a crash, or a log that grows unchecked

The loss window is the central tradeoff. An RDB snapshot is only as recent as the last fork that completed. AOF records each mutation as it happens, so the risk depends on how often those appends reach stable storage.

RDB failures are quiet. If BGSAVE fails because the fork is refused, the disk is full, or permissions are wrong, Redis keeps serving traffic from memory. The newest snapshot on disk falls further behind. You learn about it on the next restart unless you alert on the save status and last-success timestamp reported by INFO persistence.

Recovery time follows file structure. Loading an RDB file rebuilds the dataset directly. Replaying an AOF re-executes every logged command, including overwrites that no longer matter. A large AOF can therefore keep a node unavailable for much longer after a restart. Regular rewrites keep that replay time bounded.

What does fsync actually guarantee with appendfsync always, everysec, and no?

Every policy starts the same way: Redis calls write() on the AOF, which moves data into the kernel's page cache. That copy survives a Redis process crash, because the kernel still holds it and will flush it later. It does not survive a kernel panic, power loss or VM termination. Only fsync() asks the kernel to push those pages to stable storage and wait for confirmation. The three appendfsync settings differ in when that call happens and who waits for it.

Policy When fsync runs Typical loss on power failure Latency and throughput cost
always Before replying to clients, once per event-loop batch of writes Acknowledged writes are not lost, assuming the disk is honest Every write waits on disk flush latency, so throughput is bounded by fsync speed
everysec Background thread, about once per second About 1 second, more if fsync stalls Small, because the main thread rarely blocks
no Whenever the OS decides to flush Up to the kernel's flush interval, often tens of seconds Lowest

everysec is the common default because it bounds loss to about a second while keeping disk latency off the request path. Under disk contention, a slow fsync can make Redis postpone further writes. Loss can then stretch past one second, and the main thread can stall.

always makes client latency depend directly on the storage device. On a local NVMe drive that may be tolerable. On a network-attached volume, each fsync crosses the network, and the added latency can make the setting unusable.

All three guarantees assume that fsync means durable. Some drives have volatile write caches without power-loss protection, and some virtualization layers acknowledge writes before persisting them. Either one can report success early, and then even always can lose acknowledged writes.

#!/usr/bin/env bash
# Compare appendfsync policies on a disposable test instance, never production.
set -euo pipefail
CLI="redis-cli -h 127.0.0.1 -p 6379"

# Enable AOF so the fsync policy applies.
$CLI CONFIG SET appendonly yes

for policy in always everysec no; do
  $CLI CONFIG SET appendfsync "$policy" >/dev/null
  echo "== appendfsync $policy =="
  # SET-only load. Under 'always', throughput tracks fsync speed.
  redis-benchmark -h 127.0.0.1 -p 6379 -t set -n 100000 -q
done

# Return to the common default before checking stall counters.
$CLI CONFIG SET appendfsync everysec

# aof_delayed_fsync counts times the background fsync was still running
# when the next write arrived. A rising value means disk contention, and
# the loss window under everysec is then wider than one second.
$CLI INFO persistence | grep -E 'aof_enabled|aof_delayed_fsync|aof_last_write_status'

# Check whether the kernel treats the device as having a volatile cache.
# 'write back' means writes can be acknowledged before reaching media.
# fsync then relies on the device honoring cache flushes. Disks without
# power-loss protection, or virtual volumes, may not honor them.
DEV=sda   # placeholder: the block device backing the Redis data dir
cat /sys/block/"$DEV"/queue/write_cache

A test script that benchmarks each appendfsync policy, reads the aof_delayed_fsync counter to detect fsync stalls under everysec, and checks whether the underlying device uses a volatile write-back cache that can acknowledge writes before they are durable.

How much memory does forking cost, and how much data can you lose? An illustrative estimate

Forking is how Redis persists without blocking, and it costs both latency and memory. fork() copies the parent's page tables, so the call itself takes longer as the dataset grows. During that time the main thread is stalled, and on a dataset of tens of gigabytes the pause can be long enough to show up in client latency. After the fork, copy-on-write means every page the parent modifies while the child is still saving gets duplicated. Write-heavy workloads can therefore push resident memory well past the dataset size. Transparent huge pages make this worse, because a single small write copies a huge page instead of a 4 KB one.

With vm.overcommit_memory=0, the kernel's heuristic can refuse a fork it judges too large, and the background save fails. Redis recommends 1, which lets the fork succeed. That setting removes the kernel's check, so you also need real headroom. Otherwise copy-on-write growth can trigger the OOM killer. AOF rewrite forks too, so AOF does not avoid this cost.

Illustrative estimate (assumed numbers, not a benchmark). These are the assumed inputs:

  • Dataset D = 20 GB
  • Write rate W = 5,000 writes/s
  • Fraction of pages dirtied during the child's lifetime f = 0.3
  • RDB save rule: snapshot every 300 s (T_int)
  • Save duration T_save = 60 s

The formulas:

  • Peak memory ≈ D × (1 + f)
  • Worst-case loss ≈ W × (T_int + T_save)
Setup Peak fork memory Worst-case loss
RDB only, 300 s rule 20 × 1.3 = 26 GB 5,000 × 360 = 1.8M writes
AOF everysec ~26 GB during rewrite, plus rewrite buffer ~2 s × 5,000 = 10,000 writes

The two setups need similar memory headroom. The difference is how much data you can lose, and AOF everysec loses far less.

How do you configure RDB and AOF together in production?

A production baseline turns on AOF with appendonly yes and appendfsync everysec. It sets auto-aof-rewrite-percentage and auto-aof-rewrite-min-size so rewrites trigger on growth rather than on every small change, and it keeps save rules so RDB snapshots continue on a schedule. The AOF provides durability. The RDB files are compact, point-in-time artifacts that are easy to copy, archive, and restore.

When both are enabled, Redis loads the AOF at startup and ignores the RDB file, because the AOF is the more complete record. With aof-use-rdb-preamble yes, a rewrite writes the dataset in RDB format first and then appends new commands as AOF entries. You get fast loading from the binary preamble and fine-grained recovery from the tail.

Operational checks

  • Monitor INFO persistence. Alert on rdb_last_bgsave_status or aof_last_bgrewrite_status reporting err, on aof_last_write_status failures, and on rdb_changes_since_last_save growing without bound. A failed save does not crash Redis, so you only find out if you check these fields.
  • Copy snapshots off-host. A file on the same disk is lost along with that disk.
  • Test restores regularly. Load a copied snapshot into a scratch instance and verify key counts.
  • Inspect damaged files. Run redis-check-rdb to validate a snapshot. Run redis-check-aof on a truncated AOF, and use its --fix option only on a copy, since it discards the incomplete tail.

These checks surface failures while they are still cheap to fix. Without them, the first sign of a problem is often missing data after an incident, followed by days of reconstructing which writes were lost and why.

# redis.conf: AOF for durability, RDB snapshots for backups

# Working directory for both dump.rdb and the AOF files
dir /var/lib/redis

# --- AOF: primary durability mechanism ---
appendonly yes
# fsync once per second: at most ~1s of writes lost on a crash
appendfsync everysec
# Keep fsyncing during rewrites (safer, slightly more latency)
no-appendfsync-on-rewrite no

# Rewrite when the AOF has doubled since the last rewrite,
# but never for files smaller than 64mb
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

# Rewrites start with an RDB-format preamble, then append AOF commands
aof-use-rdb-preamble yes
# On startup, load a truncated AOF tail instead of refusing to start
# (Redis logs a warning; still investigate with redis-check-aof)
aof-load-truncated yes

# --- RDB: scheduled point-in-time snapshots to copy off-host ---
dbfilename dump.rdb
# Snapshot after 3600s if >=1 change, 300s if >=100, 60s if >=10000
save 3600 1
save 300 100
save 60 10000
# Reject writes if a background save fails, so failures are visible
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes

# With appendonly yes, Redis loads the AOF at startup, not dump.rdb

A redis.conf baseline that enables AOF with appendfsync everysec, growth-based auto-rewrite, and the RDB preamble, and keeps save rules so RDB snapshots are still produced for off-host backups.

Why isn't replication a substitute for persistence?

Redis replication is asynchronous. The primary acknowledges a write to the client before any replica has received it, so the replication stream always trails the primary by some amount. If the primary fails and Sentinel or Cluster promotes a replica, every write still in that gap is gone. The client was told it succeeded. Local fsync settings on the primary don't change this. appendfsync always protects the primary's own disk, but the promoted replica never saw those writes. When the old primary rejoins, it resyncs from the new primary and discards its own copy.

WAIT narrows the gap. It blocks the client until a given number of replicas have acknowledged the preceding writes, or until a timeout expires. It does not turn Redis into a strongly consistent system, for three reasons:

  • An acknowledgment means the replica received the write, not that it persisted it.
  • On timeout, WAIT returns how many replicas actually acknowledged, and the write stays applied on the primary either way. The application has to check that number and decide what to do.
  • Failover can still promote a replica that missed the write.

A worse failure happens when you disable persistence on the primary and rely on replicas for safety. If a process supervisor restarts the crashed primary quickly, the primary comes back with an empty dataset. Replicas then sync from it and wipe their own copies, which loses everything. If a primary runs without persistence, don't let it auto-restart.

Persistence only protects against a crashed or restarted process. It does nothing if the disk fails or the host is destroyed. For those cases you need RDB snapshots copied to separate storage on a different machine.

import redis

# Placeholder connection details for the current primary.
r = redis.Redis(host="redis-primary.internal", port=6379, password="example-password")

REQUIRED_REPLICAS = 1
TIMEOUT_MS = 500

def durable_set(key, value):
    # The primary applies the write and acknowledges it right away.
    r.set(key, value)

    # WAIT blocks until REQUIRED_REPLICAS have acknowledged all earlier
    # writes on this connection, or until TIMEOUT_MS passes.
    # It returns how many replicas acknowledged.
    acked = r.wait(REQUIRED_REPLICAS, TIMEOUT_MS)

    if acked < REQUIRED_REPLICAS:
        # The write is NOT rolled back. It stays on the primary and may
        # still reach replicas later, or be lost if failover happens first.
        # The application decides what to do: retry, alert, or report
        # the write as not confirmed durable.
        raise RuntimeError(
            f"{key}: only {acked}/{REQUIRED_REPLICAS} replicas acknowledged"
        )

    # Even here, the replica only received the write. It may not have
    # persisted it, and failover can still promote a replica that missed it.
    return acked

if __name__ == "__main__":
    durable_set("order:1001:status", "paid")

Using WAIT after a write to check how many replicas received it, and handling a timeout in the application, because WAIT never rolls back the write on the primary.

Which persistence setup should you use, and when should you avoid Redis for durable data?

Workload Configuration Acknowledged writes you may lose
Pure cache Persistence off, or RDB only for faster warm restarts All of them; the source of truth lives elsewhere
Cache that is expensive to rebuild RDB plus AOF everysec Roughly the last second
Job queue AOF everysec, or always on low-latency disks, plus WAIT on critical enqueues Up to a second with everysec; none from a clean crash with always
System of record Usually a transactional database instead None should be acceptable

Redis is the wrong tool for a system of record when you need any of the following:

  • Constraints, or multi-key transactions with rollback.
  • Point-in-time recovery.
  • A guarantee that an acknowledged write survives losing the primary.

WAIT only confirms that replicas received a write. It does not confirm they persisted it, and a failover can still promote a replica that missed it. A transactional database with synchronous commit and synchronous replication provides the guarantee that Redis approximates.

Budget the operating cost before you rely on persistence:

  • Memory: Reserve headroom for copy-on-write during forks.
  • Disk: Plan disk I/O for fsync and for AOF rewrites, which compete with each other.
  • Monitoring: Track fork latency, last successful save, rewrite duration and AOF size.
  • Restore drills: Load a real backup into a scratch instance on a schedule and time it. A backup you have never restored is an assumption.

The team that owns the data should set the acceptable loss window and recovery time. The platform team implements the configuration, runs the drills and reports when reality drifts from the target. Record both numbers per instance, so nobody discovers the tradeoff during an outage.

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 .env by 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.