Updated on 2024-10-30 GMT+08:00

Configuring Heartbeats on the RabbitMQ Client

If messages may be consumed more than 90 seconds after they are produced, enable heartbeats on the client and set the heartbeats to shorter than 90 seconds, to prevent the client from being disconnected from a cluster RabbitMQ instance.

What Is a Heartbeat?

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. To enable heartbeats, specify the heartbeat timeout for connections.

The heartbeat timeout defines after how long the peer TCP connection is considered closed by the server or client. The server and client negotiate the timeout value. The client must be configured with the value to 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.

After the heartbeat timeout is configured, the RabbitMQ server and client send AMQP heartbeat frames to each other at an interval of half the heartbeat timeout. After a client misses two heartbeats, it is considered unreachable and the TCP connection is closed. If the client detects that the server cannot be accessed due to heartbeats, the client needs to reconnect to the server. For more information about heartbeats, see Detecting Dead TCP Connections with Heartbeats and TCP Keepalives.

Some clients (such as C clients) do not have the logic for sending heartbeats. Even if the heartbeat timeout is configured and heartbeats are enabled, heartbeats still cannot be sent. In this case, an extra thread needs to be started to compile the logic for sending heartbeats.

LVS Heartbeat Timeout

Cluster RabbitMQ instances use Linux Virtual Servers (LVSs) for load balancing, as shown in Figure 1. Single-node instances do not have LVSs.

Figure 1 Load balancing of a cluster instance

LVS configures a heartbeat timeout of 90 seconds by default on client connections. If a client does not send a heartbeat (AMQP heartbeat frames or messages) to LVS for 90 seconds, LVS disconnects the client and the client will need to reconnect to LVS.

If messages are consumed more than 90 seconds after they are produced, enable heartbeats on the client and set the heartbeat timeout to shorter than 90 seconds.

Configuring Heartbeats on a Client

  • Java client

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

    ConnectionFactory cf = new ConnectionFactory(); 
    // The heartbeat timeout is 60 seconds.
    cf.setRequestedHeartbeat(60);
  • .NET client
    var cf = new ConnectionFactory();
    // The heartbeat timeout is 60 seconds.
    cf.RequestedHeartbeat = TimeSpan.FromSeconds(60);
  • Python Pika
    // The heartbeat is 60 seconds.
    params = pika.ConnectionParameters(host='host', heartbeat=60, credentials=pika.PlainCredentials('username', 'passwd'))
    connection = pika.BlockingConnection(params)
    
    while True:
        channel.basic_publish(exchange='', routing_key='hello', body='Hello World!')
        print(" [x] Sent 'Hello World!'")
        # The producer needs to use connection.sleep() to trigger a heartbeat. time.sleep() cannot trigger heartbeats.
        connection.sleep(200)
  • PHP client
    // The heartbeat is 60 seconds.
    $connection = new AMQPStreamConnection(RMQ_HOST, RMQ_PORT, RMQ_USER, RMQ_PASS, RMQ_vhost, ['heartbeat'=> 60]);