Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

Noticed Higher CPU, Memory Usage for WP site? WordPress Optimization Guide, .htaccess tricks and Disqus problem

$
0
0

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

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.com Page loads in less than seconds

With ~14 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 when ever 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 visitors 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

The post Noticed Higher CPU, Memory Usage for WP site? WordPress Optimization Guide, .htaccess tricks and Disqus problem appeared first on Crunchify.
Author: App Shah


Viewing all articles
Browse latest Browse all 1037

Trending Articles