Long connection, short connection, heartbeat mechanism

Long connection, short connection, heartbeat mechanism
The relationship between HTTP protocol and TCP/IP protocol
HTTP long connections and short connections are essentially TCP long connections and short connections. HTTP is an application layer protocol, using TCP protocol at the transport layer and IP protocol at the network layer. The IP protocol mainly solves network routing and addressing problems, and the TCP protocol mainly solves how to reliably deliver data packets above the IP layer, so that the receiving end on the network receives all packets sent by the sending end, and the order is consistent with the sending order. The TCP protocol is reliable and connection-oriented.

How to understand that the HTTP protocol is stateless
The HTTP protocol is stateless, which means that the protocol has no memory for transaction processing, and the server does not know the state of the client. In other words, there is no connection between opening a web page on a server and the last time a web page was opened on this server. HTTP is a stateless connection-oriented protocol. Statelessness does not mean that HTTP cannot maintain a TCP connection, nor does it mean that HTTP uses the UDP protocol (no connection).

What are long connections and short connections?
Short connections are used by default in HTTP/1.0. In other words, every time the client and server perform an HTTP operation, a connection is established, and the connection is terminated when the task is completed. 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, etc.), the browser will re-establish it every time it encounters such a Web resource. An HTTP session.
Starting from HTTP/1.1, long connections are used by default to maintain connection characteristics. Using the HTTP protocol with long connections, this line of code will be added to the response header:

Connection:keep-alive

When a long connection is used, when a web page is opened, the TCP connection used to transmit HTTP data between the client and the server will not be closed. When the client accesses the server again, it will continue to use this established connection. . Keep-Alive does not maintain the connection permanently. It has a retention time that can be set in different server software (such as Apache). To implement long connections, both the client and the server need to support long connections.

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

TCP connection
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 operations. After the read and write operations are completed, the connection can be released when both parties no longer need the connection. The establishment of a connection relies on a “three-way handshake”, while the release requires a “four-way handshake”, so the establishment of each connection requires resource consumption and time consumption.

Schematic diagram of the classic three-way handshake to establish a connection:

Classic four-way handshake closing diagram:

Long connection short connection operation process
The steps for short connection are:
Establish a connection – data transfer – close the connection…Establish a connection – data transfer – close the connection
The steps for long connections are:
Establish connection – data transfer…(keep connection)…data transfer – close connection

short connection
concept

The client and server establish a connection through a three-way handshake. The client sends a request message and the server returns a response. The connection is completed once.

At this time, both parties can initiate a close operation at any time, but usually the client initiates the close operation first. As can be seen from the above, short connections generally only transfer one request operation between client/server.

Advantages and Disadvantages of Short Connections
It is relatively simple to manage. All existing connections are useful connections and no additional control means are required.

scenes to be used

Usually when the browser accesses the server, it is a short connection.

For the server, long connections will consume server resources, and users use browsers to access the server relatively infrequently.

If there are hundreds of thousands or millions of connections, the pressure on the server will be very high and it may even collapse.

Therefore, for those with large concurrency and low request frequency, it is recommended to use short connections.

Long connection
What is a long connection
The client initiates a connection to the server, the server accepts the client connection, and the two parties establish a connection.

After the client and server complete a read and write, the connection between them will not be actively closed, and subsequent read and write operations will continue to use this connection.

Long connection life cycle
Under normal circumstances, after a long TCP connection is established, the connection will always exist as long as the two parties do not make a close request and no abnormal conditions occur.

The operating system will not automatically turn it off, and it can still be used even after changes in the physical network topology.

Therefore, it is possible for a connection to remain open for days, months, years, or longer, as long as no abnormality occurs or the user (application layer) actively closes it.

The client and ticket can always use this connection for data communication.

Advantages of long connections
Long connections can save more TCP establishment and closing operations and reduce the impact of network congestion.

When an error occurs, you can prompt without closing the connection.

Reduce CPU and memory usage because there is no need to frequently establish and close connections.

Disadvantages of long connections
When there are too many connections, the performance and number of concurrencies on the server are affected.

scenes to be used

The database connection uses TCP long connection.

RPC, remote service call. On the server, one service process frequently calls another service process. Long connections can be used to reduce the time spent on the connection.

Summarize
1. The use of long connections and short connections needs to be judged based on the application scenario.

2. Long connections are not omnipotent and require maintenance.

Implementation of long connection
heartbeat mechanism
Most application layer protocols have a HeartBeat mechanism. Usually the client sends a data packet to the server every short period of time to notify the server that it is still online.

and transmit some data that may be necessary. The typical protocol that uses heartbeat packets is IM, such as QQ/MSN/Fetion and other protocols.

In the TCP mechanism, there is a heartbeat packet mechanism, which is the TCP option: SO_KEEPALIVE.

The system defaults to the set heartbeat frequency of 2 hours. But it cannot detect machine power outages, network cable unplugging, or firewall disconnections.

Moreover, the logic layer may not be so easy to deal with disconnection. Generally speaking, it’s okay if it’s just used to keep you alive.

Why is a heartbeat mechanism needed?
Due to the unreliability of the network, it is possible that during the process of TCP maintaining a long connection, due to certain emergencies, such as the network cable being pulled out, a sudden power failure, etc.

It will cause the connection between the server and the client to be interrupted. In these emergencies, if there happens to be no interaction between the server and the client, they will not be able to find out that the other party has been offline in a short period of time.

The heartbeat mechanism can solve such problems.

KeepAlive mechanism of TCP protocol
The default KeepAlive state is not turned on.

Setsockopt needs to be set to SOL_SOCKET.SO_KEEPALIVE to 1 to open the KeepAlive state.

And three parameters can be set:

tcp_keepalive_time, tcp_keepalive_probes, tcp_keepalive_intvl,

They respectively represent: how long the connection will be idle before sending keepalive ack packets, how many ack packets will be sent without reply before the other party is considered disconnected, and the interval between two ack packets.

Many network devices, especially NAT routers, cannot maintain all connections on them due to their hardware limitations (such as memory and CPU processing power). Therefore, when necessary, some inactive connections will be selected and kicked out of the connection pool. .

The typical approach is LRU, which drops the connection that has had no data for the longest time.

By using TCP’s KeepAlive mechanism (modifying the time parameter), the connection can generate some ack packets every short period of time to reduce the risk of being kicked out. Of course, this comes at the cost of additional network and CPU burden.

How to implement the heartbeat mechanism?
Implement heartbeat mechanism:

Use the keepalive mechanism at the TCP protocol level.

Implement a customized heartbeat mechanism on the application layer.

Although the keepalive mechanism is provided at the TCP protocol level, there are several disadvantages to using it:

It is not a standard protocol for TCP and is turned off by default.

The TCP keepalive mechanism depends on the implementation of the operating system. The default keepalive heartbeat time is two hours, and modifications to the keepalive require system calls (or modifications to the system configuration), which is not flexible enough.

TCP keepalive is bound to the TCP protocol, so if it needs to be changed to the UDP protocol, the keepalive mechanism will be ineffective.

Using the keepalive mechanism at the TCP level saves traffic compared to the custom application layer heartbeat mechanism.
Detailed steps: Keepalived implements high availability of Httpd load balancing machine

Reference for part of the content:
Long connection, short connection, heartbeat mechanism and disconnection reconnection
What exactly are HTTP long connections and short connections?
—————-
Copyright statement: This article is an original article by CSDN blogger “NBED” and follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement when reprinting.
Original link: https://blog.csdn.net/xumneg111/article/details/120904893