Analysis on the principles of http persistent connections and short connections

Source: Internet
Author: User
HTTP persistent connections and short connections are essentially TCP persistent connections and short connections. HTTP belongs to the application layer protocol and uses TCP protocol at the transport layer. 1. relationship between HTTP protocol and TCP/IP protocol

HTTP persistent connections and short connections are essentially TCP persistent connections and short connections. HTTP is an application layer protocol. TCP is used at the transport layer and IP is used at the network layer. The IP protocol mainly solves the problem of network routing and addressing. the TCP protocol mainly solves how to transmit packets reliably on the IP layer, so that the other end of the network receives all packets from the initiator, and the order is the same as the sending order. TCP is reliable and connection-oriented.

2. how to understand that HTTP is stateless

The HTTP protocol is stateless, which means the protocol has no memory for transaction processing and the server does not know what the client is in. That is to say, there is no connection between opening a webpage on a server and the webpage on the server you opened before. HTTP is a stateless connection-oriented protocol. stateless does not mean that HTTP cannot maintain a TCP connection, nor does it mean that HTTP uses a UDP protocol (no connection ).

3. What are persistent connections and short connections?

In HTTP/1.0, transient connections are used by default. That is to say, the browser and the server establish a connection every time they perform an HTTP operation, but the connection is interrupted when the task ends. If an HTML or other Web page accessed by the client browser contains other Web resources, such as JavaScript files, image files, and CSS files, each time the browser encounters such a Web resource, an HTTP session is created.

However, from HTTP/1.1, persistent connections are used by default to maintain the connection feature. With the persistent connection HTTP protocol, this line of code is added to the response header:

Connection: keep-alive

When a persistent connection is used, after a webpage is opened, the TCP connection used to transmit HTTP data between the client and the server is not closed. if the client accesses the webpage on the server again, will continue to use this established connection. Keep-Alive does not Keep the connection permanently. it has a retention time, which can be set in different server software (such as Apache. To achieve persistent connection, both the client and the server support persistent connections.

HTTP persistent connections and short connections are essentially TCP persistent connections and short connections.

3.1 TCP connection

When the TCP protocol is used for network communication, a connection must be established between the server and the client before the real read/write operations. after the read/write operations are complete, when the two sides no longer need the connection, they can release the connection. the connection establishment requires three handshakes, and the release requires four handshakes, therefore, the establishment of each connection requires resource consumption and time consumption.

Classic three-way handshake:

4. Advantages and disadvantages of persistent connections and short connections

As can be seen from the above, long connections can save a lot of TCP establishment and closure operations, reduce waste and save time. For customers who frequently request resources, persistent connections are more suitable. However, there is a problem here. the probe cycle of the survival function is too long, and it is only used to detect the survival of TCP connections. it is a relatively simple practice in the case of malicious connections, the retention function is not enough. In persistent connection scenarios, the client generally does not take the initiative to close the connection between them. if the connection between the Client and the server is never closed, there will be a problem, as the number of client connections increases, the server may not be able to cope with it sooner or later. at this time, the server needs to adopt some policies, such as closing connections that have not been read or written for a long time, in this way, some malicious connections can prevent server-side service losses. if conditions permit, you can use the client machine as the granularity to limit the maximum number of persistent connections of each client, this completely prevents a client from getting tired of backend services.

Short connections are relatively simple for servers. existing connections are useful connections without additional control measures. However, if the customer requests frequently, it will waste time and bandwidth on the establishment and closure of TCP.

Persistent connections and short connections are generated by the closing policies adopted by the client and server. specific policies are used in specific application scenarios. there is no perfect choice but a proper choice.

5. When can I use persistent connections or short connections?

Persistent connections are mostly used for frequent operations and point-to-point communication, and the number of connections cannot be too large ,. Each TCP connection requires three-step handshake, which takes time. if each operation is connected first and then operated, the processing speed will be much lower, so each operation will continue to run after completion, it is OK to directly send the data packet during the next processing, so no TCP connection is required. For example, if you use persistent connections for database connections, frequent communication may cause socket errors, and frequent socket creation is a waste of resources.

Http services for websites generally use short links, because long connections consume resources for the server, however, the frequent connection of thousands or even hundreds of millions of clients to WEB sites with short connections saves some resources. if persistent connections are used and there are thousands of users at the same time, if every user occupies a connection, you can imagine it. Therefore, a large amount of concurrency is required, but each user needs to be connected in a short connection without frequent operations.

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.