issues that may occur with TCP long connections
1. Many firewall routers and so on for the free socket auto-shutdown
2. For non-normal disconnection, the server is not able to detect. In order to reclaim resources, a detection mechanism must be provided.
So, there is the heartbeat (HeartBeat) mechanism.
two implementation schemes of heartbeat mechanism
For now, there are two ways to implement TCP keepalive (industry status is almost the first one in IM)
1. The heartbeat mechanism implemented by the Application layer protocol
Many application-layer protocols have a heartbeat mechanism, which is an additional message type defined for heartbeat messages by applying their own implementation of the heartbeat of the application layer. Usually the client sends a packet to the server every other time, notifying the server that it is still online and transmitting some data that may be necessary. Typical protocols for using heartbeat packets are IM, such as qq/msn/fetion.
2. The heartbeat mechanism supported by the TCP protocol KeepAlive
Open the So_keepalive option that the TCP protocol already has. Generally implemented on the server side, the client passive response to the previous blog has a specific introduction, here is not much to say.
advantages and disadvantages of TCP keepalive and application layer heartbeat
1. KeepAlive of the TCP protocol
Advantages:
The system kernel is fully automatic for the upper application, and the kernel level timer is more efficient than the upper application.
The upper application only needs to handle the data sending and receiving, the connection exception notification.
It is simple to use and reduces the complexity of the application layer code. It also saves traffic because the application tier is added to the TCP protocol layer with an additional header packet end. The detection packets provided by the TCP protocol are more refined and consume less traffic in theory.
Disadvantages:
1th, KeepAlive can only detect the connection to survive, but not to detect the connection is available, such as a server for some reason the load is very high, the CPU is full, unable to respond to any business requests, but the use of TCP probes can still determine the connection state, This is the typical connection alive, but the business provider is dead, for the client, the best option at this time is to reconnect to the other servers after disconnection, rather than always think the current server is available, has been to the current server to send some inevitable failure of the request;
2nd, if the other end of the TCP connection suddenly dropped, this time we do not know that the network has been shut down. At this point, TCP will automatically retransmit if there is a failure to send data. The retransmission packet has a higher priority than the KeepAlive package, which means that our keepalive can never be sent out. At this point, we do not know that the connection has been faulted and interrupted. We will not know after a long time of retransmission failure.
2. Application Layer Heartbeat
Advantages:
With greater flexibility to control the timing, interval and process of detection, even with additional information on the heartbeat package, the most important thing is to do without the above mentioned shortcomings, not only to detect the connection exists, but also to detect the connection is available.
General, the application layer's heartbeat does not depend on the protocol. If one day without TCP to change to UDP, the protocol layer does not provide heartbeat mechanism, but your application layer of heartbeat is still common, may only need to make a few changes to continue to use.
Cons: Need to implement themselves, increase the development workload, due to the application of a specific network framework, may also increase the complexity of the code structure, the application layer heartbeat traffic consumption is still greater, after all, this is essentially a normal packet.