Pruning Magento Cache & Sessions Using Cron

2 Shares Google+ 2 Facebook 0 Twitter 0 LinkedIn 0 2 Shares ×

Over the weekend a client contacted me because they had serious performance issues with their Magento based store. The three main issues were:

  1. Checkout taking more than 40 seconds to complete after clicking to submit payment. (This of course causes double orders/payments and users just leaving/lower sale conversions.).
  2. Login to the front or back end taking excessively long.
  3. Slow importing of new products that would even timeout.

Upon investigation I narrowed down the issues to Magento’s Cache. Sounds weird, I know, since “caching” should, in theory, be faster. However, Magento caches heavily and does not remove invalidated files. Also Magento had saved sessions that amounted to well over 3 million files!

 

Pruning Magento Cache and Sessions

Once I purged cache the issue was resolved. However, it takes less than 24 hours for the cache to again grow large enough that the issues would return. For me, completely deleting cache every few hours didn’t seem like a good solution because visitors who hit the site just after cache was completely purged would face a slower than usual experience.

Therefore, I implemented a balanced approach by deciding to setup an hourly cron script that would find and delete files older than X. With that approach, the issues above were resolved without any noticeable performance hit when cache is pruned, instead of purged.

The script I ended up using was:

#!/bin/sh

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

* Change your path of course and run as root. (test first by manually launching or using a single command).

This 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 keeps the cache & session files below millions of files which become more of an issue with slower disk.

Make sure your Magento cookie (session) timeout is set shorter than the prune TTL. In this case, Magento’s cookie timeout is set to 1 hour so pruning files older than 2 hours won’t delete any active sessions! This server also uses Varnish FPC and APC for opcode and Magento caching.

Hope this helps another admin who experiences these issues. Please add your solution to this issue if different.

Have a Magento store or run a traffic-heavy Linux environment that’s slow? Contact me

2 Shares Google+ 2 Facebook 0 Twitter 0 LinkedIn 0 2 Shares ×
Pruning Magento Cache & Sessions Using Cron

5 Responses

  1. There’s better ways to handle this but I like your sysadmin approach. PHP has a garbage collection that cleans up sessions via the gc configuration settings in php.ini.

    Also, this fixes the Caching issue you’re referring to, which is actually cache pruning causing the issues.

    https://github.com/colinmollenhour/Cm_Cache_Backend_File

    Your method would work and is quick and effective but maybe not the best way to deal with the issue.

    MageAustralia April 17, 2013 at 9:04 am #
  2. Thanks for this. Found more about that here as well: http://magento.stackexchange.com/questions/41/how-should-i-handle-session-files-that-become-too-numerous

    @hydnj April 17, 2013 at 3:13 pm #
  3. Thanks you for this! I must say, I also prefer just using Linux to deal with files rather than playing with how Magento caches. We’ve added this to our company wiki.

    GCotton74 April 18, 2013 at 5:27 am #
  4. No need to use cron jobs to clear out sessions that are older than two hours, just use these values in php.ini:

    zend.enable_gc = On
    session.gc_probability = 1
    session.gc_divisor = 1000
    session.gc_maxlifetime = 7200

    Since the default Magento cookie (session) time is actually 3600 seconds (1 hour), unless you change it, you can set session.gc_maxlifetime to 3600 as well.

    Tom Murphy April 22, 2013 at 9:07 am #
  5. Thanks! What about Magento cache?

    @hydnj April 22, 2013 at 8:47 pm #

Leave a Reply

2 Shares Google+ 2 Facebook 0 Twitter 0 LinkedIn 0 2 Shares ×