How does Redis handle memory management and the maxmemory setting?
How Redis manages RAM with maxmemory and eviction policies like LRU, LFU and volatile-ttl, plus how to monitor usage and avoid out-of-memory errors.
Expected Interview Answer
Redis keeps all data in RAM, and the maxmemory setting caps how much memory it may use; once that limit is hit, Redis applies the configured maxmemory-policy to decide whether to evict keys or reject writes.
Policies include noeviction (reject writes with an error), the LRU family (allkeys-lru, volatile-lru), the LFU family (allkeys-lfu, volatile-lfu), volatile-ttl (evict soonest-to-expire), and random variants. The 'volatile-' policies only evict keys that have a TTL set, while 'allkeys-' policies may evict any key. Redis uses approximate LRU/LFU sampling rather than exact tracking to save memory and CPU, and reports usage via INFO memory and used_memory.
- Predictable memory ceiling prevents host OOM
- Flexible eviction policies for cache vs. store use cases
- Approximate LRU/LFU keeps overhead low
- volatile- policies protect keys without a TTL
- Observability through INFO memory and used_memory metrics
AI Mentor Explanation
Think of a dressing room with a strict kit-bag limit. When it is full and a new bag arrives, a rule decides which old bag goes — maybe the one unused longest, or the one due to leave the tour soonest. maxmemory is that shelf limit and maxmemory-policy is the rule choosing which key gets evicted when Redis is full.
Step-by-Step Explanation
Step 1
Set the ceiling
Configure maxmemory (e.g. 2gb) via redis.conf or CONFIG SET to bound RAM usage.
Step 2
Choose a policy
Set maxmemory-policy — noeviction, allkeys-lru, volatile-lru, allkeys-lfu, volatile-ttl, etc.
Step 3
Approximate tracking
Redis samples a few keys (maxmemory-samples) to approximate LRU/LFU instead of exact ordering.
Step 4
Evict or reject
On hitting the limit, Redis evicts per policy, or returns an OOM error under noeviction.
Step 5
Monitor
Watch used_memory, evicted_keys and mem_fragmentation_ratio via INFO memory.
What Interviewer Expects
- Redis is in-memory and maxmemory caps usage
- Naming the policy families: noeviction, LRU, LFU, volatile-ttl, random
- Difference between allkeys- and volatile- policies
- That LRU/LFU are approximate via sampling
- How to observe memory with INFO memory / used_memory
Common Mistakes
- Thinking Redis evicts automatically without a configured policy (default is noeviction)
- Confusing allkeys- and volatile- scopes
- Assuming eviction is exact LRU rather than sampled approximation
- Ignoring memory fragmentation vs. used_memory
- Setting maxmemory too close to system RAM, risking swap or OOM
Best Answer (HR Friendly)
“Redis stores everything in memory, so the maxmemory setting puts a cap on how much it can use. When that cap is reached, Redis follows a chosen policy to either remove older or less-used data to make room, or to reject new writes, keeping the server stable.”
Code Example
# Cap memory at 2 GB
CONFIG SET maxmemory 2gb
# Evict least-recently-used keys among all keys
CONFIG SET maxmemory-policy allkeys-lru
# Tune eviction sampling accuracy vs. CPU
CONFIG SET maxmemory-samples 10
# Inspect memory and eviction stats
INFO memoryFollow-up Questions
- What is the difference between allkeys-lru and volatile-lru?
- How does LFU differ from LRU and when is it better?
- Why does Redis use approximate rather than exact LRU?
- What happens to writes under the noeviction policy?
- How does memory fragmentation affect used_memory readings?
MCQ Practice
1. What does the maxmemory setting do?
maxmemory defines the upper bound on memory; hitting it triggers the maxmemory-policy.
2. Which policy only evicts keys that have a TTL set?
volatile- policies restrict eviction to keys with an expire set; allkeys- policies may evict any key.
3. What is the default maxmemory-policy?
By default Redis uses noeviction, rejecting writes with an error once maxmemory is reached.
Flash Cards
What triggers Redis eviction? — Reaching the maxmemory limit, after which maxmemory-policy decides what happens.
allkeys- vs volatile- policies? — allkeys- may evict any key; volatile- only evicts keys that have a TTL set.
Is Redis LRU exact? — No — it approximates LRU/LFU by sampling maxmemory-samples keys to save CPU and memory.
Default eviction policy? — noeviction — writes are rejected with an OOM error when the limit is hit.