OlivierPierre04 wrote: Fri Jan 21, 2022 8:37 am
How it works
How to use this HTTP/2
HTTPAPI is an implementation of HTTP/1.1 -- it does not offer HTTP/2.
OlivierPierre04 wrote: Fri Jan 21, 2022 8:37 am
So what's difference between the first and subsequent requests in traditional HTTP/1.1 scenario?
From HTTPAPI's perspective, there is no difference, it is doing the same thing both times.
OlivierPierre04 wrote: Fri Jan 21, 2022 8:37 am
DNS Lookup: It might take more time to resolve DNS for the first request. Subsequent requests will resolve a lot faster using browser DNS cache.
Waiting (TTFB): The first request have to establish TCP connecting to the server. Due to HTTP keep-alive mechanism, subsequent requests to the same server will reuse the existing TCP connection to prevent another TCP handshake, thus reducing three round-trip time compared the first request.
Content Download: Due to TCP slow start, the first request will need more time to download content. Since subsequent requests will reuse the TCP connection, when the TCP window scaled up, the content will be downloaded much faster than the first request.
Thus generally subsequent requests should be much faster than the first request. Actually this leads to a common network optimization strategy: Use as few domains as possible for your website.
But, in general, I agree... DNS is probably the reason it's faster. This is not a part of HTTPAPI, however, but is a part of the operating system. The way it works is that the data gets cached after you look it up the first time, so the next time you do it, it'll already know the answer.
For example, the HTTP server that runs this forum is
www.scottklement.com. When I purchased this domain and set it up on my DNS server, I gave it a "time to live" (TTL) of 3600 seconds (that's 1 hour). So if you were to use a URL beginning with
http://www.scottklement.com (or https:// doesn't matter) most likely your computer doesn't know where
www.scottklement.com is on the network, so it would ask the operating system's DNS resolver. That resolver would ask the .com DNS server who runs scottklement, and then ask the scottklement.com server what the address is for
www.scottklement.com. So it has to communicate with multiple DNS servers to finally find out the IP address. This takes time -- but the good news is, once it has done it once, it'll remember it for 1 hour. So if you do 100 requests, the first one may be slower, but the other 99 will already know the address (provided they are made within 1 hour.)
Another way that the first request might be different is TLS (which people often refer to as "SSL", but SSL is obsolete). HTTPAPI sets up the secured sockets environment the first time, but then keeps it in memory, so subsequent requests won't need to set it up again unless the activation group gets ended. On my machine, the time this takes is VERY small, but it could be different on your machine, especially if yours is underpowered.
But, we don't have to guess... you can turn on performance logging in HTTPAPI and it'll show you what is taking the time.
Code: Select all
http_debug(*on: '/my/directory/httpapi_log.txt');
http_setOption('debug-level': '2');
OlivierPierre04 wrote: Fri Jan 21, 2022 8:37 am
HTTP/2 even introduces multiplexing to better reuse a single TCP connection. That's why HTTP/2 will give a performance boost in modern front end world, where we deploy tons of small assets on the CDN servers.
You aren't writing a web browser, are you?! Are you trying to download multiple documents simultaneously? If not, this isn't going to make a difference.