Video in progress

Strong Junior ⚡ Redis and caching

What happens when Redis memory reaches the maxmemory limit?

It depends on maxmemory-policy. The default is noeviction: new writes are rejected with an OOM error. For a cache you usually set allkeys-lru or allkeys-lfu, so Redis evicts the least-used keys itself.

📖 Answer

The main policies:

Policy What it evicts
noeviction nothing, writes return an error
allkeys-lru the least recently used key among all keys
allkeys-lfu the least frequently used key
volatile-lru / volatile-lfu the same, but only among keys that have a TTL
volatile-ttl the key with the least TTL left
allkeys-random / volatile-random a random key
maxmemory 2gb
maxmemory-policy allkeys-lfu

The choice depends on what lives in Redis. If it's only a cache, use allkeys-*. If one instance holds both a cache and important data without a TTL (a queue, sessions), volatile-* protects the important keys, but once the keys with a TTL run out, it starts returning errors just like noeviction.

Not setting maxmemory at all is also dangerous: Redis eats all the server's RAM and the OS OOM killer kills it. If evicted_keys in INFO stats keeps growing, memory is not enough.