When investigating slow performance on your site, you may discover that your site has a high TTFB or Time to First Byte. Tools which show a “waterfall view” of your site’s load time like WebPageTest are a great resource to use to see whether your site is affected by high TTFB. “Time to First Byte” refers to the time it took for your browser to receive the first byte of data for your site to load. Each time a new user visits the site, their browser has to download your web page. There are several elements which can affect how long this process takes:
- Network latency
- Amount of traffic
- Apache configuration
- Dynamic content creation
WordPress and Dynamic Content Creation
WordPress, as a PHP-based content management system, uses PHP code and database queries to generate webpages. HTML sites, in comparison, can serve a single static page as a file without having to execute code or queries. This means, by nature, PHP-based websites will take more time and server resources to serve. But many sites still take advantage of the flexibility of languages like PHP to serve dynamic content. Thankfully, there are a few ways you can help improve the time it takes to generate your site’s pages.
Leverage Caching Systems
WP Engine’s caching system helps offset much of the time and resources needed to serve dynamic pages by storing each generated page as a static file in cache. This cached version can then be served to all subsequent visitors. Page cache is purged automatically every ten minutes. Since page caching helps offset much of the time and resources required to generate the requested page, it’s definitely best to do what you can to Improve Page Cacheability so that more requests are served from cache. This helps ensure your site is fast and scalable. We also recommend using our Object Caching layer, which caches the results of queries sent to the database. This means when your page does have to be generated on the server, the queries can be served much faster because the results are already stored in cache.
Load Smaller Files
If your web page is loading very large files, this content can also take a long time for the browser to download and display to users on the site. Whenever possible, serve scaled images (using a plugin like WP Smush or EWWW Image Optimizer Cloud), and minify your CSS and Javascript files (using plugins like Better WordPress Minify or Autoptimize). This will help decrease your TTFB, and allow your page to render faster. You can also defer your site’s Javascript to load lower down on your page, so that it doesn’t prevent the render of the page from happening (using a plugin like Above the Fold).
Reduce Queries
Often times one of the largest elements affecting your site’s Time to First Byte is the number of queries being run by your site’s themes or plugins to get information from the database. To help identify query bottlenecks, installing a diagnostic plugin like Query Monitor can help you find which plugins, themes, or settings are affecting the site load time.
Reduce Autoloaded Data
The wp_options table in your WordPress database stores many important settings for your site. This includes things like defining your site’s URL, theme, and active plugins. There is a column in this table for “Autoload” – when marked “yes,” this indicates WordPress has to load this row on every single page load. If there is data stored in wp_options which does not need to load on all pages by default, you may consider setting these rows to “no” for Autoload instead. To find out what your largest autoloaded rows of data are, you can use the following query under the SQL tab in phpMyAdmin on your wp_INSTALL database:
SELECT LENGTH(option_value),option_name FROM wp_options WHERE autoload='yes' ORDER BY length(option_value) DESC LIMIT 20;
Update Plugins and Themes
As simple as it may seem, plugin and theme authors often add optimizations in their updates. This could mean they have optimized the queries that their code runs to the database, or made updates that affect the efficiency of their PHP code. They also often release security updates. It’s important to stay on top of these updates to make sure your site stays secure and fast. Also keep in mind, some premium themes and plugins aren’t listed in the standard WordPress plugin database and may not show updates when they are available. Be sure to keep your licenses up to date, and periodically check for updates from your premium plugin or theme provider.
Apache Configuration
When a page is generated dynamically, the Apache web server is handling the PHP code to do this. Apache has a configuration file labeled .htaccess which defines the web server configuration for your site. This file has to be loaded with every uncached request to your site. Streamlining this file can also impact your site’s TTFB in a positive way.
Move Rewrites to User Portal
Many people use the .htaccess file to store redirects for their site. We recommend users enter these redirects in the User Portal instead. When they are placed in the User Portal, redirects are handled before they have to be loaded by Apache. This translates to faster Time to First Byte.
Limit Other Directives
There may be some PHP directives you want to include in your .htaccess file. However, many of them (like caching directives, gzip directives, and more) are already configured higher up on the server and do not need to be set in this file. Whenever possible, keep your .htaccess file as short and clean as you can.
Use Standard WordPress Rules
It’s best to leave the standard WordPress .htaccess rules as they are, without customization in the .htaccess file. The standard WordPress rules should always exist in the .htaccess file, otherwise the internal pages on your site cannot load.
Network Latency
While the sections above cover things you can change to help your site’s TTFB, unfortunately this section covers an option which is outside your own control. When your browser sends a request for a page to a site, while on the surface it seems to be making one request, it actually has to route through several network “hops” to get from your Internet Service Provider’s local and regional servers, eventually landing on the site’s server. If any of these “hops” are experiencing issues or are down, it could interrupt your connection to the site. You can use tools like a traceroute to help determine whether one of the “hops” from your local connection is experiencing latency.
Use CDN
Using a CDN will help distribute your site’s static files like images, javascript, and css, across a network of servers worldwide. This means if your server is geographically located in Belgium and a user from California visits the site, they receive your site’s content from a server location that’s geographically closer. This reduces the network latency between your end user and your site’s content.
Amount of Traffic
To some extent, the amount of traffic your site receives is also outside your control. However, the scalability of how your server responds to the traffic is something you can control. The higher your site’s cacheability, the more requests your server can serve at once. If your site is receiving a large number of uncached requests however, this can increase the server’s CPU load which will also affect the Time to First Byte. When you’re anticipating a spike in traffic, it’s important to ensure your site is able to scale to the amount of traffic you’re expecting.