Eviction Policies

Difficulty: Intermediate

Question

What happens when Redis reaches its memory limit? Explain the available eviction policies.

Answer

When used memory crosses the maxmemory limit, Redis applies the configured maxmemory-policy before executing a command that may add data. With the default noeviction, writes that need more memory fail with an OOM command not allowed error while reads still work; with any other policy Redis evicts keys until it is back under the limit. For a pure cache the usual choice is allkeys-lru or allkeys-lfu.

The eight policies

PolicyCandidate keysHow it picks
noevictionNoneRejects writes with an OOM error (default)
allkeys-lruAll keysLeast recently used
allkeys-lfuAll keysLeast frequently used
allkeys-randomAll keysRandom
volatile-lruKeys with a TTLLeast recently used
volatile-lfuKeys with a TTLLeast frequently used
volatile-randomKeys with a TTLRandom
volatile-ttlKeys with a TTLShortest remaining TTL first

The volatile-* policies behave like noeviction if no key has an expiry, which surprises people when Redis suddenly starts rejecting writes.

How eviction actually runs

  • Redis does not keep a perfect LRU list. It samples maxmemory-samples keys (default 5) and evicts the best candidate from a small pool, which approximates true LRU closely while using little memory. Raising samples to 10 gets closer to exact LRU at some CPU cost.
  • Each object stores a 24-bit field that holds either an LRU clock or, under LFU, a logarithmic access counter plus a decay timestamp.
  • Eviction happens inline when commands run, so heavy eviction adds latency. Setting lazyfree-lazy-eviction yes frees evicted values in a background thread.
  • If maxmemory is 0 (the default on 64-bit builds) there is no limit, and the OS may kill Redis instead.

Choosing a policy

  • Pure cache, unknown access pattern: allkeys-lru.
  • Some keys are steadily popular (product catalogue, config): allkeys-lfu keeps them even if they were not touched in the last second.
  • Redis is both cache and persistent store: set TTLs on cache keys only and use volatile-lru, so keys without a TTL (sessions, queues) are never evicted. Running two separate instances is often cleaner.
  • Queues, locks, or data you cannot lose: noeviction, plus alerts on memory usage.

Gotchas

  • maxmemory counts dataset memory, not replication or AOF buffers, and fragmentation means RSS can be well above it. Leave headroom (commonly 20-30 percent of RAM) for fork copy-on-write during BGSAVE.
  • Watch evicted_keys in INFO stats. A steadily rising number on a store that should not evict is an incident waiting to happen.
  • Expiry and eviction are different: expiry deletes keys whose TTL has passed, eviction deletes live keys under memory pressure.
What Redis does on a write at the memory limit. Eviction is triggered by incoming commands, not by a background timer.
  1. Command arrives that may grow memory: For example SET, LPUSH or HSET.
  2. Check used memory against maxmemory: Under the limit, the command runs normally.
  3. Policy is noeviction: Reply with an OOM error and do not run the write.
  4. Otherwise sample candidate keys: maxmemory-samples keys feed an eviction pool.
  5. Evict best candidates until under limit: Optionally freed in a background thread with lazyfree.
  6. Execute the original command: evicted_keys in INFO stats is incremented.

Code examples

Configuring and observing eviction

# redis.conf (or CONFIG SET at runtime)
maxmemory 2gb
maxmemory-policy allkeys-lfu
maxmemory-samples 10
lazyfree-lazy-eviction yes

# At runtime
redis-cli CONFIG SET maxmemory-policy allkeys-lru

# How close are we, and are we evicting?
redis-cli INFO memory | grep -E 'used_memory_human|maxmemory_human|maxmemory_policy'
redis-cli INFO stats  | grep -E 'evicted_keys|keyspace_hits|keyspace_misses'

Pick the policy to match what the instance stores, and monitor evicted_keys alongside the hit ratio to see whether the cache is undersized.

Key points

  • Default policy is noeviction: writes fail with OOM when maxmemory is reached.
  • allkeys-* considers every key; volatile-* only keys that have a TTL.
  • Redis LRU and LFU are approximate, based on sampling maxmemory-samples keys.
  • volatile policies with no TTL keys behave like noeviction.
  • Leave memory headroom for fork copy-on-write, buffers and fragmentation.

Common follow-up questions

  • Why is Redis LRU approximate rather than exact?
  • When would you prefer LFU over LRU?
  • What is the difference between expiry and eviction?
  • How would you size maxmemory on a 16 GB machine that also runs BGSAVE?

Interview tips

  • State the default (noeviction) clearly; many candidates wrongly say LRU is the default.
  • Explain the allkeys vs volatile split with a mixed workload example.
  • Mention monitoring evicted_keys and the hit ratio to show operational thinking.

Concepts covered

maxmemory, maxmemory-policy, allkeys-lru, volatile-ttl, noeviction, Approximated LRU

More Redis & Caching questions