What exactly is an HTTP long connection and a short connection?

Source: Internet
Author: User

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 problem, the TCP protocol mainly solves how to reliably pass the packet over the IP layer, so that the receiving end of the network receives all the packets sent by the sender, and the order is consistent with the order of sending. The TCP protocol is reliable and connection-oriented.

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. In other words, there is no connection between opening a Web page on a server and the last time you opened the Web page 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?

Short connections are used by default in http/1.0. That is, each time the client and the server make an HTTP operation, a connection is established and the connection is interrupted at the end of the task. When 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), the browser will reestablish an HTTP session each time it encounters such a web resource.

From http/1.1 onwards, a long connection is used by default to maintain the connection characteristics. Using the 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 is opened, the TCP connection between the client and the server for transmitting HTTP data will not be closed, and the client will continue to use this established connection when it accesses the server again. Keep-alive does not permanently keep the connection, it has a hold time that can be set in different server software (such as Apache). Implementing long connections requires both the client and the server to support long connections.

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 for network communication, a connection must be established between the client and the server before the actual read and write operation, and when the read and write operations are completed, the connection can be freed when the two parties no longer need the connection. The establishment of the connection relies on the "three-time handshake", while the release requires "four handshake", so each connection is built with resource consumption and time consumption.

The classic three-time handshake establishes the connection:

Classic four-time handshake close connection:

3.2. TCP Short Connection

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 one request is completed. The close operation can be initiated by both parties at this time, but usually the client initiates the close operation first. As noted above, short connections generally only pass one request operation between Client/server.

The advantage of short connections is that they are simpler to manage, that existing connections are useful connections, and that there is no need for additional control.

3.3. TCP Long connections

Let's simulate the case of a long connection: the client initiates a connection to the server, the server accepts the client connection, the connection is made, the client and server complete a request, the connection between them is not actively closed, and subsequent read and write operations continue to use the connection.

The KeepAlive function of TCP is mainly provided for server applications. If the client has disappeared and the connection is not fractured, it leaves a semi-open connection on the server and the server waits for data from the client, where the server waits for the client's data forever. The KeepAlive function is an attempt to detect this semi-open connection at the service end.

If a given connection does not have any action within two hours, the server sends a probe segment to the client and detects 4 client states based on the client host response:

    • The client host is still functioning and the server is up. At this point the client's TCP response is normal and the server resets the keepalive timer.
    • The client host has crashed and is shutting down or restarting. In these cases, the client cannot respond to TCP. The server will not receive a response from the client to the probe. The server sends a total of 10 such probes at each interval of 75 seconds. If the server does not receive any response, it will assume that the client has shut down and terminated the connection.
    • The client crashes and has restarted. The server receives a response to its keepalive probe, which is a reset that causes the server to terminate the connection.
    • The client is running normally, but the server is unreachable. This situation is similar to the second state.
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. Long connections are appropriate for clients that frequently request resources. In the case of long-connected scenarios, the client side generally does not actively shut down the connection, when the connection between the client and the server is not closed, and as the client connects more and more, the server keeps too many connections. At this point the server side needs to take some policies, such as shutting down some long time no request occurred connection, so as to avoid some malicious connection caused server side service damage, if the condition allows you can limit the maximum number of connections per client, this can completely avoid malicious clients to drag down the overall backend services.

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, more time and bandwidth will be wasted on TCP setup and shutdown operations.

The generation of long connections and short connections is due to the shutdown policy taken by client and server. Different scenarios are suitable for different strategies.

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.

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
when to use long connections, short connections?

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.

HTTP and socket long connections and short connection differences

Http://www.jianshu.com/p/b68d2b26f5f4

Long connections and short connections for HTTP

http://blog.jobbole.com/104108/

HTTP Persistent Connection

Https://zh.wikipedia.org/wiki/HTTP%E6%8C%81%E4%B9%85%E8%BF%9E%E6%8E%A5

Long connections

Http://baike.baidu.com/view/2831907.htm

What exactly is an HTTP long connection and a short connection?

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.