Redis Persistence: RDB vs AOF
Fast recall
Quick revision
Core answer
Understanding of Redis durability trade-offs and how they affect data loss risk and restart performance.
Assuming Redis is purely in-memory with no persistence, or not knowing that enabling both RDB and AOF is a common and valid production configuration.
Mention AOF rewrite (BGREWRITEAOF) to compact the AOF file, the appendfsync options (always/everysec/no), and Redis 7's RDB-AOF hybrid format.
Interview-ready guide
Interview answer
Redis is an in-memory store but supports two persistence mechanisms that determine how much data you'd lose on a crash: RDB snapshots and AOF (Append Only File) logging.
RDB — point-in-time snapshots
- RDB forks a child process to write the current dataset to a binary snapshot file at configured intervals —
save 900 1means: save if at least 1 key changed in the last 900 seconds - Because it's a background fork, RDB snapshots don't block the main Redis process (the fork is fast; the actual write happens in the child)
- The downside is staleness: if Redis crashes between snapshots, all writes since the last snapshot are lost — with a 15-minute interval, you can lose up to 15 minutes of data
AOF — append-only log of every write command
- AOF logs every write command to a file in the order it was received —
SET user:1 name Alice,EXPIRE user:1 3600are appended as text; on restart, Redis replays the log to reconstruct the dataset - AOF with
fsync: everysecflushes the log to disk every second — at most 1 second of data loss on crash, at the cost of slightly higher write latency - AOF with
fsync: alwaysflushes on every write — zero data loss, but much slower; only appropriate for applications with very strict durability requirements
Combining both
- The recommended production configuration uses both: AOF for durability (minimal data loss) and RDB for faster restarts — loading a binary snapshot is much faster than replaying millions of AOF commands
- AOF files grow without bound; Redis's AOF rewrite compacts them by rewriting the current state rather than the full command history:
BGREWRITEAOFor automatic rewrite when the file exceeds a configured size
For caching use cases where the cache can be rebuilt from the database on restart: disable persistence entirely and accept the cache cold-start penalty — it simplifies operations significantly.
Redis Persistence: RDB vs AOF
Direct Answer
RDB (Redis Database) takes point-in-time snapshots of the dataset at configurable intervals, producing compact binary files ideal for backups and fast restarts. AOF (Append-Only File) logs every write operation sequentially, providing much finer-grained durability — down to every second or every command. RDB risks losing minutes of data on crash; AOF risks losing at most one second. Most production deployments enable both: RDB for fast restarts and AOF for durability.
Code Example
Redis configuration for enabling both persistence modes:
# redis.conf — RDB configuration
save 900 1 # Snapshot if ≥1 key changed in 900 seconds
save 300 10 # Snapshot if ≥10 keys changed in 300 seconds
save 60 10000 # Snapshot if ≥10,000 keys changed in 60 seconds
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
# AOF configuration
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec # Best balance of safety and performance
auto-aof-rewrite-percentage 100 # Rewrite when AOF is 2× last rewrite size
auto-aof-rewrite-min-size 64mb # Don't rewrite tiny AOFs
aof-use-rdb-preamble yes # Redis 7 hybrid format — fast load + AOF tail
# Memory safety during forks
maxmemory 6gb
maxmemory-policy allkeys-lru
lazyfree-lazy-eviction yes
Spring Boot application.yml does not configure Redis server persistence directly — these settings live in redis.conf on the server. However, you can verify persistence status programmatically:
@Component
public class RedisHealthCheck {
@Autowired
private StringRedisTemplate redis;
public Map<String, String> getPersistenceInfo() {
Properties info = redis.getConnectionFactory()
.getConnection().serverCommands().info("persistence");
return Map.of(
"rdb_last_save_time", info.getProperty("rdb_last_save_time"),
"aof_enabled", info.getProperty("aof_enabled"),
"aof_last_rewrite_status", info.getProperty("aof_last_rewrite_status")
);
}
}
Common Mistakes & Pitfalls
Assuming Redis is purely in-memory with no persistence, or not knowing that enabling both RDB and AOF is a common and valid production configuration.
Check your understanding
Answer these without looking back at the article.
- What is the BGREWRITEAOF command and when should you trigger it?
- How does Redis 7's hybrid persistence format work?
- If Redis restarts and both RDB and AOF files exist, which one does Redis use?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.