How many HTTP requests can a TCP connection send? In this context, let’s talk about the relationship between TCP, HTTP, and browsers and the optimization of request handling.

Origin of TCP and HTTP

We know that TCP corresponds to the transport layer and HTTP corresponds to the application layer. In WEB projects, the HTTP protocol is based on TCP.

When the browser loads a web page from the server and makes an HTTP request, a TCP connection needs to be established. When the data request is complete, the TCP connection is immediately disconnected.

However, as time goes by, the HTML web content becomes more and more complex. There are not only content, but also JS, CSS and image resources. The TCP connection is established for each resource request, and the efficiency will be very low.

At this point, keep-alive is used to solve the problem of inefficiency.

This article on how many HTTP requests a TCP connection can send is essentially about addressing communication inefficiencies.

Here’s a look at some common interview questions to help uncover what’s involved.

Question 1: After the browser establishes a TCP connection and completes an HTTP request, does it disconnect?

The Connection attribute in the HTTP Header determines whether the Connection is persistent.

Connection in HTTP/1.0 defaults to close, which means that TCP connections are re-established and broken on every request. Disadvantages: Establishing and disconnecting TCP connections costs too much.

In HTTP/1.1, Connection is keep-alive by default, which means that the Connection can be reused without re-establishing and disconnecting TCP connections every time. If there is no connection after the timeout, the connection is automatically disconnected. This can be done by declaring Connection close.

Advantages: TCP connections can be reused, reducing the loss of establishing connections, and avoiding the overhead of SSL. It can also be reused when refreshing the page, so that SSL connections are not established.

Conclusion: Establishing a TCP Connection does not break by default (HTTP/1.1), and only closes the Connection after the request completes if Connection: close is declared in the request header. The ultimate goal of continuous opening is to reduce the performance loss caused by establishing connections.

How many HTTP requests can a TCP connection correspond to?

If Connection is close, a TCP Connection corresponds to only one HTTP request.

If Connection is keep-alive, one TCP Connection can correspond to one or more HTTP requests.

Question 3: Can multiple HTTP requests be sent simultaneously on a TCP connection?

In HTTP/1.1, a single TCP connection can only handle one request at a time. Pipelining was specified in RFC 2616 in HTTP/1.1 to address this problem, but the browser is turned off by default.

As stated in RFC 2616, a client that supports persistent connections can send multiple requests within a connection (without waiting for a response from any request). The server receiving the request must send the response in the order the request was received.

Pipelining itself has some problems, such as proxy servers that don’t handle HTTP Pipelining properly and Head of line Blocking headers (the first request takes too long and blocks other requests). Therefore, the browser turns this feature off by default.

HTTP/2.0 provides Multiplexing, a technology that allows multiple HTTP requests to be concurrent with a single TCP (theoretically unlimited, but generally browsers have a limit on the number of TCP concurrent requests).

In HTTP/1.1, connection multiplexing and establishing multiple TCP connections at the same time are used to improve performance.

Conclusion: Pipelining technology in HTTP/1.1 supports multiple requests from a single connection, but it is disabled by default. HTTP/2.0 uses multiplexing to support concurrent HTTP requests over a TCP connection.

Q4: Does the browser limit the number of TCP connections to the same Host?

Different browsers have different restrictions. For example, Chrome allows a maximum of six TCP connections to a Host.

If the server only supports HTTP/1.1, the browser will set up multiple TCP connections to the same Host to improve efficiency. In the case of HTTPS based transport, after the SSL handshake, an attempt will be made to negotiate whether HTTP/2.0 Multiplexing can be used.

Question 5: Keep-alive usage scenarios, advantages and disadvantages

Enabling keep-alive requires high memory requirements, while disabling keep-alive requires high CPU requirements. If the memory and CPU are sufficient, enabling or disabling keep-alive has little impact on performance. Keep-alive is recommended for static pages with a large number of JS or images. If the web page is dynamic, you are advised to disable keep-alive.

Note: If the keep-alive function is required, the keepalive_timeout value in nginx must be greater than 0.

summary

Through the overall analysis above, we not only understand the relationship between TCP and HTTP, but also clarify the network level optimization made by modern browsers based on different HTTP protocols. The multiplexing mechanism of HTTP2/0 is also the basis for some high-performance frameworks, such as the implementation of gRPC.

About the blogger: Author of the technology book SpringBoot Inside Technology, loves to delve into technology and writes technical articles.

Public account: “program new vision”, the blogger’s public account, welcome to follow ~

Technical exchange: Please contact the weibo user at Zhuan2quan