Ha! Hold My Brain; Be Still My Beating Heart – or – WordPress Heartbeat Troubles

My Beating Heart ..

Sir, this audacity!
.. Oh, my heart, my beating heart!
This unwarrantable presumption on the part of a common website!

With credits to Gilbert & Sullivan – HMS Pinafore

The WordPress Heartbeat was introduced in WP 3.6 to help users when editing Posts, Pages or doing other administrative functions.  When a web browser is logged in to the WP backend, a Javascript task on said browser communicates with WP periodically using the admin-ajax.php script.

The default for “active” sessions is 4 times per minute, or 240 times per hour.  Note that this occurs for each active window.  If you are working on a computer and have wp-admin, e.g. the dashboard, open in one window and you are editing a post or a page in another window, those are TWO active sessions and the WP heartbeat will happen.  For “non-active” browser sessions, the default is once every 2 minutes.

In the above paragraph I said “active” sessions, without defining those.  An “active” session is one where there has been user activity as measured by mouse and/or keyboard actions within the last 50 seconds or so.

Just get the Plugin:

For most folks, installing a plugin is a more familiar operation than editing the Theme’s “functions.php” file :).
Get the current version from GitHub at drmagu/wp-flatline.
Just download the ZIP archive and install it as a new plugin for your site.  Your server may thank you :).

Why the Heartbeat ?

Well .. it seemed like a good idea.  It supports “post-edit” locking when more than one person attempts to edit the same post or page.    It warns you when you try to leave a page (in edit mode) and it *thinks* you have not saved or updated.

In addition, WP provides an API which allows plugin and theme developers take advantage of this 2-way “browser-server” communication.

The Side Effects

Not long after people upgraded to a version of WordPress which implemented the heartbeat API, the reports of bad “side-effects” started coming in.  Especially sites on shared-hosting providers were affected, but WordPress deployed on dedicated hosting also experienced issues.

The reported problems are of two types:

  1. Excessive CPU Usage
  2. Excessive Bandwidth

Both are related of course.  The “heartbeat” javascript on the browser sends an AJAX requests to the server at “admin-ajax.php”.  This causes script execution, and a return of data to the browser.  The script execution causes CPU cycles to be consumed, and the returned data eats “bandwidth”.

CPU Usage

Just google it :).  In any case, here is a telling graph reproduced from here.

admin-ajax-high-cpu-problem2

One can clearly observe the effect of the WordPress heartbeat on server CPU loading.

Bandwidth

It is not often mentioned, but one of our web-development clients had their hosting provider complain.  Both shall remain un-named to protect the innocent, but here is a quote from an email sent by the hosting provider.

Hi xxxxxxx,

As per Karls’ comments, the /wp-admin/admin-ajax.php file is still getting hit with excessive traffic, there’s be 17,000+ hits for this one file since the 31st of October. The amount of bandwidth that the file has been called each time is around 74KB in size. (….) If the file is not used by any plugins then can we try moving it to another name so that it’s not visible? We could use admin-ajax1.php as an example name.

Obviously turning admin-ajax.php off, or blocking it, is never an option with most WordPress sites, as themes etc. often rely on AJAX for front-end as well as back-end functionality.

This mail was sent on November 24, 2014.  So 17,000 hits at 74 kB each is over 1.2 GB of “bandwidth” used in less than a month, as reported by the hosting provider.  Some providers, this one included, charge extra for traffic overages.

WP Flatline Plugin

Turning the WordPress heartbeat OFF completely is a good strategy in some situations.  Indeed, for sites that do not have multiple editors working on posts together, it may not be essential to have post locking enabled. If your posts are typically short and easily reproduced, it may not be worth consuming resources to enable autosaving.

Unfortunately, turning heartbeat off completely also interferes with some post / page edit functionality.  Amongst others: “quick edit”, page “visibility” settings and more.  Also, any plugins which rely on the heartbeat API might break.

Now, the instructions you find on the web for disabling heartbeat can be daunting for many.  So we developed a very simple solution.  A WordPress plugin which, when activated, stops all heartbeat activity.  It is aptly named the “WP Flatline Plugin” and can be installed from GitHub.

For most folks, installing a plugin is a more familiar operation than editing the Theme’s “functions.php” file :).

Get the current version from GitHub at drmagu/wp-flatline.

Just download the ZIP archive and install it as a new plugin for your site.  Your server may thank you :).

 

2 Responses to “Ha! Hold My Brain; Be Still My Beating Heart – or – WordPress Heartbeat Troubles”

  1. I just tested the WP Flatline Plugin with WordPress 4.1.1. It appears now that disabling heartbeat no longer interferes with core functionality, such as quick-edit, visibility, etc. I have corrected the text in the article to reflect this.

  2. … [Trackback]

    […] Read More Infos here: drmagu.com/ha-hold-my-brain-be-still-my-beating-heart-or-wordpress-heartbeat-troubles-951.htm […]

Leave a Reply

You must be logged in to post a comment.