Use the WordPress Heartbeat API

mikevanwinkle's picture

A new WordPress performance bottleneck is looming:  admin-ajax.php.

More and more plugins are relying on the admin-ajax.php and the wp_ajax_ hook to deliver their content and functionality making WordPress more dynamic than ever. The problem is that if you have multiple plugins running all making their own wp_ajax calls without concern for what the others do, you end up with 4 or 5 server requests being required to render one page. This is a nightmare for server admins, especially since wp_ajax_ is often implemented as a POST request which allows it to bypass caching from cloudflare or varnish or what have you.

The solution is not to abandon wp_ajax_ but to come up with a better way to manage it. I really like what the Heartbeat API is doing. Instead of each client plugin managing it's own request, you can tie into an existing "heartbeat" to get the info you need. I would recommend all plugin and theme developers use the Heartbeat API approach if at all possible. At least until wp_ajax_ gets  streamlined.



This is an issue that I'm constantly battling and thinking about. The heartbeat api also uses admin-ajax.php, so while it will throttle and batch those requests, it could actually generate more hits (if multiple visitors leave a page open).

The only true scalable solution I've found is using an external service to handle the requests. For example to track and display downloads of an app I use node.js hosted at heroku. Node.js handles these kind of requests with ease and you can request the data from there when needed.

This is similar to how Jetpack tracks visitors (using and I believe it's the way to go!

I think this creates the opportunity for a WordPress specific DBaaS. Imagine a way to store data in an external cache (after page load) then pull it into WordPress automatically (using the companion plugin).

It's late for me and I'm on my phone, but I hope this makes sense!

Or possibly use nginx to create a way to write directly to memcache then retrieve it and write it to the database periodically? I may look into this, the issue here would be security.

Haha, this is the first time I noticed that you were reading my blog stallings ... woot!