Detecting Dead TCP Connections with Heartbeats

Introduction

Network can fail in many ways, sometimes pretty subtle (e.g. high ratio packet loss). Disrupted TCP connections take a moderately long time (about 11 minutes with default configuration on Linux, for example) to be detected by the operating system. AMQP 0-9-1 offers a heartbeat feature to ensure that the application layer promptly finds out about disrupted connections (and also completely unresponsive peers). Heartbeats also defend against certain network equipment which may terminate "idle" TCP connections.

Heartbeat Timeout Interval

The heartbeat timeout value defines after what period of time the peer TCP connection should be considered dead by RabbitMQ and client libraries. This value is negotiated between the client and RabbitMQ server at the time of connection. The client must be configured to request heartbeats. In RabbitMQ versions 3.0 and higher, the broker will attempt to negotiate heartbeats by default (although the client can still veto them). The timeout is in seconds, and default value is 60 (580 prior to release 3.5.5).

Heartbeat frames are sent about every timeout / 2 seconds. After two missed heartbeats, the peer is considered to be unreachable. Different clients manifest this differently but the TCP connection will be closed. When a client detects that RabbitMQ node is unreachable due to a heartbeat, it needs to re-connect.

Heartbeats can be disabled by setting the timeout interval to 0.

Enabling Heartbeats with Java Client

To configure the heartbeat timeout in the Java client, set it with ConnectionFactory#setRequestedHeartbeat before creating a connection:

ConnectionFactory cf = new ConnectionFactory();

// set the heartbeat timeout to 60 seconds
cf.setRequestedHeartbeat(60);

Note that in case RabbitMQ server has a non-zero heartbeat timeout configured (which is the default in versions starting with 3.6.x), the client can only lower the value but not increase it.

Enabling Heartbeats with the .NET Client

To configure the heartbeat timeout in the .NET client, set it with ConnectionFactory.RequestedHeartbeat before creating a connection:

var cf = new ConnectionFactory();

// set the heartbeat timeout to 60 seconds
cf.RequestedHeartbeat = 60;

Low Timeout Values and False Positives

Setting heartbeat timeout value too low can lead to false positives (peer being considered unavailable while it really isn't the case) due to transient network congestion, short-lived server flow control, and so on. This should be taken into consideration when picking a timeout value.

Several years worth of feedback from the users and client library maintainers suggest that values lower than 5 seconds are fairly likely to cause false positives, and values of 1 second or lower are very likely to do so. Values within the 5 to 20 seconds range are optimal for most environments.

Heartbeats in STOMP

STOMP 1.2 includes heartbeats. In STOMP, heartbeat timeouts can be assymetrical: that is to say, client and server can use different values. RabbitMQ STOMP plugin fully supports this feature.

Heartbeats in STOMP are opt-in. To enable them, use the heart-beat header when connecting. See STOMP specification for an example.

Heartbeats in Shovel and Federation Plugins

Shovel and Federation plugins open Erlang client connections to RabbitMQ nodes under the hood. As such, they can be configured to use a desired heartbeat value.

Please refer to the AMQP 0-9-1 URI query parameters reference for details.