How To Tame WordPress CPU Usage

© 2010 Patrick Yuen

WordPress has a reputation of being very resource hungry and there are multiple reports of people inexplicably having problems with the web host complaining about high cpu usage. It’s hard to tell where the problem lies. With the large installed base of WordPress sites, it’s obvious that the problem is not inherent in the core code. There are numerous complaints about high cpu usage by WordPress users but few solutions. Part of the problem is that there is no one magical thing that can fix high cpu load. Sometimes the problem appear out of nowhere with no changes in traffic or code and disappear just as mysteriously. I wrote this article after struggling for months with complaints from my web host. Hopefully, you’ll find something here that might help you tame that elusive beast.

List from most impact to least impact:

Install Cloudflare. Cloudflare caches your blog content into their cloud reducing your server traffic by approximately 50%.

Install SuperCache or W3 Total Cache. This is the most critical thing you can do to reduce cpu load and reduce site load times, even more critical than running the latest version of WordPress and plugins. WordPress is very database intensive and each time a page is displayed it must render the page dynamically by making calls to the database. Adding a cache will render those pages into static content that reduces the need for database calls dramatically. Make sure you turn on compression if it is off by default. All decent web host should support this. It can save a lot of bandwidth and speed up response time dramatically.

Install Wordfence plugin. Wordfence  includes a firewall, anti-virus scanning, cellphone sign-in (two factor authentication), malicious URL scanning and live traffic including crawlers. In the Wordfence options tab, enable throttling or blocking of bot and/or humans who hit your site too often. Only you can define what is too often. A site with a many slideshows of small pictures may require more hits per second than one which is primarily text.

Install Captcha plugin. If you allow user registration on your blog, be sure to install Captcha. I’m not 100% convince it helps as most captchas can be defeated but give it a try.

Check your permalink structure. According to WordPress, permalink structures beginning with %category%, %tag%, %author%, or %postname%, require more server resources to resolve than structures such as, Day and NameMonth and Name, and %post_id%-%postname%, though as of 3.3 the %postname% structure is usable. If you’re not using the optimum setting, change it as soon as possible. WordPress should keep track of the old and new structure and redirect automatically so you should not have to worry about dead link. Backup your blog folder and database before changing it just in case.

Update WordPress and Plugins. This should be obvious but update WordPress and all your plugins to the latest versions making sure your plugins are compatible with the the version of WordPress you are running.

After troubleshooting my problems for several months, it appears the Yahoo crawler was the culprit so I’m adding this to the top of the priority list of things you should do.Throttle Yahoo crawlers. Yahoo’s bot name is SLURP. They have a special robots.txt parameter to lessen the frequency of their crawler. Start with a low value like 5 and increase this number to 10 to lower the frequency. Don’t go higher than 10. A high delay may cause Yahoo to stop indexing your site! Google spider does not support the crawl-delay option but Googlebot is very efficient and does not require it. Put this in your robots.txt file.

User-agent: Slurp
Crawl-delay: 5

Identify. If you are running multiple blogs on your site, make sure you are optimizing the right blog. Cpu reports for your account is for your entire shared server account with top process reports for individual blogs. The webhost will often report /index.php as the offending file and that info is basically useless as all that file does is call other files.

Measure how many database calls your blog makes. Add this line to your footer.php file just above /body tag. You should see a new line like this showing the number of database calls and how long it took. This will give you some idea of the effect of changes you make to optimize your blog.

14 queries in 1 second is pretty reasonable for a home page

queries

<?php echo get_num_queries(); ?> queries in <?php timer_stop(1); ?> seconds.

To edit this file, make sure you have a backup of the file. If not, simply copy the content info notepad and save. Go to Appearance-Editor. Click on footer.php, add the code, and update.

  • Guest

    Hi, thanks for the great post. I found it very helpful.

    I have been havinf issues with CPU usage the past few days and have used the methods you mentioned here but I woul dlike to ask how can I check the site now uses less CPU?

    On average I get about 600 visitors a day to my site and this will number can sometimes rise to almost 900, do you think this type of site requires a dedicated server?

    Thanks again

  • Hi,
    thank’s for the info here,very informative and will see more update soon.

  • Well I will test all your suggestions here and see if it works for me! thanks for the tips

  • can it help to upgrade to a dedicated server? I have a shared server and deleting plugins and installing w3 Total cache doesn’t seem to work

    • Yes it will help if you don’t mind going from $5/month to paying over $165/month in hosting fees. Shared hosting is shared with about 200+ other sites. Dedicated hosting gives you your own server. It’s overkill for most blogs. Make sure you install Cloudflare. It helped tremendously on my blog. If all else fails, consider changing web host or going to VPS hosting.

      • I didn’t heard before about Cloudfare, thanks a lot for the information. I’ve left 8 plugins and optimized the site with GTMetrix, and now it has a PageSpeed of 96% and an YSLow percentage of 94%. The site is now available, but at some points I get too many hits for the shared hosting service. In the end I decided to go for the $175 and get a fully-managed Linux server, A least I’l be able to sleep well:-))

  • I am having problems with site suspension and server overload too these days. Hosting company will not help 🙁
    Can anybody explain how (where) to see the measure of CPU load of our sites?

    • It varies from host to host. Ask your webhost provider.

      • thanks..but they are not of help at all 🙁

        • Not all host offer that service. Lunarpages gives stats for the last week. Sign up here if you’re interested. http://www.lunarpages.com/id/patyuen

          • Helen

            Yes, they give you stats, but they are totally useless when it comes to offering you help. I’m currently going through exactly this too much server resource consumption with them. I’ve gotten so frustrated with their lack of assistance, I’ve found another host and have VPS hosting through them. Now the only problem is that I can’t copy my files through my ftp program from LP either. Their firewall blocks me at because my folders are too large. Again, their techs are not able to be of any assistance, since each one I talk with gives me a different answer. Needless to say I am frustrated with LP. They used to be a good company to deal with, but sadly not so much any more.

          • How much storage are you using? I can easily zip up my files into 9 GB using file manager and download it via ftp. You have to download the files to your computer and upload them to your new host.

  • Poed

    No fix here or anywhere else. It eats CPUs; even the fastest biggest.