Hosting companies will send a warning email to you when the CPU utilization of the hosting server exceeds certain limit. In case if you do not take any action after warning, your service will be terminated by taking down websites offline. Some companies also suspend your hosting account making it difficult to troubleshoot without access to hosting account. If you are the one analyzing server load then this article is for you. Here, I discuss about WordPress heartbeat API, how it affects the server resource utilization and how to effectively control it to safeguard your hosting account.
What is WordPress Heartbeat API?
WordPress heartbeat is an API service introduced from WordPress version 3.6. As the name indicates, heartbeat API sends continuous pulse (technically referred as tick) to perform periodic tasks. This helps the browser to talk with the server using AJAX calls and provide real-time information on the WordPress dashboard.
Practical Examples of Heartbeat API
WordPress developers use heartbeat API to perform routine tasks to provide real-time information by synchronizing data on the server and dashboard. Below are some of the examples might help you in understanding the heartbeat API better:
- Creation of periodic auto save and revisions when you create / edit posts in editor.
- Real-time sales data on the dashboard by ecommerce plugins like WooCommerce.
- Used to show notification on the WordPress admin dashboard.
- Shows the locking information of a post by other author. On multi-author sites when an author is editing a post, all authors will see a message that the post is locked by other author.
Why it Can Cause Problem?
Well, it sounds good and helps to provide the data instantaneously. But the problem is the heartbeat API sends ajax requests to the server using “/wp-admin/admin-ajax.php“. This can send large number of requests to the hosting server and resulting in high CPU utilization. As the server resources are limited especially on shared hosting environment, it may lead to your account suspension.
How to Monitor Heartbeat API?
You can monitor the traffic from heartbeat API, by opening WordPress dashboard and then look into server statistics simultaneously. You can find the traffic from “admin-ajax.php” page is generated by heartbeat API. However, the problem is that most of the companies do not offer real-time server statistics.
Though real-time monitoring is not possible, you can use one of the statistics tool from hosting account to get the past data. Depending on the server, the past data may be few hours delayed or even older than that. Let us check with the Awstats tool which is being offered by all cPanel hosting companies like Bluehost. Open Awstats app and choose the site you want to monitor. Navigate to “Page-URL (Top 25)” section to see top 25 pages accessed on your site.
As you can see “/wp-admin/admin-ajax.php” is on top of the list. This is an indication that the WordPress heartbeat API drains the server resources and time for you to take preventive action. You can do two things – one is to control the API and other is to completely disable it.
Controlling Heartbeat API with Plugin
There is a free Heartbeat Control plugin from WP Rocket guys available in WordPress plugin repository to control the heartbeat API. You can install the plugin and navigate to “Heartbeat Control” option under “Settings” menu. You will see two options like below:
- Control heartbeat locations – choose one of the available options from the drop-down how you want to control. There are four available options – Use default, Disable everywhere, Disable on dashboard page and Allow only on post edit pages.
- Override heartbeat frequency – choose either default value or choose any value ranging from 15 – 60 seconds with 5 seconds increment.
Unfortunately, WP Rocket guys did not update the free plugin for many years. Though it still works, it is a good idea to try premium WP Rocket plugin which offers heartbeat control feature as part of site optimization. You can simply go to “Heartbeat” section and check “Control Heartbeat” option. Below that section, you can select the behavior for reducing or completely disabling for backend, post editor and frontend.
If you are already using a caching plugin, then i would recommend Perfmatters which offers additional feature to set the frequency for heartbeat. Under “Settings > Perfmatters > General” section, you can enable heartbeat by leaving the “Disable heartbeat” option as “Default”. After that, set the “Heartbeat Frequency” as you desire like 60 seconds. In this manner, you can control the setup with more granular settings.
Stop WordPress Heartbeat Without Plugin
You can also completely stop the heartbeat API by adding the below code in “functions.php” file of your theme. This can be done especially if you are the only one author maintaining your WordPress site and do not want to install an additional plugin.
add_action( 'init', 'stop_heartbeat', 1 );
function stop_heartbeat() {
wp_deregister_script('heartbeat');
}
Implications of Stopping Heartbeat API
Whether you are using a plugin or modifying “functions.php” file, be aware of the following implications of completely stopping the heartbeat API.
- Auto save and revisions will not work on Classic editor. This means you need to press the “Save Draft” button manually to save your content. However, this will not have any impact on Gutenberg editor.
- In case if the internet connectivity is lost and you unknowingly pressed “Publish / Update / Save Draft” button then the content you made online will be lost.
- You may not be able to see real-time statistics and information if any of your installed plugin uses heartbeat API to update the content from server.
If any of the above point is a concern for you then instead of stopping the API, look for upgrading server resources to handle high usage.
Conclusion
If you are the one always doing some actions directly on the admin dashboard then it is high time to stop opening dashboard unnecessarily. You can prepare the content offline and copy / paste the content on the editor. In this manner, basically you will avoid sitting long time in front of the admin dashboard thus saving server resources. If you can’t avoid opening dashboard frequently, either use plugin to control the heartbeat API or completely disable it. This will help to reduce the server load and save hosting cost.
4 Comments
Leave your reply.