iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Persistence (RDB / AOF)

Redis persistence: RDB snapshots, AOF append-only file, hybrid, and how to pick a durability story.

Redis — persistence

EXAMPLE
# ===== Three modes =====
# 1. RDB snapshots (default)
#    Periodic point-in-time dump of the dataset to dump.rdb.
#    Configurable triggers: save N seconds if M+ keys changed.
#    Fast to restart from. Lose data since last snapshot on crash.
#
# 2. AOF (Append Only File)
#    Log every write command. Replay on startup to rebuild state.
#    Durable; trades disk + write amplification for safety.
#
# 3. Hybrid (RDB + AOF; recommended in 2026)
#    AOF rewrites embed an RDB snapshot at the head, then append commands.
#    Best of both: fast restart + low data loss.

# ===== RDB configuration =====
# redis.conf
save 3600 1                 # snapshot if 1+ key changed in 3600s
save 300 100                # ... or 100+ keys in 300s
save 60 10000               # ... or 10000+ keys in 60s
dbfilename dump.rdb
dir /var/lib/redis

# Disable RDB completely:
save ""

# Trigger manually:
BGSAVE                       # background snapshot
SAVE                         # blocking; do not use in prod

# ===== AOF configuration =====
appendonly yes
appendfilename appendonly.aof
appendfsync everysec          # fsync once per second (good default)
# Other choices:
# appendfsync always         # fsync after every write (slow + safe)
# appendfsync no             # OS decides (fast + risky)
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

# AOF rewrite reduces file size (compacts commands).
BGREWRITEAOF

# ===== Hybrid mode (default in modern Redis) =====
aof-use-rdb-preamble yes
# AOF rewrites start with an RDB snapshot, then append.

# ===== Durability vs performance trade-off =====
# RDB only:    fast, smallest writes; lose seconds-to-minutes of data
# AOF everysec: ~1s data loss on crash; small perf impact
# AOF always:   minimal data loss; significant perf impact
# AOF + replication: copy data to a replica synchronously? No (Redis is async),
#                    but combined with AOF gives strong availability.

# ===== Restart behaviour =====
# If AOF is enabled, Redis loads from AOF (more recent than RDB).
# If AOF is disabled, loads from dump.rdb.
# Loading is single-threaded; large datasets take time.

# ===== Backups =====
# Copy dump.rdb (after BGSAVE) or appendonly.aof off the host periodically.
# Use redis-cli --rdb to dump from a running instance.

redis-cli --rdb /backup/dump-$(date +%F).rdb

# ===== Replication =====
# Replicas pull from the primary; if AOF is on for the replica, it persists locally.
# Sentinel + replication = HA; cluster mode = sharding + replication.

# ===== Picking a persistence story =====
# Cache only:    RDB off, AOF off, accept full data loss on restart
# Hot cache + warm restore: RDB only (fast restart, some data loss)
# Session / queue durability: AOF everysec, RDB on
# Mission-critical: AOF everysec + replication + offsite backups

# ===== Monitoring =====
INFO persistence
# Shows: rdb_last_save_time, aof_enabled, aof_current_size, aof_rewrite_in_progress

# ===== Patterns to internalise =====
# - Hybrid mode (default since 7.x) for most prod
# - AOF everysec as the safe default
# - Take periodic off-host backups
# - Test the restore (not just the backup)

# ===== Pitfalls =====
# - 'Persistence off' on a queue (data loss on restart)
# - AOF always on a high-throughput instance (latency)
# - RDB only with a busy write workload (lose minutes of data)
# - Disk full -> Redis stops accepting writes (write to disk full)

Why it matters

Redis persistence is a dial: RDB for fast restore, AOF for durability, hybrid for both. Default hybrid + AOF everysec covers most cases. Pair with replication for HA, off-host backups for disaster recovery, and TEST the restore so the dial actually works when you need it.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# RDB (snapshot)
save 900 1
save 300 10
# AOF (append-only log)
appendonly yes
appendfsync everysec
Try it Yourself »

Test yourself

Q1. RDB persistence writes…
Q2. AOF persistence writes…
Q3. For best durability vs throughput, common pick is…

Discussion

Loading…