Skip to content

Redis eviction policies — choosing between LRU, LFU, TTL and noeviction

Compare allkeys-lru, allkeys-lfu, volatile-lru, volatile-ttl, and noeviction with the workload signatures each fits and the failure modes when the wrong one is chosen.

Redis offers seven eviction policies that activate when maxmemory is reached, and the default — noeviction — is the one that fits the fewest production workloads. Choosing wrong means either the cache silently throws away hot keys, or the daemon returns OOM command not allowed and every write fails. This article compares each policy against the workload signature it actually fits, the math behind LRU and LFU sampling, and the operational signals that prove the choice was right.

How to verify

# Current policy
redis-cli CONFIG GET maxmemory
redis-cli CONFIG GET maxmemory-policy
redis-cli CONFIG GET maxmemory-samples

# Eviction stats
redis-cli INFO stats | grep evicted_keys
redis-cli INFO memory | grep -E 'used_memory_human|maxmemory_human|mem_fragmentation_ratio'

# Keys with no TTL — relevant for volatile-* policies
redis-cli --scan | head -1000 | xargs -I {} redis-cli TTL {} | grep -c '\-1'

What’s happening

Redis enforces maxmemory per-instance. When a write would push memory above the limit, Redis runs the configured eviction policy to free room. The choice of policy is a function of two questions: which keys are eligible for eviction (all keys, or only keys with a TTL), and how is the victim chosen (LRU approximation, LFU approximation, TTL closest to expiry, random, or do not evict).

The seven policies:

  • noeviction — never evict. Writes return OOM command not allowed when used memory > 'maxmemory'. Use when Redis is a primary store and losing data is unacceptable. Reads still work.
  • allkeys-lru — evict the approximately-least-recently-used key from any. Use when Redis is a cache and access skew matters; recently used keys are hot.
  • allkeys-lfu — evict the approximately-least-frequently-used key from any. Use when access frequency matters more than recency; a key accessed 1000 times a day five days ago is more valuable than one accessed once today.
  • allkeys-random — evict a random key. Use when access patterns are uniform; you do not care which key dies.
  • volatile-lru — evict the approximately-LRU key from those with a TTL. Use when you have a mix of session-style ephemeral data (TTL set) and permanent data (no TTL); evict only the ephemeral.
  • volatile-lfu — same as volatile-lru but LFU.
  • volatile-ttl — evict the key with the shortest remaining TTL. Use when keys with short TTLs are already on their way out anyway.
  • volatile-random — evict a random key from those with a TTL. Use when you want bounded loss but do not care about access patterns.

LRU and LFU in Redis are approximations — sampling-based, not exact, because exact tracking is too expensive. maxmemory-samples (default 5) controls how many candidates are sampled each eviction; higher values are more accurate but use more CPU. 10 is a common production value.

The decision

WorkloadPolicy
Pure cache, hot/cold access skewallkeys-lru
Pure cache, frequency-driven accessallkeys-lfu
Primary store (no loss tolerated)noeviction
Mixed: cache + primary in one instancevolatile-lru with TTL only on cache keys
Session store with TTLs, no other datavolatile-ttl or volatile-lru
Job queue (no eviction allowed)noeviction
Rate-limiter counters with short TTLsvolatile-ttl

The mixed pattern is worth dwelling on: many teams want one Redis to hold both ephemeral session caches (with TTL) and durable lookup tables (no TTL). volatile-lru makes Redis evict from the cache side while protecting the durable tables — but only if the team is rigorous about setting TTLs on cache keys.

The procedure

  1. Set maxmemory and the policy. Pick a value with headroom for forks, COW, and TLS buffers — 70% of physical RAM is a reasonable starting point:

    redis-cli CONFIG SET maxmemory 4gb
    redis-cli CONFIG SET maxmemory-policy allkeys-lru
    redis-cli CONFIG SET maxmemory-samples 10

    Persist by editing /etc/redis/redis.conf:

    maxmemory 4gb
    maxmemory-policy allkeys-lru
    maxmemory-samples 10
  2. Verify keys without TTL if you chose a volatile-* policy. A common failure mode is forgetting to TTL some cache keys, which then become un-evictable:

    redis-cli --scan | while read k; do
      ttl=$(redis-cli TTL "$k")
      [ "$ttl" = "-1" ] && echo "no-ttl: $k"
    done | head -20
  3. Monitor evicted_keys as the canonical signal:

    redis-cli INFO stats | grep evicted_keys

    A steady-state eviction rate is fine on a cache; a sudden spike means working-set growth or a memory-policy mismatch.

  4. Watch the OOM error rate for noeviction instances:

    redis-cli INFO stats | grep -E 'rejected_connections|total_writes_processed'
    redis-cli LATENCY LATEST
  5. For a Redis Cluster, eviction is per-shard. A hot shard evicts while cold shards are nowhere near the limit. Watch eviction per node, not just cluster-wide.

Common pitfalls

  • noeviction on a cache means writes fail after the working set grows past maxmemory. The application sees opaque errors; users see broken pages. Pick an LRU policy on caches.
  • allkeys-lru on a primary store evicts important keys when memory fills — silently. Pick noeviction and size the instance so it never fills.
  • volatile-* policies on a database where no keys have TTL is identical to noeviction from Redis’s perspective — eviction never has eligible candidates. Either set TTLs or switch to allkeys-*.
  • maxmemory-samples 5 (default) on an lru or lfu policy gives noticeably less accurate evictions than 10 — production caches almost always want 10.
  • The COW memory required during BGSAVE forks can push the daemon into eviction during a snapshot. Leave 30%+ headroom or use appendonly yes with aof-use-rdb-preamble no to avoid forks for snapshots.

In the engagements we run, every Redis instance comes up with a documented maxmemory and policy choice — and an alert that fires when evicted_keys rate deviates from baseline by more than 2x for 10 minutes. We operate that under Managed Operations — and for the sizing math behind maxmemory itself see memory tuning.