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:
- Crunchify is a mature site and started getting very higher traffic
- Couple of recent plugin updates
- Recently theme upgrade and custom modification which includes Custom Post Type and more.
- 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.
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):
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
.
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
.
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.
How to fix Disqus High CPU usage issue caused by dsq_sync_forum Sync Job?
Solution-1.
Step-1
- Go to
Plugins
->Editor
- Choose Plugin
Disqus Comment System
from combo box - Search for line 514
wp_schedule_single_event($ts, 'dsq_sync_forum');
- 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
- Go to
Comments
->Disqus
- Click on
Plugin Settings
- Uncheck box for
Disable automated comment importing
Save
changes
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?
- Load Disqus only on
onClick event
– That means, user have to click onLoad Comment button
to load comments. But there are somedisadvantages
.- If a visitor’s sees comments right served up to them, they may be more likely to read them.
- Contribution in comments section is also more likely if comments section is seen by default.
- 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):
Update:
On July 15h, 2017 we moved over to Default WordPress comment system. Get some idea why on that post 🙂