LRU Accuracy

Difficulty: Advanced

Question

What is the maxmemory-samples setting, why does Redis approximate LRU and LFU instead of implementing them exactly, and how should you tune it?

Answer

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.

Why Redis approximates

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.

How eviction picks a key

  1. A write command arrives while used_memory is above maxmemory.
  2. Redis samples maxmemory-samples random keys (from all keys, or only keys with a TTL for volatile-* policies).
  3. Each is scored by idle time (LRU), access frequency (LFU) or remaining TTL (volatile-ttl).
  4. Good candidates go into an eviction pool of 16 entries kept across rounds (added in Redis 3.0), which improves accuracy a lot.
  5. Redis evicts the best key from the pool and repeats until memory is below the limit.

The trade-off

maxmemory-samplesAccuracyCPU per eviction
3Rougher; sometimes evicts fairly recent keysLowest
5 (default)Close to LRU for most workloadsLow
10Very close to true LRUHigher

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.

LRU vs LFU

  • LRU (allkeys-lru, volatile-lru) keeps recently touched keys. A one-off scan of many old keys can push out genuinely hot ones.
  • LFU (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.

Gotchas

  • 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.
  • Eviction happens inline while processing writes; heavy eviction shows up as latency. maxmemory-eviction-tenacity (6.2+) controls how much time each cycle spends evicting.
How Redis chooses a key to evict. A small random sample plus a persistent candidate pool approximates true LRU or LFU.
  1. Write arrives above maxmemory: Eviction must free memory first.
  2. Sample N random keys: N = maxmemory-samples, default 5.
  3. Score each candidate: Idle time for LRU, frequency for LFU.
  4. Update the eviction pool: Keeps the 16 best candidates across rounds.
  5. Evict the best candidate: Repeat until under the limit.

Code examples

Configuring and observing eviction

# 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.

Key points

  • maxmemory-samples (default 5) is how many keys Redis samples per eviction decision
  • Higher values approximate true LRU/LFU better but use more CPU; 10 is close to exact
  • Redis approximates to avoid per-key linked-list memory and work on every access
  • An eviction pool of 16 candidates (since 3.0) improves accuracy across rounds
  • Default policy is noeviction; volatile-* policies only evict keys with a TTL

Common follow-up questions

  • When would you choose LFU over LRU for a cache?
  • What happens with volatile-lru when no keys have a TTL?
  • How would you measure whether changing maxmemory-samples helped?
  • What do lfu-log-factor and lfu-decay-time control?

Interview tips

  • Explain why Redis approximates (memory and CPU per key), not just what the setting does
  • Mention the eviction pool; few candidates know it
  • Tie tuning to metrics like hit ratio and evicted_keys rather than guessing

Concepts covered

maxmemory-samples, Approximated LRU, LFU, Eviction pool, maxmemory-policy

More Redis & Caching questions