HTTP also has long and short connections? HTTP long connection vs. short connection

HTTP also has long and short connections? HTTP long connection vs. short connection

You must have heard about the so-called long connections and short connections in HTTP.

However, what is a long connection? What is a short connection? I think many people are confused and can't fully tell the difference between them. Today I will take you to fully understand them and you will never forget them again.

[[262514]]

What is a long connection?

The difference between HTTP long and short connections lies in whether the TCP long connection or short connection is used.

In HTTP 1.0, short connections are used by default, but since HTTP 1.1, the default connections have all become long connections. The difference between long and short connections? Essentially, it is determined by TCP connections. Why do we say that? Because TCP is a two-way channel, it can remain open for a period of time, so there is a difference between long connections and short connections. For example: after the data transmission is completed, the TCP connection is maintained uninterrupted, and when waiting for the same domain name to request again, the TCP connection channel continues to be used for data transmission. This is a long connection.

For example, if you need to email something to your friend, the HTTP protocol refers to the express delivery form you need to fill out. The action of filling out the express delivery form when sending the item is equivalent to making an HTTP request. And your express delivery needs to be delivered by means of transportation, which can be trucks, lorries, high-speed trains, airplanes, etc. The TCP protocol refers to the means of transportation for delivering express delivery. Because transportation is needed, there must be roads, such as roads or railways on the ground and air routes in the sky. Then this transportation road is the TCP connection, because this road is bidirectional and can be transported both in and out. Therefore, the TCP connection is also called a bidirectional data channel.

If there are vehicles transporting goods on this road for a long time, it is called a long connection. If there are no vehicles passing by after a period of transportation, it is called a short connection.

Therefore, we can see that the so-called HTTP connection refers to the TCP connection. A TCP connection that can be maintained for a period of time is a long connection, and a short connection that is automatically disconnected after a request is initiated is a short connection, so there is a long connection and a short connection.

So the question is, what kind of connection can be called a long connection?

Long connection conditions:

  • ***, you need to set the HTTP header, Connection to keep-alive, but there is a problem here. Is it enough to set the Connection to keep-alive to make it a long connection? This question is obvious. Of course not. You need to set it on both the server and the client.
  • Second, are the HTTP requests we use in our daily lives long connections? The answer is obvious, of course they are. Because the current HTTP uses the 1.1 protocol, if you observe carefully, you will find that its Connection is set to keep-alive.

Now you are curious about keep-alive. What is the Connection setting keep-alive? Next, I will take you to unveil its mystery.

What is Keep-alive?

As we know, HTTP protocol uses the "request-response" mode. When the Connection is not in keep-alive mode, the server and client need to re-establish a connection for each request-response task. After the task is completed, the connection is disconnected. When the Connection is in keep-alive mode, the server and client will always maintain a connection. When a new request task occurs, there is no need to re-establish the connection. This saves time and does not consume resources.

Non-keep-alive:

As can be seen from the above figure:

Steps for short connection:

Steps for long connection:

Next, let's talk about the scenarios in which long connections and short connections are used.

  • Persistent connection usage scenarios: Persistent connections are mostly used for frequent operations, multiple requests, and one-to-one, point-to-point communication. For example, database connections use persistent connections. If short connections are used for frequent responses, socket errors will be reported and resources will be wasted.
  • Usage scenarios of short connections: short connections do not consume so many resources because they do not need to occupy TCP connections for a long time. Therefore, http services in Web sites generally use short connections. Because long connections will occupy certain resources, and websites such as Taobao and JD.com are frequently visited by users, with hundreds of millions of visits during the Double Eleven shopping festival. If long connections are used, each user will occupy a TCP connection channel, and the pressure on the server can be imagined. Therefore, it is better to use short connections in this case.

Because we mentioned above that HTTP connection is actually TCP connection, so here we have to talk in detail, what are the characteristics of TCP connection?

TCP Connection

When the network responds to a request, a connection must be established between the client and the server. When the network request is completed, both parties can release the connection when they no longer need it. The TCP connection is used here. The connection in the TCP protocol relies on the "three-way handshake" and the release relies on the "four-way handshake".

Therefore, each connection consumes resources and time.

1. TCP's "three-way handshake":

Schematic diagram of TCP's three-way handshake

Detailed explanation:

  • First handshake: The client sends a packet with the SYN flag set to 1 and the initial sequence number X to the server, which is encapsulated in the sequence number field of the packet header.
  • Second handshake: The server sends back ACK (confirmation packet), which sets both the SYN and ACK flags to 1, and modifies the sequence number to X+1.
  • The third handshake: The client sends an acknowledgment packet (ACK) to the server again, with the SYN flag bit set to 0 and the ACK flag bit set to 1. At the same time, the ACK packet sequence number field sent by the server is added by 1. It is placed in the packet and sent to the server, that is, ACK=Y+1

2. TCP’s “four breakups”:

Because TCP disconnection requires sending four packets, it is called four waves. Either the client or the server can actively disconnect. In socket programming, either party can perform a close() operation to generate a breakup operation.

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

How do short connections and long connections work? What are their advantages?

  • Short connection: The client sends a request to the server, the server receives the request, and the two parties establish a connection. The client sends a message to the server, and the server responds to the client. Then a connection is completed. At this point, both parties can initiate a close operation.
  • Advantages of short connections: simple management, all initiated connections are valid connections, and no additional control measures are required.
  • Long connection: The client sends a request to the server, the server responds to the client's connection, and the two parties establish a connection. After completing a successful response request, their connection will not be actively closed, and subsequent read and write response requests will continue to use this connection channel.

<<:  IoT Observation: Seven benefits of LoRaWAN technology application in one article

>>:  Big data changes both ends of the web hosting market

Recommend

In addition to the ping command, these network commands are also very useful

What we are going to talk about today is other co...

The momentum of large-scale commercial use of NB-IoT is unstoppable

The Internet of Things is the only way to the int...

What kind of private network solution does 5G need?

MWC2021 Shanghai has fully demonstrated the achie...

Bricklayer Los Angeles CN2 GIA (DC6)/Japan Softbank special price $46.6/year

[Restocked again on 10.15] Bandwagonhost has launc...

Master port isolation technology to make the network more secure

Currently, Ethernet technology is widely used in ...

Let’s talk about what CDN is. Do you know these characteristics?

Preface When we browse the web, watch videos, and...

What exactly is “cloud-network integration”?

Hello everyone, today I would like to talk to you...