Quick revision
The short answer
Redis has reached maxmemory, and allkeys-lru evicts the least recently used keys of any kind; a session idle for a few minutes while the user reads a product page looks colder than hot product cache entries, so sessions are evicted and users are logged out. Switching to volatile-lru does not help, because sessions have TTLs too. The fix is to separate data by importance: sessions in their own Redis with noeviction and enough capacity, caches elsewhere on allkeys-lru or allkeys-lfu, the session store sized from active sessions times average size with an alert well before maxmemory, and sessions kept small.
What to remember
In this answer, we saw that allkeys-lru at maxmemory evicts the least recently used keys of any kind, and an idle shopper's session looks colder than hot product entries, so it is evicted and the user is logged out. volatile-lru does not help because sessions have TTLs. The fix is to give sessions their own Redis with noeviction and enough memory, keep caches elsewhere on allkeys-lru or allkeys-lfu, size the session store from active sessions and session size with alerts well before maxmemory, and keep sessions small.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Redis has reached maxmemory, and allkeys-lru evicts the least recently used keys of any kind; a session idle for a few minutes while the user reads a product page looks colder than hot product cache entries, so sessions are evicted and users are logged out. Switching to volatile-lru does not help, because sessions have TTLs too. The fix is to separate data by importance: sessions in their own Redis with noeviction and enough capacity, caches elsewhere on allkeys-lru or allkeys-lfu, the session store sized from active sessions times average size with an alert well before maxmemory, and sessions kept small.
Why this question is asked
Sharing one Redis between a cache and a session store looks economical, and it works fine until the first evening the data no longer fits. Then Redis does exactly what it was told, and what it was told was wrong for half the data. Interviewers like this scenario because the symptom, users logged out at peak, is far from the cause, an eviction policy. In this answer, we will recap how maxmemory and eviction work, trace why sessions lose, see why the obvious policy change does not help, and then separate the workloads and size the session store.
1A Quick Recap of maxmemory and Eviction
maxmemory sets how much memory Redis may use for data. When a write would go over it, Redis applies maxmemory-policy. With allkeys-lru, it evicts keys it estimates to be least recently used, chosen from all keys; with allkeys-lfu, the least frequently used; with volatile-lru and volatile-lfu, only keys that have a TTL; and with noeviction it evicts nothing and refuses writes with an OOM error instead. Redis approximates LRU by sampling a few keys (maxmemory-samples, 5 by default) and evicting the oldest of the sample, which is cheap and close enough to true LRU.
INFO stats shows evicted_keys, a running total; when it climbs, Redis is deleting data to make room.
2Why Sessions Lose to Product Entries
During the evening peak the product cache is read thousands of times per second, so its keys are always recently used. A shopper's session is touched only when that shopper makes a request. If they spend four minutes comparing products on a page that is already loaded, their session key has not been touched for four minutes, and in Redis's eyes it is one of the coldest keys in the instance.
So when memory fills up, allkeys-lru keeps evicting sessions. Spring Session finds no session for the cookie on the next request, creates a new empty one, and the user is logged out with an empty checkout. Nothing is broken in the code; the policy is doing exactly its job on data that should never have been evictable.
- Peak traffic fills memoryused_memory reaches maxmemory
- New write needs spaceallkeys-lru samples keys
- Idle session is oldest in sampleProduct keys were read milliseconds ago
- Session key evictedevicted_keys climbs
- Next request finds no sessionUser logged out, checkout lost
5 steps in order. Play them, or step through them one at a time.
3Why volatile-lru Does Not Help
A common first reaction is to switch to volatile-lru, thinking it will protect sessions. It evicts only keys that have a TTL, but every Spring Session key has a TTL, the inactive interval, because that is how sessions expire. So sessions remain candidates, and if the product cache keys have TTLs too, the competition is exactly the same as before. If they do not, volatile-lru would evict only sessions, which is worse.
4The Fix: Separate Data by Importance
Sessions and cache entries have opposite requirements. A cache entry can be thrown away at any moment, because it can be rebuilt from the database. A session cannot, because it holds state the user created. Data with different rules about loss belongs in different Redis deployments, each with the policy that fits.
So we move sessions into their own Redis deployment with maxmemory-policy noeviction and enough memory for the peak, and leave the product cache where it is, on allkeys-lru or, often better for catalogues with a stable set of popular items, allkeys-lfu. Spring Session can point at a different connection factory than the cache, for example by giving the session store its own RedisConnectionFactory bean annotated with @SpringSessionRedisConnectionFactory.
| Deployment | Holds | Policy | When full |
|---|---|---|---|
| Session store | Spring Session keys | noeviction | Writes fail; alert long before |
| Cache | Product and page cache | allkeys-lru or allkeys-lfu | Coldest entries evicted, rebuilt |
5Sizing and Watching the Session Store
With noeviction, a full store does not quietly lose sessions; it rejects writes, which is louder but just as bad. So we size it properly: peak active sessions times average session size, plus headroom. For example, 400,000 concurrent sessions at 8 KB is about 3.2 GB of data; with Redis overhead and room for growth and fork copy-on-write during persistence, we would give it 8 GB. We alert at 70 percent of maxmemory, well before trouble.
We also check the size of sessions themselves. A session holding a full cart object, recently viewed products and search results can be 50 KB instead of 5 KB, which multiplies memory by ten. Keeping only identifiers in the session, such as the cart id rather than the cart, keeps it small and makes the sizing hold.
6Follow-up questions
What do users see when a noeviction session store fills up?
Writes are refused with an error like OOM command not allowed when used memory > 'maxmemory'. Spring Session cannot save the session at the end of the request, so the request fails with a server error, and new logins fail because a new session cannot be created. Reads of existing sessions still work, but any change is lost. That is why the alert must fire well before maxmemory, and why sizing is part of the fix.
Could we keep one deployment and just raise maxmemory?
It would hide the problem until the next growth spurt, and the cache, which grows to fill whatever space it has, would eventually crowd sessions out again. Separating them is what makes the behaviour predictable.
Is allkeys-lfu better than allkeys-lru for the cache?
Often, for a catalogue where some products are steadily popular. LFU keeps frequently used items even if they were not touched in the last few seconds, and evicts one-off entries first. It is worth trying with the hit rate as the measure.
7Conclusion
In this answer, we saw that allkeys-lru at maxmemory evicts the least recently used keys of any kind, and an idle shopper's session looks colder than hot product entries, so it is evicted and the user is logged out. volatile-lru does not help because sessions have TTLs. The fix is to give sessions their own Redis with noeviction and enough memory, keep caches elsewhere on allkeys-lru or allkeys-lfu, size the session store from active sessions and session size with alerts well before maxmemory, and keep sessions small.
Open with
The instance is at maxmemory, so every new write makes Redis evict something, and with allkeys-lru it evicts the least recently used keys of any kind. During the peak, product cache keys are read constantly, while a shopper's session is only touched when they make a request, so a session idle for a few minutes looks colder than product entries and gets evicted.
Then, if they want more
On the next request Spring Session finds nothing, creates a new session, and the user is logged out with their checkout gone. Switching to volatile-lru does not help, because Spring Session keys have TTLs, so they are still candidates. The real fix is to separate data by importance.
I would move sessions into their own Redis deployment with maxmemory-policy noeviction and enough capacity, and leave the product cache on allkeys-lru or allkeys-lfu, where eviction is harmless.
I would size the session store from peak active sessions times the average session size, with headroom, and alert on used memory well before maxmemory, because with noeviction a full store turns into write errors and failed logins. And I would check session size, keeping only identifiers in the session rather than whole objects.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Redis documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "INFO stats shows evicted_keys climbing during the peak, and maxmemory-policy is...": https://scrivio-interview-library.netlify.app/databases/redis-patterns/aa26b97d7b/