maxmemory and the eight eviction policies

Redis Course · lesson 4 of 19 · 5 min read

Without maxmemory Redis grows until the kernel kills it, and the default policy refuses writes rather than evicting.

Open this lesson in the learning hub

Key points

  • maxmemory defaults to 0, meaning no limit, so an unconfigured Redis will grow until the OOM killer acts.
  • The default maxmemory-policy is noeviction: once full, writes fail with an OOM error and reads keep working.
  • The allkeys-* policies may evict any key; the volatile-* policies only touch keys that carry a TTL.
  • The eight policies are noeviction, allkeys-lru, allkeys-lfu, allkeys-random, volatile-lru, volatile-lfu, volatile-random and volatile-ttl.
  • LRU and LFU are approximate: Redis samples maxmemory-samples keys (default 5) and evicts the worst of that sample.
  • LFU was added in Redis 4.0 and counts access frequency, so a key read once in a burst does not survive a genuinely hot key.

Example

# redis.conf
maxmemory 4gb
maxmemory-policy allkeys-lru
maxmemory-samples 5

$ redis-cli INFO memory
used_memory_human:3.71G
maxmemory_human:4.00G
maxmemory_policy:allkeys-lru

$ redis-cli INFO stats
evicted_keys:184203
keyspace_hits:9812004
keyspace_misses:214880

Set maxmemory explicitly, then pick allkeys-* for a cache and noeviction for anything you cannot lose.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Redis Course course, and every lesson in it is listed on the Redis Course contents page.