I have a customer who's magento session files are quickly growing out of control. We are purging them once a week, but it seems that it may need to be more frequent.
1) What do these files do? How are they connected to the users online experience (e.g. if I delete them and the user is still on the site, how will they be affected)
2) How soon can I delete them? How long do the files really need to remain on the server?
Chris
<magento_root>/var/session directory.
Session's are stored in Redis server. We can also configure the Redis cache from root/app/env. php file in Magento2.
Magento1 uses core session (core/session), whereas Magento 2 is no longer using core session like earlier. Magento2 provides five types of sessions: Magento\Backend\Model\Session– This session is used for Magento backend.
A session is a way of storing variables and making them available on multiple pages on a website. In other words, a session is a temporary object that is created on the server for each Magento 2 store users to store some values, i.e., items in a cart.
With file based sessions, they will be auto-pruned by the PHP session clean-up cron – so the files are likely to be deleted within ~7200 seconds of creation. So even on a busy site (30k uniques per day), there usually only around 4,000 session files in ./var/session – which is nothing for a Linux server.
However, the clean-up actually relies on the cron working - which doesn't normally look in the ./var/session directory of Magento. So you should set up a new system cron
/usr/bin/find /home/myuser/public_html/var/session -mindepth 1 -maxdepth 1 -type f -cmin +$(/usr/lib/php5/maxlifetime) -print0 -exec rm {} \; >/dev/null 2>&1
The default clean up period for sessions is 7200 seconds, which should be more than adequate, although you can change the above to suit.
With Memcache sessions, TCP/IP is the only overhead – which for a single-server deployment, would make it slower than file based. So then, you would use a unix socket instead, which removes that overhead and gives better security. But even still, your customer sessions will be truncated/limited as to the amount of RAM you can allocate. The average Magento session is 4Kb – so you’ll be able to support 256 active sessions, per MB you allocate. So be sure to set an appropriate limit to avoid customers randomly losing cart/session. And also bear in mind, a Memcache daemon restart will wipe out all existing sessions (BAD!).
With Redis (not native, but available via an extension), you get a similar level of support as Memcache, but with the added benefits of persistence (should you wish to use it). With the Cm_Redis extension, you'll also be able to take advantage of session compression. We've found this extension works very well on both CE and EE deployments.
The with DB, the default prune expiration setting is a mighty 1 week, so with the above store size as an example (30k uniques per day), you’ll be looking at a DB table size for core_cache_session of around 7GB – which will grind your store to a complete halt, for almost every session based operation.
From experience of hosting both large (230k unique visitors per day) and small (<1k unique visitors per day) stores, our recommendation is:
Single-server deployment - files
Multi-server deployment - Redis (using a separate database from the main Magento cache)
I wrote some really thorough replies here http://magebase.com/magento-tutorials/magento-session-storage-which-to-choose-and-why/comment-page-1/#comment-1980
Each file is one person's session and should last no longer than session.gc_maxlifetime
seconds - the garbage collection - set in the server's php.ini file or overridden in an .htaccess file. Lowering this value means fewer sessions will accumulate.
Magento has another trick concerning sessions; In the /app/etc/local.xml
file the session_save
value can be changed to db
meaning the database will be used instead of files but will still respect the aforementioned garbage collector lifetime. Also memcache
can be specified if you have set that up (see /app/etc/local.xml.additional
). Both are very useful if the server is a cluster.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With