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


<?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.