Reducing time to first byte (TTFB)

Posted on by in Web Design, Wordpress

We have also posted on Time To First Byte for WordPress and how the right host can affect your TTFB.

Time to first byte (TTFB) is the time it takes for a browser to start receiving information after it has started to make the request to the server. In some situations, as much as 75% of the time spent loading a page can be taken up by waiting for the first byte of data to arrive. You can test your time to first byte by using something like Byte Check or Load Impact

There are a few factors that come into play with TTFB, but here are some that are mostly out of your control;

  1. The network the browser is on
  2. The server that your website is hosted on
  3. The network the server is on
  4. The load across all networks
  5. The distance between servers

Basically the speed of your server, the network your server is on, and the speed of the network the browser is based on are fixed things that are difficult for you to change, but have the largest part to play when it comes to TTFB speed. There are a few other things that do count however, especially where WordPress websites are concerned.

Optimise Your Database

Mainly the size of your database affects that time taken to retrieve information. There are two ways to quickly reduce the size of your database. One is to use a plugin like “delete revisions” to remove reduntant post revisions. This will significantly reduce the size of old databases. The next thing to do is to optimise your table. If you have never done this, some tables have have up to 10MB of overhead they are not using.

Most of these issues will go away however if you use a caching plug-in like W3TC that will remove the need for persistent database queries.

Take note of the host you are with

Even if you are using a CDN, your host will have an impact on your time to first byte. We ran a test on 4 identical websites using 4 different web hosts based here in Australia, and we found there was about a 20% difference in time to first byte depending on the server you are on. Sometimes it is both impractical and too costly to move to a new hosting provider, however it is something you need to look at next time you are in the market for a host.

Use Amazon Cloud Space

Using a Content Delivery Network is worth your while because not only can it reduce the bandwidth burden on your current host, but it can improve the speed of your website by a huge amount. You can self host as a CDN, but it won’t reduce your burden, it won’t change the TTFB, and it might only reduce your overall website speed by 10%.

Using a CDN will not directly affect your TTFB, but it will reduce the impact on your server, and so if there are concurrent requests, or lots of pressure, placed on the server, this will reduce the burden.

We started out using the normal Amazon S3, but we actually found it to have a slower load time than our own server. However when we switched to Amazon Cloud Space, we went from an overall  TTFB of 4 seconds to 1 second. That is a 75% reduction in load time just by using the Cloud Space.

Using FastCGI to process PHP – Reduce TTFB

We have recently set up our WordPress site to use FastCGI instead of CGI to process PHP. We have found that this has reduced our TTFB from around2 seconds to 200ms.

FastCGI is a protocol which allows a web server such as apache to invoke another program to serve a page, but without the overhead of CGI (which needs to fork a process for every request) or the security problems of running scripts in-process in the web server (discussed above).

To set this up, start by creating a directory called fcgi in the root of your web site. Inside it create a script called php, containing the following:

#!/bin/sh
PHPRC=/etc/php5/fcgi exec /software/bin/php5 "$@"

Make it executable. Also create a .htaccess file in the same directory:

Options +ExecCGI
SetHandler fastcgi-script

This tells apache to run scripts in the /fcgi/ directory as persistent FastCGI processes. The next step is to have your individual PHP files processed using this interpreter. To do this, add the following lines to a .htaccess file in the root directory of your site (create one if none already exists):

AddType application/x-httpd-fastphp .php
Action application/x-httpd-fastphp /fcgi/php
View ratings
Rate this article
Share

6 Responses to “Reducing time to first byte (TTFB)”

  1. Vechtsportwinkel.com 24 November 2011 at 8:00 am #

    Very nice results, just by CDN. Since we are using a lot of dynamic content and high a high TTFB I was wondering if it is still feasible to go for CDN.

  2. Robert Steers 7 February 2012 at 12:50 pm #

    We have dynamic content here and we still use CDN. I don’t think most people will know the difference, unless you are getting millions of pageviews, and you are updating content every second.

  3. Famous Quotes 6 March 2012 at 10:04 pm #

    Hi Robert,

    My website gets update almost every single minute and i have found that my website really take lot time for first byte. But i have heard that the CDN cost a lot of money. Can you please suggest me some cheap and nice easy CDN provider for beginner as i have never used them before.

    • Robert Steers 7 March 2012 at 12:37 pm #

      Amazon s3 has a cheap CDN. I think you can also use coral or cloudflare for free CDNs.

  4. Damien 23 May 2013 at 12:28 pm #

    Hi Robert – interesting statistics. Is Cloud Space referred to in your article what is now known ad CloudFront? We’re using cloudfront with w3 total cache but have a very slow TTFB. Just trying to work out if it’s the cdn or hosting or what.

    • Robert Steers 23 May 2013 at 12:33 pm #

      Yes, it is CloudFront. Cloud Front will not have a massive change on your TTFB directly. First, you need to use it to reduce the burden on the server, by hosting most of the resources there. After that the first few seconds of TTFB come from the server you are hosted with, so CloudFront is unlikely to change that without moving your whole site to Amazon. TTFB is more about the pipes and the initial systems that work to resolve your request than anything else.


Let us know what you think about this article!