Back to Knowledge Hub

    How Does Redis Memory Eviction Work?

    Redis
    Memory Management
    Eviction Policies
    Performance

    What is Memory Eviction?

    Redis memory eviction automatically removes data when memory usage hits its limit - much like a library clearing out old books to make room for new ones. This keeps the service running smoothly and prevents out-of-memory errors.

    Core Functions

    1. Prevent Memory Overflow: Sets memory limits via maxmemory (unlimited by default)
    2. Maintain Write Capability: Prevents the system from entering a read-only state
    3. Smart Data Selection: Keeps the most valuable data based on usage patterns

    8 Eviction Strategies Explained

    Strategy Reference Table (Production Selection Guide)

    Strategy NameScopeAlgorithmUse CasesVersion
    noevictionNoneReject writesData loss intolerant2.0+
    allkeys-lruAll keysApprox. LRUGeneral caching (recommended)2.8+
    volatile-lruExpiring keysApprox. LRUMixed persistence2.8+
    allkeys-randomAll keysRandom selectionUniform access2.0+
    volatile-randomExpiring keysRandom selectionTemporary storage2.0+
    volatile-ttlExpiring keysTTL orderingTime-sensitive data2.0+
    allkeys-lfuAll keysApprox. LFUHot spot data4.0+
    volatile-lfuExpiring keysApprox. LFUHot temporary data4.0+

    Core Algorithm Implementation

    1. Approximate LRU Algorithm

    Interview Focus: Understanding Redis's probabilistic sampling for efficient LRU

    // Redis source snippet (evict.c)
    #define EVICTION_SAMPLES_ARRAY_SIZE 16
    unsigned long estimateObjectIdleTime(robj *o) {
        return LRU_CLOCK() - o->lru;
    }
    
    void evictionPoolPopulate(dict *sampledict, struct evictionPoolEntry *pool) {
        // Sample 5 keys into candidate pool
        for (int i = 0; i < EVICTION_SAMPLES_ARRAY_SIZE; i++) {
            // Select key with maximum idle time
            if (bestkey->lru > pool[EVPOOL_SIZE-1].idle) {
                evictionPoolPopulateItem(pool, bestkey);
            }
        }
    }
    

    Algorithm Optimization:

    • Default sample size: 5 (configurable)
    • Fixed-size candidate pool (16 entries)
    • Evicts least recently used key from pool

    Additional Notes:

    • Redis combines global LRU clock with probabilistic sampling to avoid full key scan
    • Interview focus: Understanding candidate pool mechanism and its performance benefits

    2. Approximate LFU Algorithm

    Implementation Details:

    • Uses Morris counter with probabilistic decay
    • Logarithmic frequency storage (0-255)
    • Decay factor configuration (lfu-decay-time)
    # Check key's LFU stats
    redis-cli object freq key1
    

    Additional Notes:

    • Morris counter uses logarithmic growth to prevent counter saturation
    • Interview focus: How LFU better captures hot spot data compared to traditional counters

    Memory Defragmentation

    Trigger Conditions and Configuration:

    # Enable active defragmentation
    activedefrag yes
    
    # Ignore small fragmentation areas (avoid unnecessary defrag)
    active-defrag-ignore-bytes 100mb
    
    # Trigger when used_memory_rss significantly exceeds used_memory
    active-defrag-threshold-lower 10
    

    Defragmentation Process: Here's how Redis manages memory fragmentation:

    1. Fragment Detection: Periodically scans memory to evaluate used_memory_rss vs used_memory ratio
    2. Candidate Selection: Identifies high-fragmentation areas for data reorganization
    3. Data Reorganization: Copies valid data to contiguous new memory space
    4. Memory Reclamation: Gradually releases old, fragmented memory blocks
    5. Progressive Execution: Performs defragmentation incrementally to prevent blocking

    Production Configuration Guide

    E-commerce Flash Sale Example

    # Memory limit (set to 3/4 of physical memory)
    maxmemory 12gb
    
    # Use LFU to retain hot product data
    maxmemory-policy allkeys-lfu
    
    # Adjust LFU decay period (1 hour)
    lfu-log-factor 10
    lfu-decay-time 3600
    
    # Enable defragmentation
    activedefrag yes
    

    Monitoring Metrics

    # Memory metrics
    redis-cli info memory | grep -E "used_memory|maxmemory|mem_fragmentation_ratio"
    
    # Eviction stats
    redis-cli info stats | grep evicted_keys
    
    # Defrag status
    redis-cli info memory | grep defrag
    

    Common Issues and Solutions

    1. Memory Not Released

    Symptom: Reached maxmemory but eviction not triggered
    Troubleshooting Steps:

    # Verify policy isn't noeviction
    CONFIG GET maxmemory-policy
    
    # Check key expiration (-1 means no expiry)
    TTL mykey
    
    # Analyze memory distribution
    redis-cli --bigkeys
    

    2. LFU Hot Spot Failure

    Symptom: Hot data getting unexpectedly evicted
    Troubleshooting Steps:

    # Increase counter precision (lower value = higher precision)
    lfu-log-factor 10
    
    # Accelerate frequency decay (minutes)
    lfu-decay-time 1800
    

    3. High Memory Fragmentation

    Symptom: System refuses new writes despite low memory usage
    Troubleshooting Steps:

    # Manual emergency defrag
    redis-cli memory purge
    
    # Adjust defrag intensity
    CONFIG SET active-defrag-cycle-min 5
    CONFIG SET active-defrag-cycle-max 10
    

    Performance Tuning Tips

    1. Capacity Planning: Keep memory usage under 70%
    2. Key Design: Keep objects under 1MB
    3. Version Upgrade: Use Redis 4.0+ for better eviction control
    4. Hybrid Storage: Move cold data to disk storage

    Related Resources: