RabbitMQ + Memory Limits
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
RabbitMQ is an open-source message broker that is widely used for its support of multiple messaging protocols. It's designed to handle the passing of messages between systems in a scalable and controlled way. Memory management is a crucial aspect to understand and properly configure when deploying and maintaining a RabbitMQ service. Wrong memory settings can lead to system crashes, slow message processing, or node failures.
Understanding Memory in RabbitMQ
RabbitMQ primarily uses memory to store messages, queues, and other internal structures. When RabbitMQ uses too much memory, it can trigger flow control to reduce the speed at which producers send messages, or in extreme cases, could lead to the system crashing. Therefore, managing its memory usage is critical for the stability and performance of your messaging system.
Memory Limit Configuration
RabbitMQ allows setting a maximum memory limit past which it will start blocking new messages from producers, called Memory Pressure. This limit can be set in absolute terms (e.g., 2GB) or as a percentage of the available memory. The default setting for RabbitMQ memory limit is set to 40% of the available memory.
To set or adjust the memory limit, use the RabbitMQ configuration file (rabbitmq.conf) or use environment variables. Here is an example on how to set it in the configuration file:
This sets the memory watermark to 50% of the detected RAM.
Monitoring Memory Usage
RabbitMQ provides built-in tools for monitoring, called the RabbitMQ Management Plugin, which give insights into how much memory RabbitMQ is currently using. Besides this, command-line tools like rabbitmqctl could be used:
This command displays detailed status information about the RabbitMQ node, including current memory usage.
Memory Allocations in RabbitMQ
RabbitMQ uses Erlang's garbage collected environment for managing memory, which means that memory is not released immediately after messages are removed but reclaimed in chunks periodically. Several types of memory can be allocated within RabbitMQ:
- Reserved Memory: Memory reserved for Erlang's VM processes.
- Persistent Message Storage: Messages that are set to be durable (survive a broker restart) are written to disk, but also stored in memory for quick access until they're confirmed written.
- Transient Message Storage: Messages that are non-durable remain in memory only and are lost when a node is restarted.
Best Practices
Here are some best practices for managing memory in RabbitMQ:
- Monitor: Regularly monitor the memory usage using RabbitMQ management tools.
- Scale: Scale your RabbitMQ deployment horizontally (add more nodes) or vertically (add more memory) depending on your load.
- Optimize Message Flow: Adjust producer rates or implement publisher confirms to manage the rate at which messages are published.
- Message Size: Consider the size of the messages being queued. Large messages consume significantly more memory.
Summary Table
| Parameter | Description | Example Value |
| vm_memory_high_watermark.perc | Percentage of RAM allocated to RabbitMQ | 0.5 (50%) |
| vm_memory_high_watermark.abs | Absolute RAM limit allocated to RabbitMQ | 2GB |
| reserved_memory | Memory reserver for Erlang VM processes | 512MB |
By understanding and configuring the memory settings wisely, system administrators can ensure that their RabbitMQ operates effectively and that resources are utilized optimally, preserving both the stability and performance of the system.

