Performance optimizations for HTTP connections
- Parallel connection (able to establish HTTP connections with multiple servers at the same time)
- Persistent connections
- pipelined connections
- Multiplexing connections
Parallel ConnectionsAdvantage: Parallel connections can establish multiple HTTP connections at the same time with sufficient bandwidth resources to speed up page loading.
Disadvantage: A parallel connection can be a competing resource in the case of insufficient bandwidth resources. The efficiency is reduced instead. Creating multiple connections at the same time consumes a lot of memory,for the server. A large number of users generate a large number of connections that may exceed the server's processing power, so the server can typically turn off over-the-Internet connections from a specific client.
Persistent Connection (keep-alive/persistent)
Strengths:The slow connection establishment phase can be avoided by reusing spare persistent connections that have been opened to the target server . At the same time, an open connection can also avoid a slow-start congestion adaptation phase. For faster transfer of data.
today's Web applications are in the form of parallel connections + persistent connections.
pipelined connections:The ability to agree to an optional use request pipeline on a persistent connection. Functions equivalent to pipelining. The ability to put multiple requests into the queue before the corresponding arrival.
several limitations of a pipelined connection:
- < Span style= "line-height:1.5" > If the connection is not persistent, you should not use the pipe
- The HTTP response must be echoed in the same order as the request. There is no serial number label in the HTTP message. So suppose the received response is out of sequence. Then there is no way to match it to the request.
- httpclient must be ready to close the connection at random, and be prepared to re-send all outstanding pipelined requests.
- httpclient should not be piped to send a request (POST request) that would have a side effect. For example, post is to buy a book, run it again and buy a book, obviously can't run.
The shutdown of the HTTP connectionHTTP communication is based on a TCP connection, so the shutdown of the HTTP connection is actually the shutdown of the TCP connection. Connection closure is divided intocompletely closed and half closed,Close the input and output channels are closed in conjunction with the same time. The shutdown will only shut down the input or output channel separately.
TCP shutdown and reset errorswhen one end of the output channel is closed. The peer entity at one end receives the FIN after the data is read from its buffer ( stating that the flow is over), and at the same time the peer sends a file terminator to the utility so that it knows that you are closing the connection. At this point the application of the peer entity sends a fin, and finally the TCP connection is completely disconnected. shutting down the input channel is more critical, unless you know that there's one end that's not going to send any more data.. Assuming that one end sends data to the input channel you have closed, the operating system will send back a TCP "connection to end multiplicity" message to a machine on the other end.
(Resetting the message is handled as a very serious error, directly deleting all cached data that has not been read by the peer.) Cause the application to produce an error)
The active closed party enters after sending an ACK for the other's fintime_waitstate. Time_wait is also known as a 2MSL wait state. Each detailed TCP implementation must select a message segment maximum generation time MSL. It is the maximum time within the network regardless of what message segment is discarded . We know this time is limited because TCP packets are transmitted over the network with IP packets, and the IP packets have TTL fields that limit their time to live .
Why do you want to set it to 2MSL? Because this enables the final ACK to be sent again to avoid an ACK loss.
Another result of such a 2MSL wait is the TCP connection during the 2MSL wait. The sockets that define this connection (ClientIP and port, server IP and port) cannot be used, and this connection can only be used again after the 2MSL wait has ended.
when the connection is 2MSL waiting, no matter what the late message segment will be discarded. Due to the 2MSL wait, the plug-inThe connection defined by the port pair (socket pair) cannot be re-used during this time period.
fin-wait_2 Status:The fin-wait_2 state indicates that the local side has sent fin at the same time to receive an ACK to the end, waiting to send fin to the end. But assuming that the fin has not been sent to the end, will the state continue to persist? No, assuming that the application running the active shutdown wants to run full shutdown, then a timer is set, assuming that the time exceeds 10 minutes and 75 seconds, TCP goes into the closed state.
Normal shutdown:In summary, applications that implement graceful shutdown should first shut down their output channels, and then wait for the peer entity that is connected to the other end to close its output channel when both sides tell the other that they are no longer sending any data. The connection will be completely closed. Without the necessity of resetting.
However, you cannot ensure that the peer entity implements semi-shutdown. So applications that want to shut down the connection properly should first half-shut the output channel,It then periodically checks the state of its input channel (to find the data.) Or the end of the stream), such as the end of the data mark, to end the connection.
assume that the input channel is not closed within a certain time interval. Applications can force the connection to be closed to conserve resources (memory and sockets).
Long Connectionslong-connected transmission data completion Identificationafter using the long connection, how does the client and the server know this transmission is over? are divided into two types:Infer if the data transfer has reached content-length only sizethe dynamically generated file does not have a content-length, it is a chunked transfer (chunked). The data for the chunked transfer will eventually have an empty chunked block. Indicates the end of this transmission. Many other referencesHTTP keep-alive Modeexpiration time for long connectionsThe long connection of the client cannot be held indefinitely, by the following several ways of closing the long connection:
- The server tells the client time-out that the timeout time is specified in the response header keep-alive or Max maximum transaction count
- Client or server disconnect (shut down or offline), initiate four handshake
References :1. "http authoritative guide"2. http://www.cnblogs.com/cswuyg/p/3653263.html
HTTP connection Optimization