Updated on 2023-07-20 GMT+08:00

Heartbeat Detection

RabbitMQ heartbeats help the application layer detect interrupted connections and unresponsive peers in a timely manner. Heartbeats also prevent some network devices from disconnecting TCP connections where there is no activity for a certain period of time.

Heartbeat Timeout

The heartbeat timeout defines after how long the peer TCP connection is considered closed by the server or client.

After a timeout is set on the RabbitMQ server and client separately, the server and client negotiate the timeout value. The value must be configured for the client so that it can request heartbeats. The Java, .NET, and Erlang clients maintained by RabbitMQ use the following negotiation logic:

  • If the heartbeat timeout set on neither the server nor the client is 0, the smaller value is used.
  • If the heartbeat timeout is set to 0 on either the server or the client, the non-zero value is used.
  • If the heartbeat timeout set on both the server and the client is 0, heartbeats are disabled.

For more information about heartbeat detection, see Detecting Dead TCP Connections with Heartbeats and TCP Keepalives.

Heartbeat Frames

The interval for sending heartbeat frames (also called a heartbeat interval) is half of the heartbeat timeout. After a client misses two heartbeats, it is considered unreachable. Different clients show this differently, but the TCP connection will be closed. If the client detects that the server cannot be accessed due to heartbeats, the client needs to reconnect to the server.

Any traffic, including protocol operations, message publishing, message acknowledgment, and heartbeat frames, is considered as a valid heartbeat. If there is any other traffic on the connection, the client can choose to send heartbeat frames or not. If there is no other traffic on the connection, the client must send heartbeat frames.

Configuring Heartbeat Timeout on the Client

If the heartbeat timeout is set to a small value, false alarms may be reported in the case of temporary network congestion or temporary server traffic control. In most cases, it is recommended that the timeout be set to 5 to 20 seconds.

  • Java client

    Before creating a connection, configure the ConnectionFactory#setRequestedHeartbeat parameter. Example:

    ConnectionFactory cf = new ConnectionFactory(); 
    // The heartbeat timeout is 15 seconds.
    cf.setRequestedHeartbeat(15);
  • .NET client
    var cf = new ConnectionFactory();
    // The heartbeat timeout is 15 seconds.
    cf.RequestedHeartbeat = TimeSpan.FromSeconds(15);