Step 1 · The pattern
A cache has a memory limit
Redis keeps everything in RAM. You give it a ceiling, maxmemory. Reads and writes are fast and there is plenty of room.
- read or write
- OK
In short
Eviction under maxmemory: how it works and how it breaks
A cache lives in memory, and memory is finite. Redis lets you set a ceiling with maxmemory, and a policy that decides what happens when a write would go over it. The default policy, noeviction, refuses the write with an out-of-memory error. Reads keep working, so the problem can hide until something needs to store a new session or a fresh value and cannot. For a cache, the usual choice is allkeys-lru or allkeys-lfu: remove a key that has not been used recently, or is used least often, and store the new one. The cache then holds whatever is most valuable right now. Beware the volatile-* policies, which only evict keys that have a TTL: if your keys have no TTL, they behave exactly like noeviction. If the data must not disappear, such as a queue or a balance, it does not belong in an evicting cache.
How this comes up in interviews
"Your Redis cache reaches its memory limit in production. What happens next, and what would you have configured in advance?"
Then they ask
- How do you choose between allkeys-lru, allkeys-lfu and the volatile policies?
- Sessions and cached pages share one Redis instance. What goes wrong under eviction, and what would you change?
- Which metrics tell you the cache is too small, as opposed to badly configured?
A strong answer covers
- With the default policy, noeviction, writes that need more memory fail with an out-of-memory error while reads keep working.
- For a pure cache, use allkeys-lru or allkeys-lfu so the least useful key makes room for the new one.
- The volatile-* policies only evict keys with a TTL. With no such keys they behave like noeviction.
- Data that must not disappear, such as a queue or a balance, does not belong in an evicting cache.
- Without short TTLs or eviction, memory only goes up.
Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.
On one page: the track cheat sheet.
Sources: Redis docs: key eviction ↗ · AWS Builders' Library: Caching challenges and strategies ↗ · Netflix: Announcing EVCache ↗ · Report a problem
Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.