Documentation Index
Fetch the complete documentation index at: https://mintlify.com/redis/redis/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Redis provides two primary persistence mechanisms that can be used independently or together:- RDB (Redis Database): Point-in-time snapshots
- AOF (Append-Only File): Command logging for durability
RDB Persistence
Snapshot Format
RDB files store a binary snapshot of the dataset at a specific point in time. The format includes:- Magic String: RDB format identifier
- Version: RDB format version (current:
RDB_VERSION) - Encoded Data: Compressed objects and metadata
- Checksum: CRC64 for corruption detection
- EOF Marker: 40-byte marker (
RDB_EOF_MARK_SIZE)
Return Values
Fromserver.h:360-363:
Creating Snapshots
Background Save (BGSAVE)
Redis forks a child process to write RDB without blocking:- Parent forks child process
- Child writes RDB to temporary file
- Child atomically renames temp file on success
- Parent receives child exit status
- RDB file becomes available
Fork uses copy-on-write (COW), so memory is only duplicated when modified during the save.
Synchronous Save (SAVE)
Blocking save that stops all client operations:- Used during shutdown with
stop-writes-on-bgsave-error no - Guaranteed write before process exit
- Not recommended for production use during normal operations
Configuration
Fromredis.conf:441-462:
Compression
RDB uses LZF compression fromrdb.c:364-380:
Integer Encoding
Strings representing integers are encoded efficiently:Replication and RDB
Fromreplication.c:59-60:
AOF Persistence
AOF States
Fromserver.h:346-349:
How AOF Works
AOF logs every write command in RESP format:- Command Execution: Client command is executed
- Buffer Append: Command written to
server.aof_buf - Flush: Buffer written to AOF file
- Fsync: File synchronized to disk (policy dependent)
Multi-File AOF (Redis 7.0+)
AOF uses multiple files managed by a manifest: File Types:- BASE: RDB snapshot from last rewrite (
*.base.rdbor*.base.aof) - INCR: Incremental changes since last rewrite (
*.incr.aof) - HISTORY: Previous BASE/INCR files marked for deletion (
*.incr.aof,*.base.*)
aof.c:48-71:
Fsync Policies
Fromserver.h:633-636:
redis.conf:
| Policy | Durability | Performance | Data Loss Risk |
|---|---|---|---|
always | Highest | Lowest | None (on ACK) |
everysec | Good | Good | ~1 second |
no | Lowest | Highest | Depends on OS |
With
appendfsync always, Redis guarantees data is on disk before acknowledging writes, but this significantly impacts throughput.AOF Rewrite
AOF grows indefinitely, so Redis can rewrite it to minimize size: Rewrite Process:- Fork child process
- Child generates new BASE file from memory
- Parent accumulates new commands in INCR file
- Child completes, parent updates manifest
- Old files marked as HISTORY and deleted
AOF Loading
Fromaof.c:351-358:
- Load BASE file if present
- Load INCR files in sequence order
- Update
master_repl_offsetfrom final INCR - Mark server as ready
Write Buffering
Fromaof.c:1147-1355:
AOF writes are buffered before flush:
- Reused when small (less than 4000 bytes after flush)
- Reallocated when larger to avoid memory fragmentation
Background Fsync
Withappendfsync everysec, fsync happens in background:
Hybrid Persistence
Using Both RDB and AOF
Recommended Configuration:- If AOF exists, load from AOF (more complete)
- Otherwise, load from RDB
- If neither exists, start with empty dataset
RDB Preamble in AOF
Fromaof.c:448-449:
Performance Considerations
RDB Performance
- Fork Time: Proportional to memory size (typically milliseconds)
- COW Overhead: Modified pages duplicated during save
- Disk I/O: Sequential writes, very efficient
- CPU: Compression uses CPU but reduces I/O
AOF Performance
- Write Amplification: Every command appended to file
- Fsync Cost: Depends on policy and disk speed
- Rewrite Cost: Similar to RDB BGSAVE
- Disk Usage: Grows until rewrite
Optimization
Fromredis.conf:722:
Recovery and Durability
Crash Recovery
With AOF only:- Replay all commands from AOF
- Loss: Commands since last fsync (0 to N seconds based on policy)
- Load last snapshot
- Loss: All changes since last save
- Load from AOF (more complete)
- Loss: Minimal (based on fsync policy)
Corruption Handling
RDB Corruption:The
redis-check-aof --fix command truncates AOF at the first corruption, potentially losing data. Always backup first.