This is an old revision of the document!
Optimizing time to first byte
According to Wikipedia, TTFB “measures the duration from the user or client making an HTTP request to the first byte of the page being received by the client's browser. This time is made up of the socket connection time, the time taken to send the HTTP request, and the time taken to get the first byte of the page.”
In short, it is a commonly used performance measurement of how long it takes for the web server to start sending data. It's often misused as a general performance metric: for example, more aggressive gzip compression might worsen your TTFB, but improve your overall page load time. It's useful as a diagnostic indicator though, and this page will go through some common reasons your TTFB might be very high.
High resource usage
One common reason for high TTFB is high resource usage on your server. All CPU cores might be busy, or your server might be swapping if it's running low on RAM. Some things to check for:
- Run
top
: how's your CPU usage and RAM looking? Alternatively, for RAM, you can also usefree -m
- Run
df -h
: are any partitions out of disk space? - What about your bandwidth usage? You can use tools like
nload
to check, and your provider will likely have bandwidth graphs too.
Many queries
Your TTFB may be high if your website makes very many database queries, or consists of very unoptimized application code. A common example of this is WordPress websites that use dozens of plugins. If possible, look into reducing the workload for your application. Caching may also help alleviate this: consider using LiteSpeed Cache if you aren't already.
ModSecurity
Sometimes, having a large number of ModSecurity rules, or very complex ones, can lead to a significant rise in TTFB. This is particularly true with the SecRequestBodyAccess On
directive. When diagnosing high TTFB issues, you may want to temporarily disable ModSecurity, to rule this possibility out.
Slow DNS
Rarely, a slow DNS server may be responsible for a high TTFB, especially in artificial benchmarks, where DNS is not cached. Several websites are available to test the speed of your site's DNS response, such as https://www.dnsperf.com/dns-speed-benchmark
Slow servers
This is especially common in VPSes, where some providers severely oversell, or use old setups. You may also encounter this, for example, if your server is on very old, very slow hardware. You may need to migrate to another provider.