How Does Redis AOF Persistence Work?
What is AOF Persistence?
AOF (Append Only File) is Redis' log-style persistence mechanism that records all write operations. Like a database transaction log, it enables second-level data recovery.
Core AOF Mechanisms
1. Log Appending Process
Command Execution Flow:
- Receive Command: Client sends write request
- Memory Execution: Main process executes command and modifies data
- Log Appending:
- Write command to AOF buffer
- Sync to disk file per configured policy
- Return Response: Main process returns result to client
# Check AOF status
redis-cli info persistence | grep aof_enabled
2. Rewrite Mechanism
AOF rewrite optimizes storage efficiency by rebuilding a minimal operation log.
Rewrite Triggers:
# Trigger when current AOF size exceeds 100% of previous rewrite size
auto-aof-rewrite-percentage 100
# Minimum AOF size required for rewrite
auto-aof-rewrite-min-size 64mb
Rewrite Process:
- Main process forks rewrite child
- Child scans database to build new AOF
- Main process continues:
- Appends new commands to original AOF buffer
- Simultaneously writes to rewrite buffer
- Post-rewrite:
- Main process appends rewrite buffer to new file
- Atomically replaces old AOF file
3. Sync Strategies
Redis offers three data sync strategies:
Strategy | Sync Method | Data Safety | Performance |
---|---|---|---|
always | Sync every command | Highest | Worst |
everysec | Batch sync every second | Moderate | Better |
no | OS-controlled sync | Lowest | Best |
# Configure sync strategy
appendfsync everysec
AOF File Structure
1. Original Command Format (RESP Protocol)
Interview Focus: Understand Redis Serialization Protocol
*3 # Number of command arguments
$3 # First argument length (3 bytes)
SET # Command name
$5 # Key length (5 bytes)
stock # Key name
$3 # Value length (3 bytes)
100 # Value
*2
$4
INCR
$5
stock
Key Characteristics:
- Uses Redis Serialization Protocol (RESP)
- Appends commands in execution order
- Retains expired data operations
- Supports three formats:
- Native AOF
- RDB-AOF hybrid (7.0+ default)
- Legacy compatible format
2. Rewritten Format (Memory Snapshot)
Interview Focus: Understand log compaction
SELECT 0 # Select database 0
SET stock "150" # Record final value
HSET user:1001 name "John" # Merge multiple operations
Optimization Analysis:
Aspect | Original AOF | Rewritten AOF |
---|---|---|
Content | Operation log | Memory snapshot |
Size | Potentially huge | Minimal dataset |
Recovery | Replay all commands | Direct state load |
Expired Data | Retains expired | Auto-purge |
Compatibility | All versions | 7.0+ hybrid format |
Hybrid Format Advantages:
- Starts with a fast-loading snapshot
- Keeps detailed logs of recent changes
- Gets you the best of both worlds - speed and safety
Key Configurations
1. Basic Configuration
# Enable AOF
appendonly yes
# AOF filename
appendfilename "appendonly.aof"
# Allow loading truncated AOF
aof-load-truncated yes
2. Performance Configuration
# Disable fsync during rewrite
no-appendfsync-on-rewrite yes
# Enable hybrid persistence (7.0+)
aof-use-rdb-preamble yes
Best Practices
1. Production Configuration
# Balance safety and performance
appendfsync everysec
# Auto-rewrite thresholds
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 32gb
# Enable hybrid mode
aof-use-rdb-preamble yes
2. Monitoring
# Monitor AOF status
redis-cli info persistence | grep -E "aof_current_size|aof_buffer_length"
# Track rewrite progress
watch -n 1 "redis-cli info persistence | grep aof_rewrite_in_progress"
AOF vs RDB Comparison
Feature | AOF | RDB |
---|---|---|
Data Integrity | ≤1 second loss | Minutes potential loss |
Recovery Speed | Slow (replay needed) | Fast (direct load) |
File Size | Larger (optimizable) | Smaller (compressed) |
Disaster Recovery | Per-command recovery | Full snapshot |
Performance Impact | Write throughput | Memory/CPU impact |
Use Cases | Financial transactions | Disaster recovery |
Common Issues & Solutions
1. Oversized AOF
Symptoms:
- Recovery time >1 hour
- Disk space alerts
Solutions:
# Manual rewrite
redis-cli BGREWRITEAOF
# Enable hybrid mode
CONFIG SET aof-use-rdb-preamble yes
2. Write Performance Degradation
Diagnosis:
# Check latency
redis-cli --latency -i 1
# Verify disk performance
iostat -x 1
Optimizations:
# Use SSD storage
# Tune kernel params
vm.dirty_background_ratio = 5
vm.dirty_ratio = 10
3. Data Corruption Recovery
Steps:
# Repair AOF file
redis-check-aof --fix appendonly.aof
# Specify recovery version
redis-server --appendonly yes --appendfilename appendonly-20240315.aof
Conclusion
AOF persistence provides granular data protection through operation logging. Combined with hybrid mode, it offers both fast recovery and precise data safety. Implement appropriate sync strategies and monitoring in production environments.
Related Resources: