What is TTFB and how to speed it up?
What affects TTFB?
Time to first byte depends on a few factors and the performance of a web server is only one of them. Network issues like low network throughput (a.k.a not enough bandwidth) and high latency play a big role in how fast a web server can respond. DNS is also part of TTFB. Name servers need to be queried for the IP address of the website that is being visited, so the latency of the name server and how fast it can respond can have a big impact on the time to first byte.
How to fix web server TTFB?
1. Check and optimize the web server
Your web server might be slow to respond if it does not have enough resources to process all incoming requests at the same time. These requests would then be put in a queue in the order they come in. In extreme cases this queue could become so long that requests might start to time out. While the general advise on the internet would be to simply increase the timeout setting on the web server, this is almost never the right thing to do. This will not speed up your website and will not fix the problem, but will only hide the issue from your error logs temporarily. Instead, check that enough CPU is available to the server to process all incoming requests and that there is enough free RAM. Your server may be hitting CPU or RAM memory limits during peak hours which would trigger errors and would cause it to respond slow, rising your TTFB. The speed of the disk drives of the server could also cause the time to first byte to skyrocket. This could happen if there are too many read and write operations which cause disk overload, forcing the entire system to wait for a slow disk to respond to requests for reading a piece of information. Look at the error log of your web server search for clues. Sometimes the fix could be as easy as increasing the number of workers that Apache or Nginx use to respond to requests. The log would clearly state if that is the case. On Linux operating systems, use the system load indicator to figure out if the issue is on the server, or somewhere else (for example, on the network):
The system load should always be less than the number of CPU threads available to the server. For example, a server that has 2 CPU cores should always have a load less than 2.00. The lower the system load, the faster the server actually runs. If system load is high, tools like top, iotop and htop can be used to diagnose where the problem is.
2. Confirm the network is not the bottleneck
Temporary network issues could affect TTFB. These could be caused by bad routing, where all traffic to and from the web server takes a sub-optimal or outright slow route, or a route that is highly congested. Sometimes there may be small amount of packet loss on that route, just enough to cause requests to be retried but not enough to cause an outage. To identify if there is packet loss, tools like mtr (Linux) and Winmtr (for Windows) can be used. Packet loss should then be reported to the hosting provider as it can not be fixed on the server itself, unless it is caused by a firewall installed on the server. If you have a firewall, check it’s logs to find if it intervenes with the traffic.
3. Check the speed of the name servers
The speed at which a visitor is able to resolve the IP address of your website is crucial to it’s TTFB. If the name servers that are being used for your domain are slow to respond, or they are physically too far from your visitors, the browser of the visitor may need to wait for too long to obtain the IP address of your web server. To check the latency of the name servers, the ping tool can be used on both Linux and Windows. Latency beyond 40 milliseconds will have an impact and latency above 100ms should not be tolerated. To check the actual time it takes to get a full response with all the data, a tool called dig comes handy on Linux systems. The speed at which a name server replies could be checked also with tools like WebPageTest. If name server issues have been confirmed it might be best to look for another name server provider. There are free name server providers out there with reasonably good performance.
4. Optimize website code
More often than not the time to first byte can be greatly improved by optimizing the code of your website. With Wordpress for example, the number of installed plugins would have a direct effect on the TTFB. If numerous plugins need to do their job at the same time in order for your website to be loaded, you might want to disable those that are not absolutely necessary. Security plugins usually tend to have the biggest impact on website speed and TTFB and while your Wordpress site is more secure with them, looking for a solution that does not involve plugins is recommended. Ideally, your hosting provider would provide a firewall that would allow you to insert rules which block known Wordpress threats. If your website is custom, then it is always a good idea to review your code and optimize where possible. Try to minimize the number of database queries, put proper indexes in tables and avoid processing large objects.
5. Enable caching where possible
Caching plays a big role in TTFB because it minimizes the work that your server needs to do before it can start to respond to a request. In most scenarios, most of the pages on your website do not change every time a visitors lands on them and as such should be cached. With caching enabled, a page would require processing and database queries to be made only the first time when a visitor loads it. This page is then stored in cache and served to all other visitors from there. Because of the reduced processing your web server will be able to respond fast to all requests. It will also be capable of serving more visitors at the same time. Caching can be applied to the database queries with MySQL’s query_cache, and with memcached. Wordpress does have plugins that enable memcached. When choosing which page caching solution to go with, it is always recommended to avoid plugins as much as possible. For example, installing Varnish on the server and then using a Varnish plugin for Wordpress which handles the update of the cached pages will provide dramatic decrease in TTFB and significant improvement in the loading time of the entire page. It will do so with very little resourcers compared to a popular caching plugin. Varnish can also be used as a very effective firewall against known Wordpress attacks. It’s ability to process large number of requests at the same time make it a great Layer 7 DoS/DdoS attack mitigation solution.
6. Use CDN
While using a content delivery network is always recommended for best visitor experience and SEO, a CDN can provide additional improvement of the TTFB. As we already covered, the less requests the web server needs to process, the faster it can return a response. By offloading all static assets, including javascript, css, images, video and files to a CDN, the web server has more resources available for processing and database queries, allowing it to respond faster. A content delivery network will also minimize and nearly eliminate the possibility of network bottleneck, since the bandwidth available to the web server will not be used to deliver the large images and videos. Beyond just TTFB, a CDN provides great reduction in the time it takes to load a web page completely thanks to it's global network of edge servers, which is what you are trying to achieve in the first place.