Difficulty: Advanced
What is the maxmemory-samples setting, why does Redis approximate LRU and LFU instead of implementing them exactly, and how should you tune it?
maxmemory-samples sets how many keys Redis looks at each time it picks a key to evict under an LRU, LFU or TTL-based maxmemory-policy. Redis does not keep a global ordered list of keys; it samples a few random keys and evicts the best candidate. The default of 5 is a good balance, 10 gets very close to true LRU at more CPU cost, and 3 is faster but less accurate.
An exact LRU needs a doubly linked list of every key, moved to the front on every access. That costs two extra pointers per key (16 bytes on 64-bit systems) and extra work on every read, across possibly hundreds of millions of keys. Redis instead stores a 24-bit access clock inside each object header, which costs no extra memory, and approximates.
used_memory is above maxmemory.maxmemory-samples random keys (from all keys, or only keys with a TTL for volatile-* policies).volatile-ttl).| maxmemory-samples | Accuracy | CPU per eviction |
|---|---|---|
| 3 | Rougher; sometimes evicts fairly recent keys | Lowest |
| 5 (default) | Close to LRU for most workloads | Low |
| 10 | Very close to true LRU | Higher |
Raise it if your hit rate matters a lot and you see recently used keys disappearing; keep the default if CPU is tight or the workload is fairly random anyway. Change it at runtime with CONFIG SET maxmemory-samples 10 and compare keyspace_hits / keyspace_misses and evicted_keys from INFO stats before and after.
allkeys-lru, volatile-lru) keeps recently touched keys. A one-off scan of many old keys can push out genuinely hot ones.allkeys-lfu, volatile-lfu, Redis 4.0+) keeps frequently touched keys. It uses a logarithmic 8-bit counter tuned by lfu-log-factor (default 10) and decays it over time with lfu-decay-time (default 1 minute). It often gives better hit rates for caches with stable hot sets, like popular product pages during a sale.Other policies: noeviction (the default, rejects writes with an OOM error), allkeys-random, volatile-random and volatile-ttl.
volatile-* policies only consider keys with a TTL. If none have one, they behave like noeviction.OBJECT IDLETIME key and OBJECT FREQ key (LFU only) let you inspect what Redis sees.maxmemory-eviction-tenacity (6.2+) controls how much time each cycle spends evicting.# redis.conf (or CONFIG SET at runtime)
maxmemory 4gb
maxmemory-policy allkeys-lfu
maxmemory-samples 10
# Observe the effect
redis-cli INFO stats | grep -E 'evicted_keys|keyspace_hits|keyspace_misses'
# Inspect what Redis knows about a key
redis-cli OBJECT FREQ product:991 # LFU policies only
redis-cli CONFIG SET maxmemory-policy allkeys-lru
redis-cli OBJECT IDLETIME product:991 # seconds since last access
Compare the hit ratio (hits divided by hits plus misses) before and after changing samples or policy. OBJECT FREQ errors unless an LFU policy is active.
maxmemory-samples, Approximated LRU, LFU, Eviction pool, maxmemory-policy