rabbitmqconsumerheartbeatrabbitmqctl

How to change RabbitMQ Heartbeat without restart


There are several questions here in SO about RabbitMQ heartbeat but I haven't found one addressing how to actually change the default heartbeat value of 60 seconds (580 seconds in previous versions).

In the case when a consumer is running for longer than 60 seconds and is incapable of producing any traffic that would count as heartbeat (for example PHP consumers), RabbitMQ will close the connection considering the consumer is dead, but the consumer might continue to run, and when it tries to produce the ACK the connection is closed and you get an error message like:

Broken pipe or closed connection

One can set the heartbeat at the consumer side to a higher value, for example 1800 seconds, but if the broker configuration is not changed, then the lower value will be use, in case of the default value then 60 seconds. From RabbitMQ docs:

The broker and client will attempt to negotiate heartbeats by default. When both values are non-0, the lower of the requested values will be used. If one side uses a zero value (attempts to disable heartbeats) but the other does not, the non-zero value will be used.

To change the Heartbeat value one can add the following line in /etc/rabbitmq/rabbitmq.conf (using the new configuration format)

heartbeat = 1800

This requires a restart, so the question is: How to change the rabbitmq heartbeat value without a restart?


Solution

  • I'm answering my own question since it took me some time to find how to do this, the documentation on how to use eval was not very helpful.

    It's possible to change RabbitMQ configuration values using eval:

    Evaluate an arbitrary Erlang expression.

    Using rabbitmqctl eval is then possible to change the heartbeat value without a restart like so:

    # Set
    rabbitmqctl eval 'application:set_env(rabbit, heartbeat, 1800).'
    
    # Get 
    rabbitmqctl eval 'application:get_env(rabbit, heartbeat).'