Fix Library: Admin and indexing
Customers Logged Out Randomly (Sessions and Redis or Valkey)
Last updated:
What you see
Customers are logged out while shopping, carts empty without reason, or admin users are thrown back to the login screen.
Quick answer
Sessions usually disappear because Redis or Valkey deletes them to free memory, often when sessions share an instance with the cache. Short cookie lifetimes and a cookie domain that does not match the store address cause the same symptom. Check evictions and session settings first.
What usually causes it?
- Redis or Valkey evicting session keys when memory is full
- Sessions and cache sharing one Redis instance with an eviction policy
- A short cookie lifetime
- The cookie domain does not match www and non-www addresses
- Several web servers storing sessions locally instead of in a shared store
How do you check it safely?
These checks only read information. Run them on staging first if you can, from the Magento root folder as the file system owner. Hosting setups differ, so paths and service names may need adjusting.
- See where sessions are stored.
grep -n -A12 "'session'" app/etc/env.php - Check memory, eviction policy and evicted keys on the session instance.
redis-cli -h 127.0.0.1 -p 6379 INFO memory | grep -E 'used_memory_human|maxmemory_human' redis-cli -h 127.0.0.1 -p 6379 CONFIG GET maxmemory-policy redis-cli -h 127.0.0.1 -p 6379 INFO stats | grep evicted_keys - Check cookie settings.
bin/magento config:show web/cookie/cookie_lifetime bin/magento config:show web/cookie/cookie_domain
How do you fix it?
These steps change your store. Run them on staging first, with a backup of code and database.
- Give sessions their own Redis or Valkey instance or database, with enough memory and a policy that does not evict them (noeviction or volatile-lru).
- Set a sensible cookie lifetime, in seconds.
bin/magento config:set web/cookie/cookie_lifetime 3600 - Clear a wrong cookie domain, or set it to match your store address, then flush the cache.
- On several web servers, store sessions in a shared Redis or Valkey instance.
When should you call an expert?
Call an expert if logouts continue after these fixes, or happen only under load or during sales.
Need it fixed today?
I can look at it with you. Book a 20 minute call, or if the store is down, see emergency Magento support.
FAQ
Common questions.
Can I use Valkey instead of Redis?
Recent Magento 2.4 releases support Valkey, which works with the same Redis settings. Check the system requirements for your exact version.
Why do carts empty but customers stay logged in?
Guest carts and persistent carts use different settings. Check quote lifetime and persistent shopping cart settings as well.
Related
Rather have it fixed for you?
Send a few lines about the problem, or book a 20 minute call. You get an honest read and a fixed quote.