Redis Persistence
Redis stores data in memory. Persistence saves that data to disk. This lesson covers RDB and AOF persistence methods.
Why Persistence?
Redis is an in-memory database:
- Problem: Server restart or crash causes memory data loss
- Solution: Persistence saves in-memory data to disk for recovery after restart
Memory Data → Persistence → Disk File
Disk File → Recovery → Memory Data
Redis provides two persistence methods:
- RDB: Snapshot — saves data at a point in time
- AOF: Log — records every write operation
RDB Persistence
What is RDB?
RDB (Redis Database) saves data at a specific point in time as a binary snapshot file.
Features:
- Saves complete data at a point in time
- Compact file, suitable for backup and transfer
- Fast recovery speed
- May lose data after the last snapshot
RDB Configuration
Configure in redis.conf:
# Snapshot trigger conditions
# save <seconds> <changes>
save 900 1 # At least 1 change in 900 seconds → trigger snapshot
save 300 10 # At least 10 changes in 300 seconds → trigger snapshot
save 60 10000 # At least 10000 changes in 60 seconds → trigger snapshot
# Disable RDB (comment out save or set to empty)
# save ""
# RDB file name
dbfilename dump.rdb
# RDB file storage directory
dir /var/lib/redis
# RDB compression (recommended to keep on)
rdbcompression yes
# RDB checksum (recommended to keep on)
rdbchecksum yes
Manual RDB Triggers
# SAVE: blocking snapshot (use with caution in production)
SAVE
OK # Snapshot complete, Redis was blocked during this time
# BGSAVE: background snapshot (recommended)
BGSAVE
Background saving started # Returns immediately, runs in background
RDB File Location
# Default location
/var/lib/redis/dump.rdb
# Or directory specified in config
dir /data/redis
dbfilename dump.rdb
RDB Recovery
Redis automatically loads the RDB file on startup:
# 1. Place the RDB file in the configured directory
cp dump.rdb /var/lib/redis/
# 2. Start Redis
redis-server
# Redis automatically loads dump.rdb to recover data
RDB Pros and Cons
Pros:
- Compact file, suitable for backup
- Fast recovery speed
- Minimal performance impact (BGSAVE in background)
Cons:
- May lose data after the last snapshot
- Data loss within snapshot intervals
- Large-data snapshots take longer
AOF Persistence
What is AOF?
AOF (Append Only File) records every write command. On recovery, Redis re-executes those commands.
Features:
- Records every write operation, safer data
- Human-readable file, easy to analyze and repair
- Possible data bloat (needs rewrite)
- Slower recovery speed
AOF Configuration
# Enable AOF
appendonly yes
# AOF file name
appendfilename "appendonly.aof"
# AOF sync strategy
appendfsync everysec
# Disable fsync during AOF rewrite
no-appendfsync-on-rewrite no
# AOF file size triggers rewrite
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
AOF Sync Strategies
| Strategy | Description | Performance | Safety |
|---|---|---|---|
| always | Sync on every write | Slowest | Safest |
| everysec | Sync once per second | Moderate | Reasonably safe (max 1 second data loss) |
| no | Let the OS decide | Fastest | Least safe |
appendfsync everysec — balances performance and safety.
AOF Rewrite
The AOF file grows continuously. Rewrite compresses it:
# Manually trigger AOF rewrite
BGREWRITEAOF
Background append only file rewriting started
Rewrite principle:
- Read current in-memory data
- Generate the minimum set of commands to restore that data
- Replace the old AOF file
Rewrite trigger conditions:
# AOF file size grew 100% since the last rewrite
auto-aof-rewrite-percentage 100
# AOF file must be at least 64MB to trigger rewrite
auto-aof-rewrite-min-size 64mb
AOF Recovery
Redis automatically loads the AOF file on startup:
# 1. Place the AOF file in the configured directory
cp appendonly.aof /var/lib/redis/
# 2. Start Redis
redis-server
# Redis automatically loads appendonly.aof to recover data
AOF File Repair
# Check AOF file
redis-check-aof appendonly.aof
# Repair AOF file
redis-check-aof --fix appendonly.aof
AOF Pros and Cons
Pros:
- Safer data, at most 1 second of data loss
- Human-readable file, easy to analyze
- Supports repairing corrupted files
Cons:
- Larger file size
- Slower recovery speed
- Some performance impact
RDB vs AOF Comparison
| Aspect | RDB | AOF |
|---|---|---|
| File size | Small (compact binary) | Large (text commands) |
| Recovery speed | Fast | Slow |
| Data safety | May lose minutes of data | At most 1 second lost |
| Performance impact | Low (background snapshot) | Medium (frequent fsync) |
| File readability | Not readable | Readable |
| Use case | Backup, replication | High data safety requirements |
Hybrid Persistence
Redis 4.0+ supports hybrid persistence, combining the benefits of RDB and AOF.
Configuring Hybrid Persistence
# Enable AOF
appendonly yes
# Enable hybrid persistence
aof-use-rdb-preamble yes
Hybrid Persistence Principle
During AOF rewrite:
- First write current data in RDB format (fast, compact)
- Then append new write operations in AOF format
AOF file structure:
[RDB format snapshot data] + [AOF format incremental commands]
Hybrid Persistence Advantages
- Fast recovery (RDB part)
- Safe data (AOF part)
- Moderate file size
Persistence Best Practices
Scenario 1: Cache Only
# Caching may not need persistence, or just RDB
save 900 1
appendonly no
Scenario 2: Data Storage
# Data storage uses AOF or hybrid persistence
save 900 1
appendonly yes
appendfsync everysec
aof-use-rdb-preamble yes
Scenario 3: High Availability
# Enable both RDB and AOF
save 900 1
save 300 10
appendonly yes
appendfsync everysec
aof-use-rdb-preamble yes
Backup Strategy
# Regularly backup RDB and AOF files
# 1. Use cron to copy files
0 2 * * * cp /var/lib/redis/dump.rdb /backup/dump-$(date +\%Y\%m\%d).rdb
0 2 * * * cp /var/lib/redis/appendonly.aof /backup/aof-$(date +\%Y\%m\%d).aof
# 2. Create a snapshot with BGSAVE then backup
redis-cli BGSAVE
sleep 10 # Wait for snapshot to complete
cp /var/lib/redis/dump.rdb /backup/
Recovery Strategy
# If both RDB and AOF exist, Redis prefers AOF
# 1. Stop Redis
redis-cli SHUTDOWN NOSAVE
# 2. Restore file
cp /backup/appendonly.aof /var/lib/redis/
# 3. Start Redis
redis-server
Persistence Monitoring
Checking Persistence Status
# View RDB status
INFO persistence
# Key metrics:
# rdb_last_save_time: Last snapshot time
# rdb_changes_since_last_save: Changes since last snapshot
# aof_enabled: Whether AOF is enabled
# aof_rewrite_in_progress: AOF rewrite in progress
Monitoring Persistence Performance
# Monitor RDB snapshot duration
redis-cli INFO persistence | grep rdb_last_bgsave_time_sec
# Monitor AOF rewrite duration
redis-cli INFO persistence | grep aof_last_rewrite_time_sec
❓ FAQ
📖 Summary
- RDB is a snapshot — saves data at a point in time
- AOF is a log — records every write operation
- RDB has fast recovery and small files but may lose data
- AOF is safer but has larger files and slower recovery
- Hybrid persistence combines the benefits of both (Redis 4.0+)
- Use RDB for caching, AOF or hybrid for data storage
- Regularly back up persistence files and monitor performance
📝 Exercises
- RDB config: Configure automatic RDB snapshot conditions, manually trigger BGSAVE
- AOF config: Enable AOF, test performance differences between sync strategies
- AOF rewrite: Trigger AOF rewrite and observe file size changes
- Data recovery: Back up RDB/AOF files and simulate data recovery
Next Lesson
In the next lesson, we will learn Redis Security, covering security configuration and access control.



