Skip to content
Redis Persistence: RDB, AOF, Hybrid & the Pure Cache Dilemma
Databases 12 min read

Redis Persistence: RDB, AOF, Hybrid & the Pure Cache Dilemma

This article explains Redis persistence mechanisms (RDB snapshots, AOF logging, hybrid persistence), their trade-offs (fork latency, memory overhead, data loss windows), and provides a decision framework: disable persistence for pure caches backed by a database, enable RDB+AOF everysec for caches with locks/counters, and never rely on Redis as the sole source of truth.

Code Farmer Manor Chronicle
Code Farmer Manor Chronicle
Code Farmer Manor Chronicle

RDB: Periodic Full Snapshots

RDB writes a binary dump of the entire in-memory dataset to dump.rdb at configurable intervals. The default rules in redis.conf are:

save 900 1      # at least 1 change in 900 seconds
save 300 10     # at least 10 changes in 300 seconds
save 60 10000   # at least 10000 changes in 60 seconds
dbfilename dump.rdb
dir /var/lib/redis

Any rule triggers a background save ( bgsave). The main process fork() s a child; the child traverses memory and writes the snapshot while the parent continues serving requests. This relies on the OS copy-on-write (COW) mechanism: after fork, parent and child share the same physical pages; when the parent modifies a page, that page is copied, so the child sees a consistent point-in-time view.

This implementation yields four inevitable consequences that explain all RDB-related production stalls:

fork() itself is blocking. The larger the instance, the longer the fork. A 10 GB instance can take 100 ms or more, during which the main thread is completely stopped. Therefore large instances should schedule bgsave, AOF rewrite, and diskless replication away from peak hours.

COW can double memory usage. The more writes during the snapshot, the more pages are copied. Physical memory consumed by the child grows with write intensity. Reserve 50% memory headroom.

There is a data-loss window. Writes between two snapshots are lost on restart. With save 60 10000 the worst case is losing over a minute of data.

SAVE is a blocking full disk write — never use in production; use BGSAVE .

RDB's greatest strength is recovery speed: loading the binary file is an order of magnitude faster than replaying commands one by one.

AOF: Log Every Write Command

AOF takes a different approach: store the operations, not the result. Every write command is appended to a log file; on restart the commands are replayed from the beginning.

appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec        # core setting

auto-aof-rewrite-percentage 100  # trigger when size doubles since last rewrite
auto-aof-rewrite-min-size 64mb   # and at least 64 MB
appendfsync

controls how often data is flushed to disk. The three modes differ dramatically: always — fsync after every command. Max loss ≈ 0. Throughput worst (one disk I/O per command).

everysec — background thread fsyncs once per second. Max loss 1 second. Good throughput. no — delegate to OS (≈ 30 seconds). Max loss 30 seconds. Best throughput.

everysec is the answer for the vast majority of cases: losing one second of data is acceptable for caches, locks, and counters, while always imposes a real throughput penalty.

AOF grows without bound because a key updated 100 times generates 100 log entries. Hence rewrite : the child process (again via fork + COW) produces a minimal equivalent command set from the current in-memory data. During rewrite, new writes go into a buffer and are appended after rewrite finishes — under heavy write load this buffer can swell memory and prolong rewrite time.

AOF recovery is the opposite of RDB: slow, because every command must be re-executed.

Hybrid Persistence: The 4.0+ Default Optimum

aof-use-rdb-preamble yes   # enabled by default since 4.0

The idea is straightforward: during AOF rewrite, the first half of the file is written in RDB format (full dataset), the second half appends incremental commands.

Fast recovery: the RDB prefix loads directly, no command replay.

Small loss window: only the post-rewrite tail is commands, so loss window still equals the appendfsync setting.

You get RDB's recovery speed plus AOF's low data loss. New instances without legacy baggage should enable hybrid persistence directly.

Choose Configuration by Redis Role (Key Section)

The same Redis instance needs completely opposite settings depending on its role:

Pure cache (database is the system of record) — Disable all persistence: comment out all save lines + appendonly no. Reason: data can be rebuilt from the DB; persistence only wastes I/O and memory headroom.

Cache + locks / counters — RDB + AOF everysec (enable hybrid). Reason: locks and counters must survive restart as much as possible; a 1-second window is acceptable.

Used as primary store (not recommended but happens) — AOF everysec + replication + regular backups. Even always cannot guarantee zero loss on crash; the relational database remains the ultimate source of truth.

The first row is the counter-intuitive one. Many teams assume "persistence is always good," but it has costs: fork stalls, COW memory doubling, disk I/O. If Redis data can be reconstructed from MySQL, those costs are pure waste and actually degrade cache performance.

Decision rule in one sentence: If Redis dies and its data is wiped, what happens to the business?

Just a brief slowdown while cache warms up → turn persistence off.

Lock loss causes duplicate charges, counter reset breaks reports → turn it on.

Redis is the only data source → fix the architecture, not the config.

Operational Metrics to Watch

INFO persistence
# rdb_last_bgsave_status:ok          ← last snapshot success
# aof_last_write_status:ok           ← last fsync success
# rdb_changes_since_last_save        ← large value means snapshot lagging
# aof_rewrite_in_progress:1          ← rewrite running; avoid other heavy ops

Three habits worth forming:

Run BGSAVE manually before a release — captures a known-good snapshot for rollback confidence.

Schedule BGREWRITEAOF during off-peak — it also forks.

Back up corrupted files before attempting recovery. RDB corruption can be checked with redis-check-rdb, but recoverable data is limited; never experiment on the original file.

File placement matters:

RDB: dump.rdb goes to the directory set by dir.

AOF: Since 7.0 it uses a multi-file structure with an appendonlydir/ directory and a manifest file. You must replace the entire directory; overwriting only appendonly.aof does not work — a pitfall I hit in a Docker environment.

Checklist

RDB = fork + COW full snapshot; fast recovery, large loss window; watch fork blocking and memory doubling on large instances.

AOF = write command log; everysec is the sweet spot (max 1s loss); rewrite also forks.

4.0+ enable hybrid persistence aof-use-rdb-preamble yes.

Pure cache role can disable persistence entirely — evaluate "what if it's lost" before changing config.

Pre-deploy BGSAVE for a snapshot; monitor INFO persistence for health.

7.0 AOF recovery requires replacing the whole appendonlydir/.

Core data always belongs in a relational database; persistence is a safety net, not the primary guarantee.

One-sentence summary: Persistence solves "restart without loss," not "never lose." Treating Redis as the sole source of truth no amount of configuration can save you.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

OperationsRedisConfigurationPersistenceCache StrategyAOFRDBHybrid Persistence
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.