Time to first byte: what is it and why is it important

Now I am working on a project for one client. This is an e-commerce site, so I’m very interested in some aspects of performance. For starters, these are various indicators that characterize the loading time of a site. Next - this is the start time of the page rendering, which is important for those visitors who, after entering the site, want to see its contents as quickly as possible (naturally, all site visitors fall into this category). Among the performance indicators that interest me are those that reflect the specifics of my client’s activities. For example: "How fast does the main image of the product load?" An analysis of all these indicators can provide valuable information about the state of the project.







However, there is one indicator, which, as it seems, front-end developers often do not pay due attention. It's about time to first byte (Time to First Byte, TTFB). You can understand this, you can and at least partially forgive developers this attitude towards TTFB, especially considering the fact that they see this indicator as something that depends only on the backend of the projects. But if we try to literally briefly express the problem regarding this indicator, then we can say the following: "Although a good TTFB value does not necessarily mean that the site demonstrating it can be considered fast, a poor TTFB indicator almost certainly indicates problems with the project's performance."



Even if we take into account the fact that the front-end developer may be in a position in which he is not able to independently influence the backend and TTFB, it is important to take into account the fact that high TTFB values ​​can significantly affect the site’s performance. As a result, the efforts of a front-end developer striving for website speed will resemble a catch-up game. This applies, for example, to image optimization, and to minimizing the volume of materials that make up the most important sections of the project, and to asynchronously downloading web fonts. This is not to say that, knowing this, you can give up and abandon front-end optimizations. But if the TTFB is too high, then all such optimizations are reminiscent of trying to fix a problem in conditions when it has already done harm, and when it is too late to fix this problem. As a matter of fact, that is why it is very important for those who are developing the front-end to closely monitor the TTFB indicator, and it is very important, when its values ​​are too high, take measures to improve it.



What is TTFB?









TTFB does not look very informative ( full size image )



TTFB is an indicator that looks, to put it mildly, opaque. So much is influenced by him that I have the feeling that we are always shrugging off his serious analysis. Many speculate that TTFB is just the time it takes the server to prepare a response, but it is, in fact, only a small part of what affects TTFB.



The first thing I would like to draw your attention to is that upon learning what people are usually very surprised. We are talking about the fact that TTFB includes the time that the request from the client goes over the network to the server, and the time it takes the server response path to the client. This is the so-called “round trip time” (RTT). TTFB is not just some time the server spent preparing a response. It is also the time that is spent on the way data going from client to server and from server to client (this data, of course, contains the “first byte” that interests us).



Now, armed with this knowledge, we can easily understand the reason that when viewing sites from mobile devices, the TTFB indicator is often simply indecently large. It is quite possible that earlier in such situations you would ask something like this: “I’m sure the server does not know that I’m viewing the site from a mobile. How then does it increase TTFB? ” The reason for this is that, as a rule, mobile network connections are high latency connections. If the RTT indicator, which reflects the time required for the data to travel from the phone to the server and back, is, for example, 250 ms, then TTFB will increase by the corresponding value.



If I would like readers of this material to take out only one major idea from it, then I would formulate this idea as follows: "Network delays affect TTFB."



What else affects TTFB? In fact - a lot of everything. Here is a far from complete list of what contributes to the formation of this indicator. The items on this list are in random order.





A 0 ms TTFB is a pipe dream. Therefore, it is important to note that there is not something in the list that always negatively affects TTFB or always worsens this indicator. This list is best understood as a description of the TTFB structure of a project. My goal is not to criticize certain technologies, but to show how certain technologies can influence TTFB. And, to be honest, given how much things happen before the client receives the first byte of the response from the server, it is already surprising that the sites are generally loading.



Uncovering Mystery with TTFB



Now, hopefully, TTFB doesn’t look that mysterious anymore. And if you spend a little time on the implementation of API Server Timing , then you can start measuring tricky server time indicators and sending them to client systems. This will allow web developers to detect and eliminate potential performance bottlenecks that were previously hidden from their eyes.



The Server Timing API allows developers to extend query responses with the optional Server-Timing



HTTP header. It contains time information measured by the application itself.



It was this mechanism that we used last year when working on the BBC iPlayer.









A new Server-Timing header can be added to any answer ( full size image )



Note that Server Timing also puts stress on the system. In the course of its work, you need to measure the relevant indicators and fill out the Server-Timing



header. The browser only allows the front-end developer to view this data using the appropriate tools.









Now, right in the browser, you can see the TTFB structure ( full-size image )



If you want to implement API Server Timing, take a look at this material .



Summary



It is very important that web developers understand the extent to which TTFB affects what they call "site performance." Time to the first byte is a certain border, after crossing which we can talk about website optimization. The lower this indicator, the better.



Dear readers! Do you optimize your web projects with TTFB?








All Articles