Long connections and short connections for HTTP

Source: Internet
Author: User

Original: https://www.cnblogs.com/gotodsp/p/6366163.html

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:

    • Short connections are used by default in http/1.0. That is, the client and the server once each HTTP operation, the connection is established, the end of the task to interrupt the connection
    • 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.

Long connections:

    • 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

3.2. TCP Short Connection

Simulate the situation of a TCP short connection:

    • The client initiates a connection request to the server, the server receives the request, and then 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, and 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

From the above can be seen:

Advantages and disadvantages of long connections:

    • Long connections eliminate the more TCP setup and shutdown operations, reducing waste and saving 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.

Advantages and disadvantages of short connections:

    • For the server management is simple, the existing connection is a useful connection, do not need additional control means.
    • 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.

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 for frequent, point-to-point communication, and the number of connections cannot be too many.

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 a short connection without frequent operation.

Long connections and short connections for HTTP

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.