NoSQL · Data models beyond tables
How does Redis eviction work?
Short answer
Redis enforces maxmemory with a policy such as noeviction, allkeys-lru, volatile-lru, allkeys-lfu or volatile-ttl. The policy decides which keys are removed when memory is full.
When memory reaches maxmemory, Redis applies the configured policy to free space. If no key can be evicted, it returns an error for writes. The choice between allkeys and volatile determines whether keys without a TTL are candidates.
maxmemory 4gb
maxmemory-policy allkeys-lru
maxmemory-policy noeviction
maxmemory-policy volatile-lru
maxmemory-policy allkeys-lfu
maxmemory-policy volatile-ttlChoosing a policy
- noeviction: reject writes when full, safest when every key must survive
- allkeys-lru: evict least recently used keys across the whole keyspace
- allkeys-lfu: evict least frequently used keys, better for stable hot sets
- volatile-lru or volatile-ttl: only evict keys with a TTL, protecting persistent data
- random: cheap but unpredictable, rarely the right choice
Redis approximates LRU and LFU by sampling rather than maintaining a perfect ordering. That is why the policy is fast. It also means a recently used key can occasionally be evicted, so the cache must tolerate misses.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What is the difference between LRU and LFU?
- 2Why does Redis approximate LRU?
- 3What happens with maxmemory-policy noeviction when memory is full?
- 4Why separate cache and durable keys?
Say it in the interview
Redis evicts keys when it reaches maxmemory, using a policy like allkeys-lru or volatile-ttl. For a pure cache I use allkeys-lru or lfu, because every key is disposable. For a mixed store I use a volatile policy and only give cache keys a TTL, so durable keys are protected. The eviction is approximate because Redis samples keys instead of keeping exact order, so the application must handle misses. I never run noeviction on a cache unless I want writes to fail at the limit.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live NoSQL mock interview and get scored feedback on depth, clarity and confidence.