Script Valley
Redis: Complete Course
Caching PatternsLesson 3.4

Redis cache eviction policies: LRU, LFU, and allkeys explained

maxmemory config, allkeys-lru, volatile-lru, allkeys-lfu, volatile-lfu, noeviction, allkeys-random, choosing eviction policy

What happens when Redis runs out of memory

Set a memory limit in redis.conf. When that limit is hit, Redis applies an eviction policy to free space for new writes.

# redis.conf
maxmemory 256mb
maxmemory-policy allkeys-lru

Policy reference

noeviction — return an error on write. Use only when data loss is unacceptable and you will handle the error in code.

allkeys-lru — evict the least recently used key across all keys. Best default for general caches.

volatile-lru — LRU but only among keys that have a TTL set. Preserves keys without TTLs.

allkeys-lfu — evict the least frequently used key. Better than LRU for skewed access patterns where some keys are accessed rarely but recently.

allkeys-random — evict a random key. Fast but unpredictable.

volatile-ttl — evict the key with the shortest remaining TTL first.

Choosing a policy

For a pure cache where all data is also in a database: use allkeys-lru. For a mixed store (some keys are persistent, some are cache): use volatile-lru and ensure cached keys always have TTLs. For workloads with hot keys accessed millions of times: use allkeys-lfu.

Up next

Preventing cache stampede with Redis locks and probabilistic early expiration

Sign in to track progress