How to fix Higher CPU, Memory Usage for WordPress site? Optimization Guide, htaccess tricks and Disqus

Last updated
App Shah

Crunchify » WordPress Optimization and Tutorials » How to fix Higher CPU, Memory Usage for WordPress site? Optimization Guide, htaccess tricks and Disqus

We are hosted at Squidix semi dedicated hosting and it’s pretty reliable. We never faced any issue with them. But recently we noticed higher CPU and Memory usage on our server.

There might be multiple reasons for higher CPU and Memory usage:

  1. Crunchify is a mature site and started getting very higher traffic
  2. Couple of recent plugin updates
  3. Recently theme upgrade and custom modification which includes Custom Post Type and more.
  4. may be something else…

Let’s get started –

Check Point-1: Memory and CPU usage

Here is a problem definition. Why these much Memory and CPU usage? Checkout below ~50% CPU usage and ~4GB Memory usage graph.

Higher-CPU-and-Memory-Usage-while-Disqus-Cron-Jobs-enabled - Crunchify tips

We have an open bug on official disqus github repo.

Check Point-2: Entry Processes

Once we looked at our cPanel stats then we noticed the same high number of entry processes.

Seriously?? ~228 entry process at a time??? Bluehost and Hostgator by default allows maximum 25 entry process at a time compare to Squidix allows 400. There is no way you could have survived with Bluehost or Hostgator hosting in this case.

Point here is ==> ~228 entry processes are toooo much.

Checkout stats (before):

Higher-CPU-Usage-with-blocking-Disqus-Sync-Cron-Jobs

In order to fix this higher WordPress CPU, Memory and Entry process usage we started looking at all possibilities.

Precaution Step-1

First we eliminated higher traffic pattern as a cause because we use WP Super Cache WordPress plugin and it handles this situation very well. In fact – page loads in near half a second.

Crunchify Page Loading in less than half a second

With ~16 millions calls per month we didn’t see any degradation on site loading metrics. Let’s move on.

Precaution Step-2

Any recently plugin and theme update?

Hmm… those updates were from very reliable WordPress contributor and after spending significant amount of time we also ruled out this possibility too. We do have local WordPress setting using MAMP and after disabling and enabling those plugins we didn’t notice any page load time or higher CPU issue.

Precaution Step-3

Now I left with only my last doubt about plugin Disqus. I googled it and found couple of very interested threads about higher CPU usage and Disqus.

Higher-CPU-usage-and-Disqus

On Crunchify, we do have Disqus Sync enabled by default.

Now think about this scenario:

When you have normal traffic like ~2000 active users at at time then Disqus’s sync job also executes ~2000 times at given time and that’s not good.

1 Sync job = 1 Cron job call. And finally we found the actual problem.

1900 active users on crunchify.com at any given time

How to fix Disqus High CPU usage issue caused by dsq_sync_forum Sync Job?

Solution-1.

Step-1

  1. Go to Plugins -> Editor
  2. Choose Plugin Disqus Comment System from combo box
  3. Search for line 514 wp_schedule_single_event($ts, 'dsq_sync_forum');
  4. Replace it with this code block and save file.
$next_scheduled = wp_next_scheduled('dsq_sync_forum');
if($next_scheduled) {
    // do nothing
} else {
    // It's time to sync now
    wp_schedule_single_event($ts, 'dsq_sync_forum');
}

Step-2

  1. Go to Comments -> Disqus
  2. Click on Plugin Settings
  3. Uncheck box for Disable automated comment importing
  4. Save changes
Disqus dsq_sync_forum High CPU issue fix

So, How to sync comments now?

Now after this whenever you want to sync Disqus comments with WordPress Database then just check above box and save settings. All comments will be imported to your Database within 10 minutes.

Solution-2. How to Disable wp-cron.php via .htaccess trick?

  1. Load Disqus only on onClick event – That means, user have to click on Load Comment button to load comments. But there are some disadvantages.
    1. If a visitor’s sees comments right served up to them, they may be more likely to read them.
    2. Contribution in comments section is also more likely if comments section is seen by default.
  2. Block Disqus sync on every page load by adding .htaccess rule.
    • I tried this approach and it worked too.

In order to achieve point-2 above I’ve add below mod rewrite rules into .htaccess file.

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{QUERY_STRING} !=""
 RewriteCond %{QUERY_STRING} !^s=.*
 RewriteCond %{QUERY_STRING} !^q=.*
 RewriteCond %{QUERY_STRING} !^url=.*
 RewriteCond %{QUERY_STRING} !^ver=.*
 RewriteCond %{REQUEST_URI} !^/wp-admin.*
 RewriteRule ^(.*)$ /$1? [R=301,L]
</IfModule>

Basically above code will block all wp-cron.php jobs 🙂

NOTE: we have recently added url pattern which is required for all WordPress Embed functionality and ver pattern which is required to load style.css or other files with version number as parameter.

Let’s understand this block:

  • Ignore all query string in URL and redirect to your blog URL
  • If query string contains ?s= and ?q= then only it’s allow. Sample:
    • https://crunchify.com/search/?q=wp%20super%20cache
    • https://crunchify.com/?s=wordpress
  • Ignore this rule if URL contains wp-admin

After enabling above rule too, I saw significant improvement on Memory and CPU usage and it's nearly zero. Also – entry process was almost reached to zero(0). That means visitors were served cached page almost all the time.

This post will helps you also if you have below problem and it’s part of WordPress optimization tips.

  • WordPress High CPU usage?
  • WordPress High Memory usage?
  • wordpress high cpu usage fix
  • wordpress excessive resource usage
  • wordpress cpu usage problem
  • Fix dsq_sync_forum high CPU issue
  • wordpress plugin cpu usage
  • reduce wp cpu usage
  • How to monitor and reduce WordPress memory usage
  • wordpress memory usage exceeded
  • wordpress memory usage optimization
  • wordpress disqus plugin memory usage

Check out the difference (After):

Very-Less-Memory-and-CPU-usage-after-enabling-htaccess-rules

Update: On July 15h, 2017 we moved over to Default WordPress comment system. Get some idea why on that post 🙂

29 thoughts on “How to fix Higher CPU, Memory Usage for WordPress site? Optimization Guide, htaccess tricks and Disqus”

  1. Hi, I have a website with 300 users at a time. I am using wordpress with 2 CPU and 4 GB and seeing performance issues. Wondering when you had 2000 active users how much CPU and Memory you were using for your site?

    Reply
  2. Nice Article, I have Ecommerce books website.
    After using cache plugin like lite speed cache & W3 total cahe – my website hosting & memiory usage is very high please help for resolve issue

    Reply
  3. Good post but not for the problem I have unfortunately. I’m seeing connection lost/high CPU caused by ajax. Already added heartbeat and disabled it on the dashboard. Still the same. So when I have a longer page, not very long but long enough and I try to update it, it hangs. Shared hosting. Godaddy tech is clueless, they only point to plugins which I only have just a few. Other sites with same plugins work fine no issues, only this one on shared hosting. Can’t find the culprit….

    Reply
    • Hi Oxana Ox – sorry to hear about your site issue. To be honest, in my case most of the time, shared hosting caused us so many issues in the past.

      After migrating to Kinsta and Google Cloud have been better.

      One more point is to move to latest version of PHP. At least PHP 7.3+. Which PHP version are you on? Having long post shouldn’t have any issue.

      Reply
  4. Thank you for these tips on how to fix higher CPU usage, I can now apply this on my website. Also, if you find that high CPU usage persists even in support of standard processes, you may simply need a faster computer. You can also reduce CPU load by adding more RAM, which allows your computer to store more application data.

    Reply
  5. It didn’t answer the Question Specified in the title, How to Fix High memory usage on WordPress ?
    it just gave solution to disqus problem, what with other many millions people ? atleast you could give us any clue of how to find the culprit code ?

    Reply
  6. Good article! About analyzing GC logs, gceasy is also good. It is free web tool. You have to upload your GC logs to their server, and results are provided online.

    Reply
  7. RewriteEngine On
    RewriteCond %{QUERY_STRING} !=””
    RewriteCond %{QUERY_STRING} !^s=.*
    RewriteCond %{QUERY_STRING} !^q=.*
    RewriteCond %{QUERY_STRING} !^url=.*
    RewriteCond %{QUERY_STRING} !^ver=.*
    RewriteCond %{REQUEST_URI} !^/wp-admin.*
    RewriteRule ^(.*)$ /$1? [R=301,L]

    not working on https(SSL) site

    Reply
  8. thanks working so good… now CPU load average less than 1.000xx

    view on the status plugin server-status-littlebizzy.

    Reply
    • Hi Karim – it’s really great to hear it’s working perfectly fine for you. Checkout some more optimization articles on Crunchify and let me know your feedback. Happy blogging.

      Reply
  9. thanks for providing a information which I found to be quite accurate and useful. I think it is a genuine problem Memory Usage for WordPress site?

    Reply
  10. I think Disqus must’ve recently changed something, as it’s hammering our CPU at the moment. Totally nuts. Our traffic hasn’t increased proportionately, so I am stumped as to what’s going on.

    Reply
    • Yeah. I still have below code in .htaccess file.

       RewriteEngine On
       RewriteCond %{QUERY_STRING} !=""
       RewriteCond %{QUERY_STRING} !^s=.*
       RewriteCond %{QUERY_STRING} !^q=.*
       RewriteCond %{QUERY_STRING} !^url=.*
       RewriteCond %{QUERY_STRING} !^ver=.*
       RewriteCond %{REQUEST_URI} !^/wp-admin.*
       RewriteRule ^(.*)$ /$1? [R=301,L]
      

      Auto syncing by creating cron jobs entry is not the right way to do it and unfortunately Disqus still hasn’t fixed that 🙁

      Reply
  11. Bundle of thanks for this tutorial. Due to this tutorial I get rid of excessive usage of CPU.
    Keep sharing like this..
    That’s a great tutorial which help me.,
    -Humna

    Reply

Leave a Comment