📜 ⬆️ ⬇️

Latency when loading web pages

The post “ Something about the weight of the page ” made me want to write a small addition. Many people notice that optimizing the size of web pages is becoming less relevant due to the increase in bandwidth. Sooner or later, everyone will sit on gigabit, and it will be completely unimportant whether your page weighs 100Kb or 250. Perhaps it will be so. However, in addition to the channel speed, there is another parameter - delay or latency. And if the bandwidth of the channels with the development of technology can grow even more, then the latency has a physical limit: this is the speed of light in the optical fiber - about 200 thousand kilometers per second.

Although this speed is very high, but still not enough to forget about it, because our planet is rather big. Wolfram Alpha knowingly gives to requests for distances the time of passage of this distance by light in the fiber. Suppose you have a server in Tokyo, and the client came from Rio de Janeiro. Even if these two cities are connected by fiber along the shortest path on the surface of the Earth, the light will go 86.7 ms.

What does this mean for you? Suppose a user opens your page in a browser, for example, from bookmarks. From the moment he clicked, until when the signal reaches the server, a minimum of 86.7 ms will pass. And just as much for the signal to come back. Your page cannot load faster than 173.4 ms, even if it weighs little and is cached by the client (to find out that it has not changed, you still have to ask the server).

It would seem that 173.4 ms are little things in life. But not everything is so simple. Many pages require scripts, styles and images, without loading which the page is non-functional. The browser cannot start all this without downloading the HTML itself at least a little bit (links to scripts in HTML). So, we will need 86.7 ms (HTML request) + 86.7 ms (HTML retrieval) + 86.7 ms (everything else request) + 86.7 ms (everything else retrieval) - at least 346.8 ms.
')
Of course, and 346.8 ms you can wait. It happens, however, that for a normal page display, an AJAX request is required. For example, the main information block on your page can be automatically updated from the server via AJAX. In order not to complicate the logic of the server, you do not give it away when you first load the page, but leave it empty, but immediately call the update function. There are many such sites. Say, on rts.micex.ru , indexes, quotes and other blocks are loaded this way. Now, if you come to see actual quotes, you will have to wait for the HTML download, the JS download, and then the AJAX request to complete - at least 520.2 ms. The half-second delay is already uncomfortable though, of course, you can suffer.

It also happens, however, that some information is loaded by an AJAX request that is waiting for the results of another AJAX request. Sometimes this is due to the lack of focus on the interaction between the client and the server: to send the next request, we need information from the previous one. More often, this is due to the laziness of the developer, who did not understand the functions that allow to execute certain code after completing several requests at once (or did not write such functions himself, if Vanilla JS prefers), and therefore simply sends the second request to onreadystatechange of the first.

This wonderful picture of the download is demonstrated by the start page of one online banking system, which I use:

Here additional resources and scripts are loaded via 9 consecutive AJAX requests. Add to this a request for HTML and a request for a script that loads all this stuff - the minimum delay is obtained when we set up the client and server in 1.9 seconds. That's what a thing: even though the terabit Internet all over the Earth, spend, make infinitely fast servers and hard disks, and a user from Rio de Janeiro will still wait two seconds because of the crooked programmers.

Even if we assume that engineers will learn to send signals not across the surface, but through the Earth and at the speed of light in a vacuum (say, sending a beam of relativistic neutrinos, and putting a detector on that side), the latency will drop a maximum of two times. But, I think, earlier the Internet will spend on the Moon, than it will be made. And from the moon such a site will load themselves count how much.

Of course, you may have few clients from the other end of the Earth, but the wires do not lie along the shortest path, and the delays on repeaters and routers are quite tangible.

Well, the obvious useful tips in conclusion:

Source: https://habr.com/ru/post/163903/


All Articles