Redis Evicted Sessions Before Writes Failed: Fixes
In this article
When Redis evicts sessions before writes fail, the root cause is almost always a misconfigured maxmemory policy combined with an eviction policy that targets keys without expiration (like allkeys-lru), causing the server to silently drop session keys under memory pressure. The fix is to set maxmemory-policy volatile-lru (or volatile-ttl) so Redis only evicts keys that have an expiration set, and to ensure all session keys carry a TTL. This article explains why eviction happens, how to diagnose it, and the exact configuration steps to prevent it.
Background: Why Redis Evicts Keys
Redis is an in-memory data store, so it must respect a memory limit defined by maxmemory. When the dataset grows beyond that limit, Redis must free space. The eviction policy (maxmemory-policy) decides which keys to remove. The default policy is noeviction, which returns an error on writes that would exceed the limit—but many production setups change this to an eviction policy to keep the server responsive. If the policy is allkeys-lru or allkeys-random, Redis can evict any key, including active sessions that have no expiration. That’s exactly the scenario where sessions vanish before writes fail: the server evicts session keys to make room, and the application only later sees write errors when the memory is full again.
How Eviction Policies Differ
Redis offers several eviction policies, grouped by whether they consider only keys with an expiration set (volatile) or all keys. The most common are:
noeviction– returns errors on writes when memory is full; no eviction.allkeys-lru– evicts the least recently used key among all keys, regardless of TTL.allkeys-random– evicts a random key from all keys.volatile-lru– evicts the least recently used key among keys that have an expiration set.volatile-ttl– evicts the key with the shortest remaining TTL among volatile keys.volatile-random– evicts a random volatile key.
If your session keys have a TTL (e.g., 30 minutes), volatile-lru or volatile-ttl will only evict keys that are already meant to expire, which is safer than evicting permanent keys. However, if your session keys do not have a TTL, even volatile-lru will not evict them—it will fall back to noeviction behavior and return errors on writes. That’s why setting a TTL on sessions is critical.
Diagnosing the Problem
To confirm that eviction is the cause of lost sessions, check Redis’s INFO stats for the evicted_keys counter. If it’s increasing, eviction is happening. Also check maxmemory_policy in CONFIG GET maxmemory-policy. If it’s allkeys-lru or allkeys-random, that’s a red flag. Additionally, monitor used_memory against maxmemory—if they’re close, eviction is likely. Another sign: your application logs show “OOM command not allowed when used memory > 'maxmemory'” errors, which indicate that the server has reached the limit and the policy is noeviction or that no volatile keys are available to evict.
How to Prevent Eviction of Sessions
Prevention involves three steps: set a TTL on all session keys, choose a volatile eviction policy, and monitor memory usage.
- Set a TTL on every session key. When you store a session, use
SETEXorEXPIREto give it a reasonable lifetime (e.g., 30 minutes). This marks it as volatile, making it eligible for volatile eviction policies. - Change the eviction policy to
volatile-lruorvolatile-ttl. Set this inredis.confor viaCONFIG SET maxmemory-policy volatile-lru. This ensures that when memory is full, Redis only evicts keys that are already scheduled to expire, not active sessions. - Monitor and tune
maxmemory. Setmaxmemoryto a value that leaves headroom for peak usage. UseINFO memoryto trackused_memoryandmaxmemory. If you consistently approach the limit, increase memory or optimize session storage (e.g., use smaller keys, compress values).
Additionally, consider using Redis’s maxmemory-samples setting (default 5) to control the accuracy of LRU eviction. Higher samples improve accuracy but use more CPU.
Common Mistakes and Misconceptions
A frequent misconception is that setting maxmemory-policy allkeys-lru is safe because it evicts the least recently used keys—but that includes sessions that are actively used if they haven’t been accessed recently enough. Another mistake is assuming that volatile-lru will evict keys without TTL. It won’t; it only considers volatile keys. If no volatile keys exist, Redis behaves like noeviction and returns errors on writes. Also, some developers forget to set maxmemory at all, which defaults to 0 (unlimited) on 64-bit systems, but on 32-bit systems it defaults to 3GB. If you don’t set a limit, eviction never triggers, but you risk swapping or OOM kills from the OS.
Alternatives to Eviction
If eviction is unacceptable for your use case, consider these alternatives:
- Use
noevictionand handle write errors gracefully. The application can retry or return a friendly error, but sessions won’t be silently dropped. - Use a separate Redis instance or database for sessions. This isolates session data from other cached data, so eviction in the cache doesn’t affect sessions.
- Use Redis with a disk-backed store like Redis Enterprise or Redis on Flash. These allow data to spill to disk, but they’re commercial offerings.
- Use a different session store, such as a relational database or Memcached (which also evicts, but with LRU). Memcached has a similar issue, so the same TTL and policy considerations apply.
Frequently Asked Questions
Why did Redis evict my session keys even though they have a TTL?
If your policy is allkeys-lru or allkeys-random, Redis ignores TTLs and can evict any key. Switch to volatile-lru or volatile-ttl to restrict eviction to keys with expiration. Also verify that the TTL is actually set—if you use SET without EXPIRE, the key is not volatile.
What is the difference between volatile-lru and volatile-ttl?
volatile-lru evicts the least recently used volatile key, while volatile-ttl evicts the volatile key with the shortest remaining time-to-live. For sessions, volatile-ttl might evict sessions that are about to expire anyway, which is often preferable. volatile-lru is better if you want to keep recently active sessions.
Can I set a TTL on an existing key without losing the value?
Yes, use the EXPIRE command: EXPIRE session_key 1800 sets a 30-minute TTL. The value remains unchanged. If you’re using a Redis client, most provide a method to set expiration on an existing key.
What happens if maxmemory is reached and the policy is noeviction?
Redis returns an error for any write command that would increase memory usage (e.g., SET, LPUSH, HSET). Reads still work. Your application will see errors like “OOM command not allowed when used memory > 'maxmemory'”. You can catch these errors and handle them gracefully, but sessions won’t be evicted.
How do I monitor evictions in real time?
Use INFO stats and watch the evicted_keys field. You can also use redis-cli --stat for a live view, or set up a monitoring tool like Prometheus with the Redis exporter. If evicted_keys is increasing, eviction is occurring.
Should I use maxmemory-policy allkeys-lru for caching and sessions together?
It’s risky. If you use the same Redis instance for caching and sessions, allkeys-lru can evict sessions. Better to separate them: use one Redis database for cache with allkeys-lru, and another for sessions with volatile-ttl. Or use the same instance but ensure sessions have TTL and use a volatile policy.