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

48,656

Solution 1

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.

Solution 2

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.

Solution 3

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...).

Solution 4

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).

Share:
48,656
sparkle
Author by

sparkle

Updated on July 09, 2022

Comments

  • sparkle
    sparkle almost 2 years

    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

  • AD7six
    AD7six almost 10 years
    What are "native HTML caching capabilities"? The only references I can find to that term are two of your answers here on Stack overflow =).
  • Igal Zeifman
    Igal Zeifman almost 10 years
    Probably bad phrasing on my part. :) I just meant that it can do static and dynamic caching be default.
  • AD7six
    AD7six almost 10 years
    These terms don't mean much when tossed around without explanation. I'm assuming static means "normal" i.e. the CDN simply honours cache expiration headers, and dynamic means e.g. the cdn recognising that the response doesn't change, ignoring any relevant cache headers and caching anyway, and serving the cached result immediately whilst in the backend making a request to obtain a fresh copy of the content (replacing the cached copy for future requests) - i.e. similar to varnish's grace mechanism.
  • AD7six
    AD7six almost 10 years
    ... but anyway I'm not much of a fan of the "use a CDN" stock advice to be handed out to solve any and all frontend performance problems. A CDN is often a part of the solution - it's not a silver bullet to use in place of fixing the reasons a site is slow in the first place. It's akin to Q:"how do I fly a plane?" A:"Just use the autopilot." - it might "work" but nobody learns anything that way.
  • Igal Zeifman
    Igal Zeifman almost 10 years
    In my post I write: "Fact is, 9 out of 10 CDNs will actually contribute to the delay – not promote a solution..." I don't think CDN is a silver bullet. Far (very far) from it.
  • Patrick James McDougle
    Patrick James McDougle over 9 years
    Outputting the head as soon as possible, can be a big performance gain. If your head contains css files or scripts (even though it shouldn't), the browser could be paralleling those downloads. If the browser has to wait to parse the document, it doesn't even know what it could be downloading.
  • TobiMcNamobi
    TobiMcNamobi over 9 years
    Please elaborate a bit more on what cloudflare and cdn services are and what they do. And how they solve the problem in question. Also provide links, please - but do not make this a "link only answer".
  • ar099968
    ar099968 over 8 years
    send the first byte before ob_start <?php echo ' '; ob_start(); ?>
  • Ken
    Ken over 6 years
    This is what it was for me. I had a bad .htaccess file that increased my TTFB by 1000ms.
  • Brijesh Tanwar
    Brijesh Tanwar almost 5 years
    can you please share what you had been changed ?
  • Jintor
    Jintor about 3 years
    WOW !!!!! changing from <?php echo '<html' .... to <html><?php echo '....'; ?> made a difference to me, I was always at 300 to 400 (and wondering why) and this small change made it so that it's now 90-146 ;)
  • mihca
    mihca almost 3 years
    Same holds also for other template engines. For example, when using JSP the browser will not see its first byte before Tomcat has created the resulting HTML-Output from the JSP file.