Flow Control

There are two flow control mechanisms in RabbitMQ. Both work by exerting TCP backpressure on connections that are publishing too fast. They are:

Both mechanisms will temporarily block connections - the server will pause reading from the sockets of connected clients which send content-bearing methods (such as basic.publish) which have been blocked. Connection heartbeat monitoring will be disabled too.

The intent here is to introduce a flow control mechanism that throttles producers but lets consumers continue unaffected. However, since AMQP permits producers and consumers to operate on the same channel, and on different channels of a single connection, this logic is necessarily imperfect. In practice that does not pose any problems for most applications since the throttling is observable merely as a delay. Nevertheless, other design considerations permitting, it is advisable to only use individual AMQP connections for either producing or consuming.

Per-Connection Flow Control

RabbitMQ will block connections which are publishing too quickly for queues to keep up. No configuration is required.

A blocked connection will show a state of blocked in rabbitmqctl and the management plugin. Typically such fast-publishing connections will be blocked and unblocked several times per second, so inspecting a connection's state may not show per-connection flow control happening clearly. The last_blocked_by and last_blocked_age fields in rabbitmqctl and the management plugin can be inspected to determine whether flow control is taking place. The management plugin web UI unifies this information into a single field.

Memory-Based Flow Control

The RabbitMQ server detects the total amount of RAM installed in the computer on startup and when rabbitmqctl set_vm_memory_high_watermark fraction is executed. By default, when the RabbitMQ server uses above 40% of the installed RAM, it raises a memory alarm and blocks all connections. Once the memory alarm has cleared (e.g. due to the server paging messages to disk or delivering them to clients) normal service resumes.

The default memory threshold is set to 40% of installed RAM. Note that this does not prevent the RabbitMQ server from using more than 40%, it is merely the point at which publishers are throttled. Erlang's garbage collector can, in the worst case, cause double the amount of memory to be used (by default, 80% of RAM). It is strongly recommended that OS swap or page files are enabled.

32-bit architectures impose a per process memory limit of 4GB, though under Windows, this is frequently further reduced to 2GB. Common implementations of 64-bit architectures (i.e. AMD64 and Intel EM64T) permit only a paltry 256TB per process. 64-bit Windows again further limits this to 8TB. However, note that even under 64-bit Windows, a 32-bit process frequently only has a maximum address space of 2GB.

Configuring the Memory Threshold

The memory threshold at which the flow control is triggered can be adjusted by editing the rabbitmq.config file (in the appropriate location for your platform, as discussed in the configuration guide). The example below sets the threshold to the default value of 0.4:

[{rabbit, [{vm_memory_high_watermark, 0.4}]}].

The default value of 0.4 stands for 40% of installed RAM or 40% of available virtual address space, whichever is smaller. E.g. on a 32-bit Windows platform, if you have 4GB of RAM installed, 40% of 4GB is 1.6GB, but 32-bit Windows normally limits processes to 2GB, so the threshold is actually to 40% of 2GB (which is 820MB).

A value of 0 makes the memory alarm go off immediately and thus disables all publishing (this may be useful if you wish to disable publishing globally; use rabbitmqctl set_vm_memory_high_watermark 0). To prevent the memory alarm from going off at all, set some high multiplier such as 100.

The memory limit is appended to the RABBITMQ_NODENAME.log file when the RabbitMQ server starts:

=INFO REPORT==== 29-Oct-2009::15:43:27 ===
Memory limit set to 2048MB.
The memory limit may also be queried using the rabbitmqctl status command.

The threshhold can be changed while the broker is running using the rabbitmqctl set_vm_memory_high_watermark fraction command. This command will take effect until the broker shuts down. The corresponding configuration setting should also be changed when the effects should survive a broker restart. The memory limit may change on systems with hot-swappable RAM when this command is executed without altering the threshhold, due to the fact that the total amount of system RAM is queried.

Versions of Erlang prior to R13B had a bug that meant they do not reliably detect the amount of memory installed on Windows platforms. If RabbitMQ is running under Windows on a version of Erlang prior to R13B, then it does not attempt to automatically set a threshold. Instead, it behaves as if it's unable to recognise the platform: see below.

Unrecognised platforms

If the RabbitMQ server is unable to recognise your system, or if you're using Windows and a version of Erlang prior to R13B, it will append a warning to the RABBITMQ_NODENAME.log file. It then assumes than 1GB of RAM is installed:

=WARNING REPORT==== 29-Oct-2009::17:23:44 ===
Unknown total memory size for your OS {unix,magic_homebrew_os}. Assuming memory size is 1024MB.

In this case, the vm_memory_high_watermark configuration value is used to scale the assumed 1GB RAM. With the default value of vm_memory_high_watermark set to 0.4, RabbitMQ's memory threshold is set to 410MB, thus it will throttle producers whenever RabbitMQ is using more than 410MB memory. Thus when RabbitMQ can't recognize your platform, if you actually have 8GB RAM installed and you want RabbitMQ to throttle producers when the server is using above 3GB, set vm_memory_high_watermark to 3.

It is advised you do not set the threshold above 50% of your installed RAM, and to note that under Windows with a 32-bit Erlang VM, it is commonly the case that a maximum of 2GB memory is available to RabbitMQ.