sparkle
sparkle

Reputation: 7398

How to reduce the "Time for the first byte" on my website?

if you go to the homepage, you'll see that before the page loads, the browser waits ...

How i could reduce Time for the first byte?

enter image description here

Upvotes: 20

Views: 52354

Answers (7)

Supaiku
Supaiku

Reputation: 121

Errors in .htaccess can also greatly increase TTFB.

I had to remove some old lines of code left by Wordfence to resolve my 8-12 second TTFB (now 500ms).

Upvotes: 3

Adrian P.
Adrian P.

Reputation: 5228

I've dealt with huge TTFB (8-10 seconds) and searching desperately for a solution. After searching and searching without any success I decided to take a closer look to my PHP code and database indexes.

The output buffering solution lower my TTFB a bit but not enough. I had users complaints again.

The real problem is the server processing time (DB queries and PHP loops) and the HTML source you generated.

Now, I suggest take these steps:

  1. Take a look at the database indexes. Be sure you use the proper indexes for "all data" you return. Use Explain to verify if your index is used, which one is used and how it is used.

In my case, I return an array of objects and I checked my indexes for my main table. All looked OK but I forgot that my objects include other smaller objects from other tables. These tables were not properly indexed. Hence my huge TTFB. I just pass from 8 sec to 2 sec just my adding the proper index to the right tables.

  1. Take a look at your PHP code.

You may have some loop in loop which can be slow to process. You should use a PHP MVC framework. Your choice. I will not name any.

Avoid such code even it is working. I know, some PHP4 programmers will say it is good. :)

$query = "SELECT something FROM table";
$result = mysqli_query($mysqli, $query);

if($result) {
    while($row = mysqli_fetch_assoc($result)) {
        $query = "UPDATE other_table SET something_else = "'.$row['something'].'";
        $result2 = mysqli_query($mysqli, $query)
    }
}
  1. Pay attention to the generated HTML code.

For example, you generate Javascript code through PHP loops. The logic is OK. The loading time is not. Let's say you return 100 rows into a table. For each row you have only 5 possible actions (change status, edit, delete, duplicate, print). That means 5 jQuery dialogs (HTML divs, with controls) and 5 JS scripts multiply by 100 rows = thousands of lines of code to be written on that page. My case, over 32.000 lines on my HTML code of 4MB. Just passed from 2 sec to under 1 sec after I have put all these dialogs on proper JS functions.

In conclusion, (if you are still reading this :)) don't search for some magic functions to reduce your TTFB. Search your code and your database.

PS: Some other things will help for speed increasing: browser caching and compression, use of CDN, minify HTML, CSS and JS, defer parsing of JavaScript, combine images into CSS sprites etc. Use Google Page speed and Google Audits for more performance suggestions.

Upvotes: 7

ginger
ginger

Reputation: 1

you can use cloudflare and cdn services for TTFB. If you dont take proper feedback change your host server.

Upvotes: -2

AD7six
AD7six

Reputation: 66378

Nobody can give you a detailed answer without you showing the code responsible for generating the content of the site - because that's where the delay is.

However, since the site is using php, you are most likely using output buffering

Given that's the case, the following code will give a TTFB of (network latency +) 2s:

<?php ob_start(); ?>
<!doctype html>
<html>
    <head>
        <title>Slow to Load, Slow to finish</title>
    </head>
    <body>
    <?php 
        sleep(2); // Simulate slow processing
        echo "Body"
    ?>
    </body>
</html>

Whereas this will give you a TTFB of (network latency +) 0s:

<!doctype html>
<html>
    <head>
        <title>Fast to load, Slow to finish</title>
    </head>
    <body>
        <?php ob_start(); ?>
        <?php 
            sleep(2); // Simulate slow processing
            echo "Body"
        ?>
    </body>
</html>

The time to load the whole page is the same in both cases - only where the delay is changes. If you are specifically focussed on reducing TTFB (why), that should give you enough information to investigate further.

IMPORTANT: There are a lot of frontend changes you should make before focussing on TTFB.

Upvotes: 19

Igal Zeifman
Igal Zeifman

Reputation: 1146

Probably the most effective solution is to use a CDN with native HTML caching capabilities (static and dynamic). TTFB relies on your ability to quickly process the HTML on the origin server, you can skip processing time altogether by serving a fresh cached copy from CDN.

I wrote a post about it recently, which looks into TTFB delay factors and average load time of different resources (based on data gathered across 1B sessions). You may find it useful: http://www.incapsula.com/the-incapsula-blog/item/809-using-cdn-to-improve-seo-and-ttfb

Upvotes: -1

Alexander Holsgrove
Alexander Holsgrove

Reputation: 1803

I think it depends on what tools you use to measure this sort of data. When I used webpagetest.org - the time to first byte was 292 ms which is good. Perhaps you should re-run your check?

Part of this number is down to where you are in relation to the server - the more hops you have to do - the larger this number. It's also about the server hardware and connectivity - usually this is something you can't really control. You may want to look into other hosts, but I'd first run a few more tests - get your friends to test your site out on webpagetest.org (or similar) and see what values they get.

Upvotes: 0

Frunsi
Frunsi

Reputation: 7157

The delay is caused by the server-side script which generates the index page.

By a quick look at your website, I can guess that the website is using PHP. So, the delay is caused by something contained in your index.php script.

The hosting, the network, the hardware and the HTTP server (Apache) are definitely NOT the cause. Your graph shows that static files (.css, .js and so on) are delivered rather fast.

So, for more details you should provide more information (the slow execution of index.php can have many different reasons...).

Upvotes: 4

Related Questions