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:

Memory Data → Persistence → Disk File
Disk File → Recovery → Memory Data

Redis provides two persistence methods:

RDB Persistence

What is RDB?

RDB (Redis Database) saves data at a specific point in time as a binary snapshot file.

Features:

RDB Configuration

Configure in redis.conf:

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

REDIS
# 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
⚠️ Note: SAVE blocks Redis until the snapshot is complete. BGSAVE runs in the background and does not block.

RDB File Location

BASH
# 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:

BASH
# 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:

Cons:

AOF Persistence

What is AOF?

AOF (Append Only File) records every write command. On recovery, Redis re-executes those commands.

Features:

AOF Configuration

CONF
# 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
💡 Recommendation: appendfsync everysec — balances performance and safety.

AOF Rewrite

The AOF file grows continuously. Rewrite compresses it:

REDIS
# Manually trigger AOF rewrite
BGREWRITEAOF
Background append only file rewriting started

Rewrite principle:

Rewrite trigger conditions:

CONF
# 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:

BASH
# 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

BASH
# Check AOF file
redis-check-aof appendonly.aof

# Repair AOF file
redis-check-aof --fix appendonly.aof

AOF Pros and Cons

Pros:

Cons:

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

CONF
# Enable AOF
appendonly yes

# Enable hybrid persistence
aof-use-rdb-preamble yes

Hybrid Persistence Principle

During AOF rewrite:

  1. First write current data in RDB format (fast, compact)
  2. Then append new write operations in AOF format
AOF file structure:
[RDB format snapshot data] + [AOF format incremental commands]

Hybrid Persistence Advantages

💡 Recommendation: Redis 4.0+ should use hybrid persistence for both performance and safety.

Persistence Best Practices

Scenario 1: Cache Only

CONF
# Caching may not need persistence, or just RDB
save 900 1
appendonly no

Scenario 2: Data Storage

CONF
# Data storage uses AOF or hybrid persistence
save 900 1
appendonly yes
appendfsync everysec
aof-use-rdb-preamble yes

Scenario 3: High Availability

CONF
# Enable both RDB and AOF
save 900 1
save 300 10
appendonly yes
appendfsync everysec
aof-use-rdb-preamble yes

Backup Strategy

BASH
# 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

BASH
# 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

REDIS
# 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

BASH
# 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

Q Can RDB and AOF be enabled at the same time?
A Yes. Redis uses both methods and prefers AOF for recovery.
Q How do I choose a persistence method?
A For caching, use RDB or no persistence. For data storage, use AOF or hybrid persistence.
Q What if the AOF file gets too large?
A Use BGREWRITEAOF or configure automatic rewrite conditions.
Q Does persistence affect performance?
A There is some impact. RDB has a small impact (background snapshot), AOF has a moderate impact (frequent fsync).
Q How do I minimize data loss?
A Use AOF with appendfsync everysec — at most 1 second of data loss.

📖 Summary

📝 Exercises

  1. RDB config: Configure automatic RDB snapshot conditions, manually trigger BGSAVE
  2. AOF config: Enable AOF, test performance differences between sync strategies
  3. AOF rewrite: Trigger AOF rewrite and observe file size changes
  4. 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.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏