Interviewer: why merge HTTP requests?



Thinking path:

Why do you want to implement batch call? – > Reduce transmission loss in the network – > how to reduce? – > How to reduce network loss by merging HTTP requests – > merging HTTP requests?

This paper will solve this problem. Let’s look at the impact of a single request carrying a large amount of information and multiple requests carrying a small amount of information on the whole time.

1. The client sends a request

1.1 HTTP 1.1

A long connection can be maintained, but between each different request, the client sends a request header to the server

Requests that cannot be executed in parallel are in a connection

Assuming that n connections need to be established without merging, merging can save the time of (n-1) * RTT, which refers to the network delay (the time taken for transmission in the transmission medium, that is, the time from the beginning of the message entering the network to its beginning to leave the network).

1.2 TCP packet loss

Slow start, congestion control window

When TCP packets arrive out of order, the merged files can allow the head of the queue to make up for packet loss in the queue. However, when resources are separated, the previous resource is not loaded, and the subsequent resources cannot be loaded. There will be a more serious head of the queue blocking problem, and the packet loss rate will seriously affect the transmission rate of multiple files in the case of keep alive.

1.3 browser thread limit

There are more than 2-6 threads, and several requests will be sent serially on each connection. Too many TCP connections will cause great pressure on the server.

1.4 DNS cache problem

The DNS cache needs to be found for each request. Multiple requests need to be found multiple times, and the cache may be emptied for no reason

2. The server processes the request

Each request needs to use a connection, establish a thread and allocate part of the CPU, which is a burden for the CPU. In particular, after the connection is established, even if the request is sent back, the connection will remain for a period of time before timeout.

At this time, maintaining the connection is a huge waste of server resources.

3. HTTP 2.0

All the features described above are based on HTTP / 1.1, or disadvantages. There are long connections but can not process requests in parallel. TCP’s slow start, congestion control and queue head blocking have brought many disadvantages to the whole performance. Therefore, we have HTTP 2.0 to make targeted improvements. Something very interesting. Look at the picture directly:

HTTP/1.1 network的请求图

HTTP/2 network的请求图

It’s so cool. Http / 2 has many more features to solve many problems of HTTP / 1.1

3.1 Fully multiplexed

Solved the problem of team leader blocking. For the same TCP connection, you can now send multiple requests and receive multiple responses! In http / 1.1, if a packet loss occurs in the last request in a connection, all subsequent requests must wait for the first request to fill the packet and continue to be executed after receiving the response. In http / 2, it can be processed directly in parallel.

3.2 Header Compression

All HTTP requests and responses have headers, but the header may contain cache information, resulting in a rapid increase in its size. However, in a connection, the request headers of most requests actually carry similar information, so http / 2 uses an index table to store the request header of the first request, and then the subsequent similar requests only need to carry the index number. Head compression reduces the head size by 30% on average, and speeds up the transmission speed in the overall network.

These two points are most relevant to this article. With these two points, the benefits of merging HTTP requests have basically disappeared under the HTTP / 2 protocol. Merging does not merge requests. It depends more on business requirements and back-end configurations.

4. Summary

It’s a trade-off. In fact, the most important thing is to see what you transmit, because merging HTTP requests actually reduces the network delay, but if you process it on the server much longer than the network delay, merging HTTP requests will not bring you a lot of performance improvement.

Moreover, the transmission of a large amount of data will certainly reduce the cache hit rate of the browser, and the utilization of the cache will be greatly reduced. However, when the amount of data carried by HTTP requests is relatively small, the performance improvement brought by merging requests will be obvious.


Recent hot article recommendations:

1.1000 + java interview questions and answers (2021 latest version)

2.Stop playing if / else on the full screen. Try the strategy mode. It’s really fragrant!!

3.what the fuck! What is the new syntax of XX ≠ null in Java?

4.Spring boot 2.5 heavy release, dark mode is too explosive!

5.Java development manual (Songshan version) is the latest release. Download it quickly!

Feel good, don’t forget to like + forward!

Recommended Today

Apache sqoop

Source: dark horse big data 1.png From the standpoint of Apache, data flow can be divided into data import and export: Import: data import. RDBMS—–>Hadoop Export: data export. Hadoop—->RDBMS 1.2 sqoop installation The prerequisite for installing sqoop is that you already have a Java and Hadoop environment. Latest stable version: 1.4.6 Download the sqoop installation […]