Diagnosing admin-ajax.php Slow Responses in WordPress

admin-ajax.php slow

With each CMS platform comes its own unique issues that you have to troubleshoot and deal with as a web developer or business owner. While WordPress might be the most widely used CMS, with hundreds of thousands of plugins and themes available, this also can result in it being one of the trickiest to optimize correctly as there are so many different variables that could be introducing delays. If you have ever run a website speed test on your WordPress site, then you have probably encountered references to admin-ajax.php showing up in your results. Today we want to briefly explore how to better diagnose admin-ajax.php slow server responses and what your options are as a WordPress site owner.

What is the admin-ajax.php File?

In WordPress 3.6 the WordPress heartbeat API was introduced to allow WordPress to communicate between the web-browser and the server. It uses /wp-admin/admin-ajax.php to run AJAX calls from the web-browser to keep track of what is going on in the dashboard. For example, if you log in to your WordPress site, you will immediately see a request for admin-ajax.php in your logs. The benefits of this are improvements upon session management, revision tracking, and auto saving.

WP heartbeat API from the Make WordPress Core development team: Initially it will be used for autosave, post locking and log-in expiration warning while a user is writing or editing. The idea is to have a relatively simple API that sends XHR requests to the server every 15 seconds and triggers events (or callbacks) on receiving data. Other components would be able to “hitch a ride” or get notified about another user’s activities. This can be used to block simultaneous editing of widgets and menus or any other tasks that require regular updates from the server.

There are two different types of issues that can cause admin-ajax.php slow server responses. The first is a backend CPU issue and the second is more of a frontend issue where you will notice third party plugins polling this file in your website speed tests. There was also a slight update from the core team in WordPress 4.1 to improve the “go to sleep” and “wake up” functionality, but a lot of people still seem to be experiencing issues.

admin-ajax.php Backend CPU Issues

The first issue we encounter is on the backend. The pulse from admin-ajax.php is only around 98 bytes, which is very small. However, over time this begins to add up. A lot of people run into backend performance issues when working with cheap and shared web hosts who have CPU restrictions in place. WP Tavern experienced this problem first hand and had to migrate hosts. Be sure to do some research into which WordPress hosting service is best before making a final decision.

InMotion Hosting wrote a great article on how the Heartbeat API affects your CPU loads. Basically, they logged into their WordPress dashboard and watched the access logs. They noticed that with the dashboard in focus the requests were spaced out to every 60 seconds. With the dashboard out of focus, the requests were spaced out to every 120 seconds. This is a lot of requests if you just left your dashboard open.

And each POST request had a corresponding PHP script execution on the server using CPU time. They left their dashboard open for 30 minutes and there were 25 PHP script executions with a total of 5.77 CPU seconds.

And that was just one user. If you have multiple people logging into your WordPress backend the number of requests from the API increases simultaneously.

Heartbeat Control Plugin

So how do you fix this? Well, Jeff Matson, a WordPress developer, created a great free WordPress plugin called Heartbeat Control to manage the frequency of the heartbeat API. You can also disable the heartbeat APi entirely if you want, however we don’t recommend doing this. You can download Heartbeat Control from the WordPress repository. Once you install it you can find it in your dashboard under “Settings.”

On the Heartbeat Control configuration page you have the following options for heartbeat locations:

  • Modify heartbeat
  • Disable heartbeat
  • Modify heartbeat

You can also change the location where you want the above heartbeat settings to effect such as:

  • WordPress dashboard
  • Frontend
  • Post editor

You can then change the frequency from 15 seconds all the way up to 300 seconds. By changing the interval to 300 seconds you are less likely to experience server performance issues.

heartbeat control settings

admin-ajax.php Slow From Third Party Plugins

The second issue we run into is third party plugins utilizing admin-ajax.php for certain AJAX functionalities such as firing up popups or social counters. Some plugins make tons of POST requests to this file which add up super quick. How can you diagnose which plugins are using admin-ajax.php? Well, one quick way, without digging through a bunch of code, is to fire up up Chrome DevTools and look at the individual network requests.

Example 1 with WP Notification Bar Plugin

In this example below we can see there is some waiting time after DOMContentLoaded.

Note: Just because there is a request doesn’t mean it is necessarily slowing your site down, as some things can load asynchronously. AJAX loads after the page loads, but it is still not efficient when plugin developers are making tons of requests. Even when using AJAX great care should still be taken to minimize the number of AJAX requests that are employed.

Then take a look at the “post form action” on the HTTP response header. In our troubleshooting the plugin almost always reveals itself in the name of the action. For example, you can see the action is mtsnb_add_impression.

If we do a quick search in the source code on the page we can quickly see the class with mtsnb and now we know from that it is our notification bar plugin at the top of my page polling admin-ajax.php. Also the form data bar_id is another hint.

Summary

So the next time you run your WordPress speed tests just be aware of how admin-ajax.php slow responses might be affecting your site, both from a backend CPU utilization aspect and also third party plugins making repeated AJAX calls. It could be time to find a new alternative plugin. Another easy way to fix CPU loads due to the heartbeat API is to simply upgrade to a better web host or VPS. If you want to discuss admin-ajax.php further, make sure to hop in our community thread.

Related Articles

Diagnosing admin-ajax.php Slow Responses in WordPress was last modified: May 30th, 2018 by Brian Jackson
Share This