Customers were losing their cart contents and getting logged out randomly during shopping. The problem intensified on weekends with high traffic. Fix time: 2 hours.
Symptoms
- Customers randomly logged out during shopping
- Cart resets without reason
- Problem worse during high traffic
- Redis
INFO memory:used_memoryclose tomaxmemory
Cause
Redis was configured with maxmemory-policy allkeys-lru – when free memory ran out it evicted the least recently used keys, including active user sessions.
Solution
redis-cli CONFIG SET maxmemory-policy volatile-lru redis-cli CONFIG SET maxmemory 2gb
<?php
// app/etc/env.php - separate Redis instances for sessions and cache
'session' => [
'save' => 'redis',
'redis' => ['host' => '127.0.0.1', 'port' => '6380'], // sessions
],
'cache' => [
'frontend' => ['default' => [
'backend_options' => ['server' => '127.0.0.1', 'port' => '6379'], // cache
]],
],
Takeaways
Using Redis for both cache and sessions simultaneously is asking for trouble. Cache can be evicted – sessions cannot. Separate them into two instances with different eviction policies: allkeys-lru for cache, noeviction or volatile-lru for sessions.
