Cron. Using Magento Cache and Session Sorting

Note: Magento 1.x only. A customer contacted me over the weekend because they were having serious performance issues with their Magento based store. There were three main issues:

  1. Checkout takes more than 30 seconds to complete after you click to submit payment. (Due to double orders/payments and users leaving the store)
  2. Login on front or back end taking too long.
  3. Slow import of new products and timeouts.

After investigation I narrowed down the issues to Magento’s cache. Magento 1.* caches heavily but doesn’t always remove invalid files. In addition, Magento had saved sessions that had a volume of over 3 million.

 Cron.  Using Magento Cache and Session Sorting

Read also: MySQL Performance: Stop Hoarding. Drop unused MySQL database

Magento Cache and Session Sorting

Once I purged the cache the problem was resolved. However, it took less than 24 hours for the cache to become so large again that the problems returned. In a case like this it is recommended that you implement a balanced approach by setting up a one hour cron script to delete files older than x. With that approach, the issues above were resolved without any noticeable performance hit when the cache was truncated rather than purged.

The script I was using was:


find /var/www/html/var/cache/ -type f -mmin +120 -exec rm {} \;
find /var/www/html/var/session/ -type f -mmin +120 -exec rm {} \;
find /var/www/html/var/report/ -mtime +7 -exec rm -f {} \;
find /var/www/html/var/tmp/ -mtime +1 -exec rm -f {} \;

* Change your way and run as root. (Test manually first).

This method prunes magento’s cached files older than 2 hours, session files older than 2 hours, report files older than 7 days and tmp files older than 1 day. This now puts cache and session files under millions of files which avoids storage I/O bottlenecks.

Make sure your Magento cookie (session) timeout is set lower than Prun TTL. In this case, Magento’s cookie timeout is set to 1 hour, so truncating files older than 2 hours won’t delete any active sessions! This server also uses Varnish FPC And APC for opcode PHP Opcache and Magento Caching.

Hope this helps another magento admin. Please add your solution to this issue if it is different.

Last Updated: 22 May 2019

Leave a Comment