How does rabbitmq heartbeat work

Oleg Majewski picture Oleg Majewski · Nov 10, 2015 · Viewed 19.8k times · Source

The native rabbitmq client for java allows to setup heartbeat on connection settings, for example like this:

import com.rabbitmq.client.ConnectionFactory;

...

ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setAutomaticRecoveryEnabled(true);
        connectionFactory.setHost("some://host");
        connectionFactory.setConnectionTimeout(5000);
        connectionFactory.setRequestedHeartbeat(5); // keeps an idle connection alive

What is the rabbitmq client doing with the heartbeat settings? Is it sending a stubbed message to special exchange/queue or what does it else?

Can somebody explain it in details?

Answer

Derick Bailey picture Derick Bailey · Nov 11, 2015

from the RMQ Heartbeat documentation:

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.

This isn't a request to a queue or stubbed message. This is a TCP/IP connection with packets sent across in a specific format for the heartbeat.

If you want the real details, you can read the AMQP 0.9.1 Specification, section 4.2.1 and 4.2.7 with errata on how RabbitMQ corrects for errors in the specification, as well.