Improving WordPress Transients
Modern websites can be complex, especially when a high traffic site offers advanced features that need more intensive server processing. WordPress has a few tricks up its sleeve to help developers improve performance, but as we’ve written about before, those tricks don’t always scale.
If you’ve been engineering performant sites with WordPress, you’ve probably encountered the Transients API—a basic caching method. What developers may not know is, at scale, the synchronous way WordPress handles transients can lead to performance issues that can result in a poor user experience. That’s where Async Transients comes in, our open-source Composer library for WordPress transients.
Wait, what exactly is the problem here?
Transients are a way of caching data for a set amount of time in WordPress. Unlike storing data in the object cache, transient data is stored only temporarily, with the expectation that it is updated periodically. Transients are always created with a set maximum lifetime, after which they expire and are deleted.
However, as typically implemented, transient expiration can interfere with visitor experiences. If a visitor action requires transient data, WordPress checks to see if it is available. If it isn’t, the data is then fetched to recreate the missing transient. During this process, a visitor would have the suboptimal experience of waiting, potentially for several seconds, while the data is fetched. If 100 visitors all trigger the same transient request at the same time, WordPress will process all 100 requests concurrently. This synchronous data fetching causes increased server load, resulting in poor, slower site performance.
Our Async Transients library remedies this issue by automating the transient update process. Instead of transients updating only when they are discovered missing, transient expiration times are monitored and, once expired, re-fetched in the background.
To avoid interruptions for site visitors during these data-fetching periods features requiring transients use slightly outdated data. Once the refreshed transients are fully processed, the data is updated behind the scenes.
The WordPress Transients API offers a simple way to temporarily store cached data in the database; our solution improves upon the feature by removing data-fetching delays and efficiencies—and can be implemented with minimal developer effort.
Find our Async Transients library—for download or collaboration—on GitHub.
Hey there — i really like the Async Transient plugin — it’s super helpful and works far better than TLC Transients which was quite brittle — two quick things — one is a self plug, I added a PR for the project that gives you a single interface to access these transients, makes it a far cleaner approach.
The second is if you are using non fpm workers for your PHP processes this will always return false and not cache at all — maybe fallback to using get_/set_transient if if ( ! function_exists( ‘fastcgi_finish_request’ ) ).
Great work either way.