Lesson 14 / 20

Memory Limits & Eviction Policies

Cap Redis memory with maxmemory and choose an eviction policy such as allkeys-lru so a full cache degrades gracefully.

maxmemory and eviction

Redis lives in RAM, so set a limit with maxmemory. When the limit is reached, the maxmemory-policy decides what happens: noeviction rejects writes, allkeys-lru removes the least recently used keys, and volatile-ttl removes keys with the soonest expiry.

Set the limit and policy in redis.conf or at runtime.

redis-cli CONFIG SET maxmemory 256mb
redis-cli CONFIG SET maxmemory-policy allkeys-lru
redis-cli INFO memory

For a pure cache use allkeys-lru. If Redis also holds data you cannot rebuild, such as queues or sessions, avoid evicting it and size memory with headroom.

Quick check

Quick check: Which policy evicts the least recently used keys across the whole keyspace?

  • noeviction
  • allkeys-lru
  • volatile-ttl
  • allkeys-random
Answer

allkeys-lru — allkeys-lru removes the least recently used keys when maxmemory is reached.