Heartbeat Mechanism in Software Design

        foreword
       In the design architecture of the software, heartbeat detection is very important. For example, in dubbo service and web api invoke, the consumer side needs to sense whether the provider side is alive or not. If it is not alive, switch to call another provider.
 
       1. What is heartbeat detection
       To judge whether the other party (device, process or other network element) is operating normally, it is generally used to send simple communication packets at regular intervals. Used to detect abnormal disconnection of TCP.
       The basic reason is that the server cannot effectively determine whether the client is online. That is, the server cannot distinguish whether the client has been idle for a long time or has been disconnected. The so-called heartbeat packet is that the client regularly sends simple information to the server to tell it that I am still there.
       The code is to send a fixed message to the server every few minutes, and the server will reply with a fixed message after receiving it. If the server does not receive client information within a few minutes, the client is considered disconnected. For example, if some communication software is not used for a long time, if you want to know whether its status is online or offline, you need heartbeat packets, and send and receive packets regularly.
        The contracting party can be the client or the server, whichever is convenient and reasonable. Usually the client. The server can also periodically poll and send heartbeats.
       Generally speaking, for the sake of efficiency, the client actively sends packets to the server, not the other way around.
 
       Second, the realization mechanism of the heartbeat mechanism
Both receiving and sending data in the network are implemented using SOCKET in the operating system. But if the socket has been disconnected, there must be problems with sending and receiving data. But how to judge whether the socket can still be used? This requires the creation of a heartbeat mechanism in the system. In fact, TCP has implemented a mechanism called heartbeat for us. If you set the heartbeat, then TCP will send the heartbeat of the number of times you set (for example, 2 times) within a certain period of time (for example, if you set it to 3 seconds), and this information will not affect the protocol you define yourself. . The so-called "heartbeat" is to regularly send a custom structure ( heartbeat packet or heartbeat frame) to let the other party know that it is "online". to ensure the validity of the link.
The so-called heartbeat packet is that the client regularly sends simple information to the server to tell it that I am still there. The code is to send a fixed message to the server every few minutes, and the server will reply with a fixed message after receiving it. If the server does not receive the client message within a few minutes, the client will be considered disconnected. For example, if some communication software is not used for a long time, if you want to know whether its status is online or offline, you need heartbeat packets, and send and receive packets regularly. Contractor: It can be the client or the server, whichever is convenient and reasonable. Usually the client. The server can also periodically poll and send heartbeats. The reason why the heartbeat packet is called a heartbeat packet is because it is sent every fixed time like a heartbeat to tell the server that the client is still alive. In fact, this is to maintain a long connection . As for the content of this packet, there is no special regulation, but it is generally a small packet, or an empty packet containing only the packet header.
In the mechanism of TCP, there is a mechanism for heartbeat packets, which is the option of TCP. The system default is to set the heartbeat frequency of 2 hours. But it can't check that the machine is powered off, the network cable is unplugged, and the firewall is disconnected. And the logic layer may not handle disconnection so well. In general, if it is only used for keeping alive, it is still ok. Heartbeat packets are generally implemented by sending empty packets at the logical layer. In the next timer, an empty packet is sent to the client at a certain time interval, and then the client returns the same empty packet. If the server does not receive the feedback packet sent by the client within a certain period of time, it can only determine Said disconnected. Just send or recv, if the result is zero, it is dropped. 
However, under a long connection , there may be no data exchange for a long time. In theory, this connection is always connected, but in practice, it is difficult to know if the intermediate node fails. Even worse, some nodes ( firewalls ) will automatically disconnect connections that have no data interaction within a certain period of time. At this time, we need our heartbeat packet to maintain a long connection and keep alive. After learning the disconnection, the server logic may need to do some things, such as data cleaning and reconnection after disconnection. Of course, this must be done by the logic layer according to the requirements.
       In general, heartbeat packets are mainly used for keep-alive and disconnection processing of long connections. In general applications, the determination time is 30-40 seconds. If it is really demanding, it is 6-9 seconds.
 
       3. The basic steps of heartbeat detection:
       1. The client sends a detection packet to the server every time interval.
       2. The client starts a timeout timer when sending packets.
       3. The server side receives the detection packet and should respond with a packet.
       4. If the client receives the response packet from the server, the server is normal, and the timeout timer is deleted.
       5. If the client's timeout timer expires and the response packet is still not received, the server is hung up.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327041321&siteId=291194637