HTTP long connections and short connections

Source: Internet
Author: User

0. TCP/IP

TCP/IP is a protocol group that can be divided into three levels: Network layer, Transport layer and application layer.
At the network layer are IP protocols, ICMP protocols, ARP protocols, RARP protocols, and BOOTP protocols.
There are TCP protocols and UDP protocols in the transport layer.
At the application level: TCP includes FTP, HTTP, TELNET, SMTP, and other protocols
UDP includes DNS, TFTP, and other protocols

1. The relationship between the HTTP protocol and the TCP/IP protocol

Long connections and short connections to HTTP are essentially TCP long connections and short connections. HTTP belongs to the Application layer protocol, which uses the TCP protocol at the Transport Layer and IP protocol at the network layer. The IP protocol mainly solves the network routing and addressing problems, the TCP protocol mainly solves how to reliably pass the packet over the IP layer, so that all the packets sent by the originator are received on the other end of the network, and the order is consistent with the order of issuing. TCP has a reliable, connection-oriented feature.

2. How to understand that the HTTP protocol is stateless

The HTTP protocol is stateless, meaning that the protocol has no memory capability for transactions, and the server does not know what state the client is. That is, there is no connection between opening a Web page on a server and the pages you have previously opened on this server. HTTP is a stateless, connection-oriented protocol, and stateless does not mean that HTTP cannot maintain TCP connections, nor does it use the UDP protocol (no connection) on behalf of HTTP.

3. What is long connection, short connection?

  in http/1.0, a short connection is used by default . That is, the browser and the server each HTTP operation, the connection is established, but the end of the task to disconnect. If a client browser accesses an HTML or other type of Web page that contains other Web resources, such as JavaScript files, image files, CSS files, and so on, when the browser encounters such a Web resource, an HTTP session is established.

However , from http/1.1, a long connection is used by default to maintain the connection characteristics. Using a long-connected HTTP protocol, this line of code is added to the response header:

Connection:keep-alive

In the case of a long connection, when a Web page opens, the TCP connection between the client and the server for transmitting HTTP data does not close, and if the client accesses the Web page on the server again, it will continue to use this established connection. Keep-alive does not permanently keep the connection, it has a hold time that can be set in different server software (such as Apache). Long connections are supported for both the client and the server.

The long and short connections of the HTTP protocol are essentially long connections and short connections to the TCP protocol.

3.1 TCP connections

When the TCP protocol is used in network communication, before the actual read and write operation, the server and the client must establish a connection, when the read and write operation is completed, the two sides no longer need the connection when they can release the connection, the connection is established to need three handshake, and release will require 4 handshake, So the establishment of each connection requires resource consumption and time consumption.

Classic three-time handshake:

Classic four-time handshake close diagram:

3.2 TCP Short Connection

We simulate the case of a TCP short connection, the client initiates a connection request to the server, the server receives the request, and the two sides establish the connection. The client sends a message to the server, the server responds to the client, and then a read-write is completed, and either of the parties can initiate a close operation, but usually the client initiates the close operation. Why, the general server will not reply to the client immediately after closing the connection, of course, do not rule out a special situation. From the above description, short connections generally only pass a read and write operation between Client/server

The advantage of short connections is that they are simpler to manage, that existing connections are useful connections, and that no additional control is required

3.3 TCP Long Connections

Next we simulate the case of a long connection, the client initiates a connection to the server, the server accepts the client connection, and the two sides establish the connection. After the client and server have completed a read and write, the connection between them is not actively closed, and subsequent read and write operations continue to use the connection.

First of all, the TCP/IP feature described in the TCP/IP details, the KeepAlive function is mainly provided for the server application, the server application wants to know whether the customer host crashes, which can be used on behalf of the customer resources. If the customer has disappeared, leaving a semi-open connection on the server while the server is waiting for data from the client, the server should be waiting for the client's data, and the KeepAlive function is trying to detect this semi-open connection on the server side.

If a given connection does not have any action within two hours, the server sends a probe segment to the client and the client host must be in one of the following 4 states:

    1. The client host is still functioning and can be reached from the server. The client's TCP response is normal, and the server knows the other side is normal, the server after two hours will be keepalive timer reset.
    2. The client host has crashed and is shutting down or restarting. In either case, the client's TCP is not responding. The server will not receive a response to the probe and time out after 75 seconds. The server sends a total of 10 such probes at each interval of 75 seconds. If the server does not receive a response, it considers the client host to be closed and terminates the connection.
    3. The client host crashed and has restarted. The server receives a response to its keepalive probe, which is a reset that causes the server to terminate the connection.
    4. The client runs normally, but the server is unreachable, which is similar to 2, where TCP can find no response to the probe.

3.4 Long connection Short connection operation Process

The operation steps for a short connection are:
Establish connection--data transfer--close connection ... Establish connection--data transfer--close connection
The procedure for long connections is:
Establish connection--data transfer ... (Keep connected) ... Data transfer--close connection

4. Advantages and disadvantages of long connections and short connections

As can be seen from the above, long connections can eliminate more TCP setup and shutdown operations, reduce waste, save time . For customers who frequently request resources, they are more suitable for long connections. However, there is a problem , the survival function of the detection cycle is too long , there is it is only to detect the survival of TCP connections, is a relatively gentle practice, encounter malicious connection, the keepalive function is not enough. In the case of long-connected applications, the client side generally does not actively shut down their connection between theclient and the server if the connection is not closed, there will be a problem, as the client connection more and more, the server sooner or later can not carry the time , At this time, the server side needs to take some policies, such as the closure of some long-term no read and write events occurred connection, so as to avoid some malicious connection caused server-side service damage, if the condition is allowed to the client machine for granularity, limit the maximum number of connections per client, This can completely avoid an egg-hurting client from compromising the backend service.

Short connections are simpler for the server to manage, and the connections that exist are useful connections and do not require additional control. However, if customer requests are frequent , time and bandwidth will be wasted on TCP setup and shutdown operations .

The generation of long connections and short connections is based on client and server shutdown policies, specific application scenarios with specific strategies, no perfect choice, only the right choice.

5. When to use long connection, short connection?
long connections are used to operate frequently, point-to-point communications, and the number of connections can not be too many cases. Each TCP connection requires a three-step handshake, which takes time, if each operation is first connected, then operation then the processing speed will be reduced a lot, so after each operation is continuously open, the processing of the direct sending of packets is OK, do not establish a TCP connection. For example, the connection of the database with a long connection, if the frequent communication with short connections will cause socket errors, and frequent socket creation is also a waste of resources.

HTTP services like Web sites generally use short links , because long connections consume a certain amount of resources for the server, and tens of thousands or even billions of clients, such as Web sites, use short connections to save resources if they are connected with long connections, and thousands of users at the same time, If each user occupies a connection, then you can imagine it. So the concurrency is large, but each user needs to be short-connected in case of frequent operation.

6. Send and Receive mode

    • Asynchronous

Message sending and receiving are separate, independent and mutually exclusive. This approach is divided into two different situations:
(1) Asynchronous duplex: Receive and send in the same program, by two different sub-processes are responsible for sending and receiving respectively
(2) Asynchronous Simplex: Receive and send is done with two different programs.

    • Synchronous

The message is sent and received synchronously, which waits to receive the return message after the message is sent. Synchronous mode generally need to consider the time-out problem, that is, the message can not wait indefinitely after sending out, need to set the timeout period, more than the time the sender no longer wait for the read return message, the direct notification timeout return.

In a long connection, it is generally not possible to determine when the read-write ends, so it is necessary to add a length message header. The reading function reads the length of the message header, and then reads the corresponding length of the message according to the length.

7. What is a socket?

A socket is an intermediate software abstraction layer that the application layer communicates with the TCP/IP protocol family, which is a set of interfaces. In design mode, the socket is actually a façade mode, it is the complex TCP/IP protocol family hidden behind the socket interface, for the user, a set of simple interface is all, let the socket to organize data to meet the specified protocol.

Socket Communication Example

Host A's application communicates with host B's application and must establish a connection through the socket, and the socket connection must be built with the underlying TCP/IP protocol to establish a TCP connection. Establishing a TCP connection requires the underlying IP protocol to address the hosts in the network. We know that the IP protocol used by the network layer can help us to locate the target host based on the IP address, but there may be multiple applications running on one host, and how to communicate with the specified application is specified by the TCP or UPD address, or the port number. This makes it possible for a Socket instance to uniquely represent a communication link for an application on a host.

Establish a communication link
When the client wants to communicate with the server, the client first creates a socket instance, the operating system assigns the socket instance a local port number that is not used, and creates a socket data structure that contains local and remote addresses and port numbers. This data structure will remain in the system until the connection is closed. Before the constructor for the socket instance is returned correctly, the three-time handshake protocol for TCP will be made, and after the TCP handshake protocol completes, the socket instance object will be created, or a IOException error will be thrown.
The corresponding server will create a ServerSocket instance, ServerSocket creation is simpler as long as the specified port number is not occupied, the general instance creation will succeed, and the operating system will create an underlying data structure for the ServerSocket instance. This data structure contains the port number of the specified listener and the wildcard character that contains the listening address, usually "*" which listens to all addresses. Then when the accept () method is called, it goes into a blocking state and waits for the client's request. When a new request arrives, a new socket data structure is created for the connection, which contains the address and port information for the requested source address and port. This newly created data structure will be associated to an incomplete list of connection data structures for the ServerSocket instance, and note that the server's corresponding socket instance is not created, and the socket instance of the server is not returned until the three handshake with the client is completed. and move the data structure of the Socket instance to the completed list in the never-complete list. Therefore, each data structure in the list associated with ServerSocket represents a TCP connection that is established with a client.

remark:
Maximum number of TCP connections for a single machine under Windows
Adjusting the system parameters to adjust the maximum number of TCP connections for a single machine, the number of TCP connections under Windows is determined by multiple parameters:
The following are all modified by the registry [HKEY_LOCAL_MACHINE \SYSTEM \CurrentControlSet \services \TCPIP \parameters]

1. Maximum number of TCP connectionsTcpNumConnections
2.TCP shutdown delay TimeTcpTimedWaitDelay (30-240) s
3. Maximum number of dynamic portsMaxUserPort (default =, Max = 65534) TCP client and server connections, the client must be assigned a dynamic port, by default, the dynamic port is allocated a range of 1024-5000, which means by default, Clients can initiate up to 3977 Socket connections at the same time
4.Max TCB QuantityMaxFreeTcbs
The system assigns a TCP control block to each TCP connection (TCP-control block or TCB), which is used to cache some parameters of a TCP connection, and each TCB needs to allocate 0.5 KB of Pagepool and 0.5KB Non-pagepool, saying that Each TCP connection consumes 1KB of system memory.
For non-Server versions, the default value for MaxFreeTcbs is 64M or more of the server version of physical memory, which defaults to 2000. In other words, by default, the Server version can establish and maintain up to 2000 TCP connections at a time.
5. Maximum TCB Hash table numberThe MaxHashTableSize TCB is managed by a hash table.
This value indicates the amount of allocated pagepool memory, that is, if MaxFreeTcbs = 1000, the amount of memory Pagepool is 500KB then the maxhashtablesize should be greater than 500. The larger the number, the higher the redundancy of the hash table, and the less time it takes to allocate and find the TCP connection. This value must be a power of 2 and a maximum of 65536.

Typical configurations for IBM WebSphere Voice server under Windows Server 2003
MaxUserPort = 65534 (Decimal)
MaxHashTableSize = 65536 (Decimal)
MaxFreeTcbs = 16000 (Decimal)
Here we can see that the maxhashtablesize is configured to be 4 times times larger than the MAXFREETCBS, which can greatly increase the speed of TCP setup.

HTTP long connections and short connections

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.