By default Magento stores customer sessions as files in var/session. That works on a small store with one server, and causes two problems as a store grows.
The problems with file sessions
- Locking: PHP locks a session file while a request uses it. Checkout fires several requests at once, so they queue behind each other and the page stalls.
- More than one server: sessions don’t follow the customer between servers, so people get signed out and lose their carts at random.
- Disk growth: millions of small session files build up when cleanup doesn’t run.
Check where sessions are stored
Look at the session section in app/etc/env.php. 'save' => 'files' means file sessions.
Move them to Redis
bin/magento setup:config:set --session-save=redis \
--session-save-redis-host=127.0.0.1 --session-save-redis-port=6379 \
--session-save-redis-db=2
bin/magento cache:flush
Use a database number that the cache does not use (see separating sessions and cache). Everyone currently signed in is signed out by the switch, so do it outside busy hours.
After switching
Old files in var/session are no longer used and can be removed. Watch Redis memory for a few days, and give sessions their own Redis instance on busy stores.
Catch it automatically
MageMonitor checks for this every few minutes as part of its Magento health checks, and alerts you by email, Slack or webhook the moment it appears, with the exact commands to fix it. See MageMonitor for Magento 2.
