This is the 12th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021.

DNS optimization

Commonly used means are:

  1. Reduce the number of domain names, but this approach conflicts with CDN optimization methods
  2. The DNS query beforehand
    1. The common way to write this is to add it to HTML<link rel="dns-prefetch" href="https://abc.com">
    2. Less common is to add in the HTTP response headerLink: <https://abc.com/>; Rel = DNS - prefetch.

Time granularity: a DNS query takes about 20 to 120ms. Related command: time nslookup baidu.com The real time is the approximate time of a DNS query.

Connection: keep-alive

Earlier we said that a TCP connection would be closed after sending an HTTP request and response, and we thought it was wasteful, why close it? Isn’t it good to continue to “hold on” and wait for the next HTTP request?

To recap, establishing a TCP connection can take anywhere from tens to hundreds of milliseconds (depending on distance). In the early days of the Internet, servers closed TCP connections by default after an HTTP response to save resources. Since web pages at the time contained very few requests, perhaps only one HTML, this was the right thing to do at the time.

However, as user requirements increased, a page often contained dozens or hundreds of requests, so we needed a mechanism to keep the TCP connection closed for a period of time after the server responded, and the client did not close the TCP connection for a period of time after receiving the response.

Connection: keep-alive Connection: keep-alive Connection: keep-alive

  1. The client adds this field in the request header to indicate that the CLIENT will keep the TCP connection for a period of time after receiving the response.
  2. The server adds this field in the response header to indicate that it will keep the TCP connection for a period of time after sending the response.

If one party does not support this function, simply change it to Connection: close. The default value of this field in HTTP/1.0 is close, and the default value of this field in HTTP/1.1 is keep-alive.

How long do YOU keep the TCP connection open? The default duration is generally 60 to 300 seconds for browsers and 5 to 20 seconds for servers. The two parties can use keep-alive: timeout=5, Max =1000 fields to inform each other of their configurations. Timeout =5 indicates that a maximum of five seconds are held, and Max =1000 indicates that a maximum of 1000 requests are supported. Some browsers follow the server’s configuration, while others don’t (IE, for example).

The protocols we use in practice are HTTP/1.1 or HTTP/2.0, which by default add Connection: keep-alive to requests and responses without requiring the programmer to do anything.

After this function is enabled, TCP connections remain for a longer period (say 5 seconds) after the first HTTP response ends.

  • If there is a new HTTP request within five seconds, the timer is reset and held for another five seconds after the HTTP response ends, and so on until the 1000 limit is exceeded.
  • If there are no new requests after 5 seconds, the connection is closed.

To summarize

  • If both parties do not addConnection: Keep-AliveField, the server closes the TCP connection after receiving the response, and the client closes the TCP connection after receiving the response.
  • If both sides addConnection: Keep-AliveField, the server retains the TCP connection after receiving the response, and the client retains the TCP connection after receiving the response for sending the next request.
  • If one party adds the TCP connection and the other party does not, the unadded party actively closes the TCP connection at some point.

Note that the server and client programs need to program this field to achieve the desired functions, but these functions are already packaged by Nginx, Apache, Chrome, Firefox and other software, and do not need to be cared about by the backend programmers.

One last word

If this article is helpful to you, or inspired, help pay attention to it, your support is the biggest motivation I insist on writing, thank you for your support.