Introduction
Redis is an in-memory data store that provides optional persistence mechanisms to ensure data durability. By default, Redis keeps all data in RAM, but persistence can be enabled to avoid data loss in case of system failures or restarts.
Redis offers two main types of persistence:
- RDB (Redis Database File) – Snapshot-based persistence.
- AOF (Append-Only File) – Log-based persistence.
A combination of RDB and AOF can be used for an optimal balance between performance and durability.
RDB (Redis Database Snapshot)
How RDB Works
- Periodically saves a snapshot of the dataset to a binary file (
dump.rdb). - Redis automatically restores data from the snapshot when restarted.
- Data is saved only at specific intervals, so recent writes may be lost if Redis crashes.
Configuring RDB Persistence
Modify redis.conf file:
save 900 1 # Save every 15 minutes if at least 1 change is made
save 300 10 # Save every 5 minutes if at least 10 changes are made
save 60 100 # Save every 1 minute if at least 100 changes are madeTo check RDB settings:
CONFIG GET saveTo trigger a manual save:
SAVE # Blocks Redis until save completes
BGSAVE # Runs save in the backgroundPros and Cons of RDB
| Feature | Advantage | Disadvantage |
|---|---|---|
| Performance | Faster, bulk writes | Data loss since last snapshot |
| Disk Usage | Compact file size | Requires full rewrite of dataset |
| Backup | Easy to move & copy | Less frequent data persistence |
| Best for | Regular backups, fast restarts | Not ideal for real-time durability |
AOF (Append-Only File)
How AOF Works
- Logs every write operation (
SET,RPUSH, etc.) toappendonly.aof. - More durable than RDB, with configurable fsync (flush to disk) policies.
- Redis replays the AOF log on restart to recover data.
Configuring AOF Persistence
Enable AOF in redis.conf:
appendonly yes # Enable AOF
appendfsync everysec # Flush to disk every second (balanced approach)To check AOF status:
CONFIG GET appendonlyTo rewrite AOF (optimize file size):
BGREWRITEAOFPros and Cons of AOF
| Feature | Advantage | Disadvantage |
|---|---|---|
| Durability | Minimal data loss | Larger disk space usage |
| Performance | Configurable fsync | Slower than RDB for heavy writes |
| Backup | More real-time recovery | May require AOF rewrites |
| Best for | Real-time persistence | Systems needing lower latency |
RDB vs. AOF: Key Differences
| Feature | RDB (Snapshot) | AOF (Append-Only) |
|---|---|---|
| Data Loss | Possible (last snapshot) | Minimal (depends on fsync setting) |
| Performance | Fast (bulk writes) | Slower (continuous writes) |
| Disk Usage | Compact | Large (logs every command) |
| Recovery Speed | Faster | Slower (replays all operations) |
| Use Case | Backup, fast restarts | Real-time durability |
Using RDB + AOF Together
For optimal performance and durability, use both RDB and AOF:
save 60 100
appendonly yes
appendfsync everysecThis ensures fast snapshot backups (RDB) + real-time persistence (AOF).
Checking Persistence Status
Run the following commands in Redis CLI:
INFO Persistence # Shows RDB and AOF status
CONFIG GET save # Check RDB settings
CONFIG GET appendonly # Check if AOF is enabledConclusion
Redis provides flexible persistence options:
- RDB for periodic snapshots (better for backups and low-latency applications).
- AOF for real-time persistence (better for durability and logging every operation).
- Combining RDB + AOF gives the best of both worlds.