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!

19

19 responses to “How to Take Control of The WordPress Heartbeat API”

  1. Thanks for sharing the Heartbeat Control Plugin. I host my site on SiteGround, and I have problem almost every day before I manually modify the Heartbeat rate. May I know which host plan is using for wptavern :-)

  2. Hey Joseph,

    It’s Hristo from SiteGround here. I suppose something is not working fine with the caching and Heartbeat. I’ve checked your ticket history but didn’t find any complaints from you about that. Usually, we fix such issues really fast and easy. Please send me an email at hristo.p siteground.com and I will do my best to fix this for you :)

    Best,

    Hristo Pandjarov
    WordPress Expert
    SiteGround.com

    • Hi Hristo,

      I just want to point out that I had a Heartbeat issue and was also told to throttle or disable it. I decided to disable it completely since I’m the only one maintaining my site. But if SG is supposed to be able to handle a lot of Heartbeats then a policy might need to be looked at. Just a friendly thought :-)

  3. You say “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.”

    How do I do that? And what are good tools to trace the culprits? Also when I come close to maxing out I just adjust the php limit mb allowed. My host apparently allows that. How do I find oud the max? Thanks!

    • Most cheap web hosts offer a system for tracking resource usage. The most important one to take notice of is your CPU usage. Consistently high’ish usage is okay, but it’s big spikes which can cause serious problems. If you have a flat CPU usage below their limit, then you are unlikely to get suspended. But often, you can have a fluctuating usage, which on average may be low, but as soon as you spike, you risk bogging down the whole server, and this is what will get your arse banned ASAP.

      That PHP memory limit is only on a per page basis. You shouldn’t generally need to adjust it, and if you are, then that’s probably a sign there is something wrong with your site, perhaps a plugin which is sucking up more memory than it should. Or it could be that your host provides sucky defaults.

      You can solve all of this by getting a VPS. With a VPS your site will just run slower than normal, and you won’t get banned.*

      * actually, you can theoretically get banned, but I’ve never heard of that happening in normal circumstances.

  4. WOW…. I was having this issue and the WordPress forums were a massive mess, with no one having a clue about this… always blaming a server setup.

    I am glad there is a solution… I ended up going with a fast VPS with tons of resources due to non-WP script needs, and that solved this problem, but it was a mess for several weeks on 2 hosts (one of which was hostgator).

  5. Hi Jeff,

    I encounter the heartbeat issue using Hostgator. I a bit panic when they temporary suspended my account. After I receive their email and look a way to solve my problem with them. I didn’t know that this really a big problem of heartbeat control. My CPU hosting was down and one of the reason was the heartbeat according to the Hostgator support. Currently, I’m still waiting for there feedback.

    btw, thankyou very much for sharing this post. I found a very useful reference to solve my problem.

Newsletter

Subscribe Via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.