HTTP persistent connection

Source: Internet
Author: User
Document directory
  • 1. Name
  • 2. Principles
  • 3. Implementation
  • 4. Advantages
  • 5. Disadvantages
  • 6. Browser

Another small easyproxy project was created last week to implement code reverse proxy. Specifically, requests on the TCP layer (as long as they follow the protocol established on the TCP layer) are parsed, and then distribute them to specific services.

One problem encountered in this process is the persistent connection problem in HTTP. I checked the specific HTTP protocol and found that there was still a blind spot in this knowledge.

This article can be regarded as your own study notes. I am using many of the materials that I have seen and thought are good. I hope this article will be helpful to you.

Basic knowledge 1. Name

Wikipedia:

"Http persistent connection, also called HTTP keep-alive, or HTTP connection reuse, is the idea of using a single TCP connection to send and receive multiple HTTP requests/responses, as opposed to opening a new connection for every single request/response pair."

This is because I think the term "persistent connection" is not very accurate, and "persistent connection" is used, while direct translation words like "long connection" are not used anymore. But after all, this name has been used for so long, and we will continue to use it.

2. Principles

In the HTTP protocol, the same TCP connection is used to send and receive multiple HTTP requests/responses, rather than opening a new connection method for each new request/response.

It is an image of the difference between a common connection and a persistent connection on the Internet.

We can clearly see that the difference between the two is that persistent connections will be reused.

3. Implementation

In HTTP 1.0

There is no official keepalive operation. Generally, an index is added to an existing protocol. If the browser supports keep-alive, it will add:

Connection: Keep-Alive

Then, when the server receives a request and responds, It also adds a header in the response:

Connection: Keep-Alive

In this way, the connection will not be interrupted, but the connection will be maintained. When the client sends another request, it uses the same connection. This continues until the client or server determines that the session has ended, and one Party interrupts the connection.

For example, I use chrome to access the blog homepage.

Request Header:

 

Response Header:

In HTTP 1.1 

All connections are persistent connections by default, unless otherwise specified.

Therefore, high versions of browsers support persistent connections.

 

4. Advantages
  • Less CPU and memory usage (because the number of connections opened at the same time is reduced)
  • HTTP pipelining that allows requests and responses
  • Reduced network congestion (reduced TCP connections)
  • Reduces the latency of subsequent requests (no handshake is required)
  • No need to disable TCP connection when an error is reported

In fact, all of its advantages lies in reusing the previously established TCP connection to reduce the consumption of re-establishing a TCP connection.

5. Disadvantages
  • Idle connections can be disconnected after a period of time, which may affect the overall performance (for example, those Web Services with a large number of visits)

 

6. Browser

Browsers of later versions support persistent connections by default, and they maintain persistent connections by "timeout management ".

If no request or response occurs within a period of time, the connection is automatically disconnected.

 

 

References:

Wikipedia: English

Persistent HTTP connections in RFC 2616 "Hypertext Transfer Protocol -- HTTP/1.1

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.