WordPress’ Heartbeat Caused Me Heartburn

WordPress’ Heartbeat Caused Me Heartburn

Since around January I’ve been having a terrible time with my WordPress hosting.  Timeouts, pages failing to load and a very slow site.  In the past month it got so bad that I could not write a single post without my site going down several times.  The errors I was getting included 400, 403, 500, 502, 503 and 504.  Sometimes my site would just timeout, with a PHP error being returned.

I knew it was some kind of issue with traffic.  While I don’t get many visitors, I could see through my control panel that my processes for the server were through the roof.  If I manually shut down some of the processes my site would come back up.

WordPress seemed the issue.  I deactivated the plugins on all 7 of the sites I have running from this hosting account but the problems persisted.   I sent in a support ticket to my hosting service to have them take a look and my suspicions were confirmed – WordPress was the culprit.

WordPress Heartbeat

Starting with WordPress 3.6 a “feature” called the WordPress Heartbeat API was added to allow WordPress to communicate between the browser and the server more efficiently.  It is used for session management, revision tracking and auto savings, among other things.

In processes I was seeing lots of calls to admin-ajax.php, which happens to be the file that controls the conversation between the browser and the server.  The problem is, this file gets called excessively which was putting strain on my server.  Any time the browser is open with a page using the Heartbeat API, even if it is in a hidden tab, the page is making calls to the server.  And the Heartbeat API isn’t just hooked into the admin side, it also occurs on the public side of your site.

You can see how this easily gets out of hand.  If I have WordPress edit windows open for two of my sites and my husband has an edit window for his site, plus each of us may have the site open in our browsers, PLUS visitors come to our site…. well for seven sites you can easily see how the server gets overwhelmed.

Curing the Heartburn

My hosting company, Hostgator, identified the problem immediately, sending me log files and links to several articles.  They also provided a fix – adding the following code on the last line of the core WordPress file admin/plugins.php to stop the Heartbeat API altogether.  This was a good stopgap solution, but the minute WordPress was updated it would likely be overwritten.

A search found this hugely informative page from inmotionhosting.com which outlines the problem and suggests using a plugin called Heartbeat Control.  There are detailed installation instructions here, but basically install the plugin, activate it and then look for the control panel under Settings->Heartbeat Control.  Change the “control heartbeat location” to “Allow only on post edit pages” and then override the heartbeat frequency to 60 seconds.

Heartburn Cured!

And there, the problem is solved!  There are a few caveats, however.

First, as the inmotion article suggests:

By default WordPress uses the Heartbeat API to manage things such as post locking so only one admin can edit a post at once, it’s also used for auto saving. Going forward the API could be used more and more by WordPress developers to handle certain tasks…
 When WordPress updates keep an eye on how it works – you may need to tweak things.

Second, keep in mind that your posts aren’t auto-saving and posts may not be locked if more than one admin tries to access it at once.  Remember to hit “Save Draft” while typing to save yourself grief.

Finally, for the above reasons this isn’t a total cure.  Hopefully future versions of WordPress will address the Heatbeat API problem to reduce server load or at least give admins more control over how Heartbeat works.

Follow me

Submit a Comment