How to Take Control of The WordPress Heartbeat API

Heartbeat Control Featured Image
photo credit: osseouscc

The WordPress Heartbeat API, introduced in WordPress 3.6, simulates a pulse and is responsible for revision tracking, session management, and more. The pulse is around 98 Bytes in size, but it can cause performance issues in certain situations.

If you’ve been notified by your webhost that your account is using too many resources and cite POST /wp-admin/admin-ajax.php as the cause, it’s likely due to the Heartbeat API.

How The Heartbeat API Works

Inmotion Hosting has an excellent article that explains how the Heartbeat API works. If you monitor server requests while in the post editor, you’ll see POST /wp-admin/admin-ajax.php "http://example.com/wp-admin/index.php appear every 15 seconds. This is the pulse generated by the API.

The pulse makes sure you have a persistent connection to the web server and provides a way for developers to trigger events. For example, if the pulse takes 30 seconds or longer to process, WordPress displays a connection error and attempts to reconnect to the server.

The Problem

Execution Time
Execution Time

There are several situations in which a pulse is generated either automatically, or by a user. Most of the time pulses are harmless and don’t do anything.

Each pulse (POST request) executes a PHP script equating to CPU time on the server. In an experiment, InMotion Hosting left the dashboard open for a half hour and noticed 25 PHP script executions that used 5.77 CPU seconds. The small amount of CPU time was used to do nothing.

The problem is compounded by the fact that each user who has access to the backend of WordPress generates a pulse. As the number of users navigating the WordPress backend simultaneously increases, so does the number of pulses generated from the API.

If a webhosting provider has strict limits on the number of processes or resources used, you could easily go over the limit.

How to Control The Heartbeat API

Heartbeat Control, developed by Jeff Matson, is a new plugin that enables you to control the interval of pulses. After activating the plugin, you’ll find the settings located in Tools>Heartbeat Control.

Heartbeat Control Settings
Heartbeat Control Settings

You can choose to disable the API entirely, only on the dashboard page, or allow it only on the post editing screen. You can also choose the interval when pulses are sent. The choices range from 15-60 seconds with 5 second intervals in between.

By changing the interval to 60 seconds, pulses are less likely to impact server performance. Several HostGator customers who use the plugin report they no longer experience suspensions for using too many resources.

Why InMotion Hosting Doesn’t Throttle The API by Default

If increasing the pulse interval from 15 to 60 seconds increases server performance, why doesn’t InMotion Hosting throttle the API on every webhosting server?

Matson, who works for InMotion Hosting, says a one size fits all approach doesn’t work. “The reason we don’t do that is because there are some users that require it at the default rate or an even higher frequency. When making server-wide changes, you have to be extremely careful that you do not impact a customer’s site.”

Matson goes on to say that, “As a host, the number one goal is making customers happy and restricting the normal operation of a user’s site is unacceptable.”

How HostGator Almost Deleted WP Tavern

I sympathize with those who use HostGator as they have a strict CPU resource restriction. If pulses from the Heartbeat API cause the CPU to become busy or cause a backup in processes, using 25% of the CPU longer than 90 seconds is easy to do.

HostGator’s policy for using too many resources is to suspend the account. This makes it impossible to troubleshoot the problem. If you exceed the resource allotment more than five times, the site is removed from the server and your account is banned.

HostGator Resource Policy
HostGator Resource Policy

In late 2013, when WP Tavern used HostGator, I experienced first-hand what it’s like to have a website suspended. When I upgraded the Tavern to WordPress 3.6, which introduced the Heartbeat API, I started to experience problems. The site would routinely lock up and when I viewed the processes tab in cPanel, it looked like a simple process repeated itself, leading to a denial of service.

Site Offline ErrorsI can’t confirm if the Heartbeat API was the culprit but it’s high on my list of suspicions. After suspending the Tavern twice, HostGator informed me that if the site was suspended again, it would be removed from the server without giving me a chance to back it up. Needless to say, I switched hosts immediately.

Don’t End Up in a Similar Situation

If you’re using a webhosting service with strict resource limits, consider installing Heartbeat Control to limit the number of pulses. Not only will it mean fewer calls to the server, but it may lead to a performance increase as well. Keep a close eye on the resources used and if you come close to maxing out, it’s a sign you need to upgrade to a better plan. Don’t end up in a position where you may lose your site!

There are 19 comments

Comments are closed.