Stage 3 · Build
Redis
Redis Persistence
RDB snapshots, AOF logging, hybrid persistence — tradeoffs and configuration.
Persistence Options
Redis is an in-memory database, but it can persist data to disk for durability. The three persistence modes are RDB (snapshots), AOF (append-only file), and hybrid (RDB + AOF). Each offers different tradeoffs between performance, data safety, and file size.
| Mode | Data Safety | Performance | File Size |
|---|---|---|---|
| RDB | May lose last few minutes | Fast reads/writes | Compact, single file |
| AOF | Loses at most 1 second | Slightly slower writes | Large, needs rewriting |
| Hybrid | Loses at most 1 second | Balanced | Moderate |
RDB Snapshots
# redis.conf
# Save if at least 1000 keys changed in 60 seconds
save 60 1000
# Save if at least 100 keys changed in 300 seconds
save 300 100
# Disable RDB (for pure AOF or caching-only)
save ""
# Compression
rdbcompression yes
rdbchecksum yes
# Snapshot filename
dbfilename dump.rdb
# Snapshot directory
dir /var/lib/redis/RDB forks a child process to create the snapshot. The fork is fast due to copy-on-write, but large datasets can cause latency spikes during the fork. The snapshot is a point-in-time copy — it does not capture changes made during the snapshot process.
# Trigger snapshot immediately
BGSAVE
# Check if snapshot is in progress
LASTSAVE
# Check last save time
INFO persistenceBGSAVE is non-blocking — it forks a background process to create the snapshot. SAVE blocks the server until the snapshot completes. Never use SAVE in production.
RDB snapshots are periodic. If Redis crashes between snapshots, all changes since the last snapshot are lost. Use AOF or hybrid for data that cannot afford to lose minutes of updates.
AOF Logging
# Enable AOF
appendonly yes
appendfilename "appendonly.aof"
# fsync policy: how often to flush to disk
# always: fsync after every write (safest, slowest)
# everysec: fsync once per second (good default)
# no: let OS decide (fastest, least safe)
appendfsync everysec
# Auto-rewrite AOF when it gets too large
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
# Use AOF for loading data even if RDB is newer
aof-use-rdb-preamble yesappendfsync = everysec is the recommended default. It means at most 1 second of data loss on crash. The 'always' setting provides maximum durability but can reduce write throughput by 10x.
# Rewrite AOF (compact the log)
BGREWRITEAOF
# Check AOF info
INFO aof
# Repair corrupted AOF
redis-check-aof --fix appendonly.aofThe AOF grows indefinitely as commands are appended. BGREWRITEAOF creates a compact version with only the current state, discarding redundant commands. It runs in the background without blocking clients.
Hybrid Persistence
# Hybrid: AOF file starts with RDB snapshot, followed by AOF commands
appendonly yes
aof-use-rdb-preamble yes
# This gives you:
# 1. Fast startup from RDB snapshot
# 2. Durability from AOF commands after the snapshot
# 3. Smaller AOF file (RDB is more compact than AOF)Hybrid persistence is the recommended approach for production. It combines fast restarts (from RDB preamble) with AOF durability. On restart, Redis loads the RDB preamble, then replays the AOF tail.
Backup Strategies
#!/usr/bin/env bash
# redis-backup.sh — safe backup without downtime
set -euo pipefail
BACKUP_DIR="/backup/redis"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
REDIS_DIR="/var/lib/redis"
# Trigger BGSAVE and wait for completion
redis-cli BGSAVE
while [ "$(redis-cli LASTSAVE)" == "$LAST_SAVE" ]; do
sleep 1
done
# Copy the snapshot
cp "$REDIS_DIR/dump.rdb" "$BACKUP_DIR/dump_$TIMESTAMP.rdb"
# Also backup AOF if enabled
if [ -f "$REDIS_DIR/appendonly.aof" ]; then
cp "$REDIS_DIR/appendonly.aof" "$BACKUP_DIR/aof_$TIMESTAMP.aof"
fi
# Compress older backups
find "$BACKUP_DIR" -name "dump_*.rdb" -mtime +1 -exec gzip {} \;
# Keep only last 7 days
find "$BACKUP_DIR" -name "*.rdb.gz" -mtime +7 -delete
find "$BACKUP_DIR" -name "*.aof" -mtime +7 -delete
echo "Backup completed: $TIMESTAMP"This script triggers BGSAVE, waits for completion, and copies the snapshot to a backup directory. The snapshot is safe to copy while Redis is running — it is a point-in-time file.
Disaster Recovery
# 1. Stop Redis
redis-cli SHUTDOWN NOSAVE
# 2. Restore from backup
cp /backup/redis/dump_latest.rdb /var/lib/redis/dump.rdb
chown redis:redis /var/lib/redis/dump.rdb
# 3. If using AOF, also restore AOF
cp /backup/redis/aof_latest.aof /var/lib/redis/appendonly.aof
chown redis:redis /var/lib/redis/appendonly.aof
# 4. Start Redis
systemctl start redis
# 5. Verify data
redis-cli DBSIZE
redis-cli INFO keyspaceRedis loads RDB on startup by default. If AOF is enabled, it loads AOF instead (it is more complete). Ensure file permissions are correct — Redis will refuse to start if the dump file is owned by the wrong user.
Restore backups to a separate Redis instance monthly. Verify key counts, data integrity, and load time. An untested backup strategy is not a backup strategy.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.