Back to Knowledge Hub

    How Does Redis AOF Persistence Work?

    Redis
    Persistence
    Data Safety
    Append-only Log

    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.

    AOF Workflow

    Core AOF Mechanisms

    1. Log Appending Process

    Command Execution Flow:

    1. Receive Command: Client sends write request
    2. Memory Execution: Main process executes command and modifies data
    3. Log Appending:
      • Write command to AOF buffer
      • Sync to disk file per configured policy
    4. 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:

    1. Main process forks rewrite child
    2. Child scans database to build new AOF
    3. Main process continues:
      • Appends new commands to original AOF buffer
      • Simultaneously writes to rewrite buffer
    4. Post-rewrite:
      • Main process appends rewrite buffer to new file
      • Atomically replaces old AOF file

    3. Sync Strategies

    Redis offers three data sync strategies:

    StrategySync MethodData SafetyPerformance
    alwaysSync every commandHighestWorst
    everysecBatch sync every secondModerateBetter
    noOS-controlled syncLowestBest
    # 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:

    AspectOriginal AOFRewritten AOF
    ContentOperation logMemory snapshot
    SizePotentially hugeMinimal dataset
    RecoveryReplay all commandsDirect state load
    Expired DataRetains expiredAuto-purge
    CompatibilityAll versions7.0+ hybrid format

    Hybrid Format Advantages:

    1. Starts with a fast-loading snapshot
    2. Keeps detailed logs of recent changes
    3. 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

    FeatureAOFRDB
    Data Integrity≤1 second lossMinutes potential loss
    Recovery SpeedSlow (replay needed)Fast (direct load)
    File SizeLarger (optimizable)Smaller (compressed)
    Disaster RecoveryPer-command recoveryFull snapshot
    Performance ImpactWrite throughputMemory/CPU impact
    Use CasesFinancial transactionsDisaster 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: