Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 778
sparkle Avatar asked Dec 16 '11 20:12

sparkle


2 Answers

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.

like image 91
AD7six Avatar answered Nov 16 '22 03:11

AD7six


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.

like image 22
Adrian P. Avatar answered Nov 16 '22 02:11

Adrian P.