0
0
RabbitMQdevops~5 mins

Lazy queues for memory management in RabbitMQ - Commands & Configuration

Choose your learning style9 modes available
Introduction
When a RabbitMQ queue holds many messages, it can use a lot of memory. Lazy queues help by keeping messages on disk instead of in memory, which saves RAM and keeps the server stable.
When your queue receives a large number of messages that do not need to be processed immediately.
When you want to prevent RabbitMQ from using too much memory and slowing down or crashing.
When messages can wait in the queue for some time before being consumed.
When running RabbitMQ on servers with limited RAM and you want to avoid memory pressure.
When you want to improve stability for workloads with bursty or heavy message loads.
Config File - lazy-queue.json
lazy-queue.json
{
  "name": "my-lazy-queue",
  "durable": true,
  "arguments": {
    "x-queue-mode": "lazy"
  }
}

This JSON file defines a durable RabbitMQ queue named my-lazy-queue. The key part is the arguments section where x-queue-mode is set to lazy. This tells RabbitMQ to keep messages on disk instead of memory, helping with memory management.

Commands
This command creates a durable queue named 'my-lazy-queue' with the lazy mode enabled to save memory by storing messages on disk.
Terminal
rabbitmqadmin declare queue name=my-lazy-queue durable=true arguments='{"x-queue-mode":"lazy"}'
Expected OutputExpected
Successfully declared queue 'my-lazy-queue'
durable=true - Makes the queue survive RabbitMQ restarts
arguments='{"x-queue-mode":"lazy"}' - Sets the queue to lazy mode for memory management
This command lists all queues with their names, durability, and arguments to verify the lazy queue was created correctly.
Terminal
rabbitmqctl list_queues name durable arguments
Expected OutputExpected
Listing queues ... my-lazy-queue true {"x-queue-mode":"lazy"}
Check the overall RabbitMQ server status to ensure it is running well after creating the lazy queue.
Terminal
rabbitmqctl status
Expected OutputExpected
Status of node rabbit@localhost ... [{pid,12345},{running_applications,[{rabbit,"RabbitMQ","3.11.14"}]}]
Key Concept

If you remember nothing else from this pattern, remember: lazy queues keep messages on disk to save memory and improve RabbitMQ stability under heavy load.

Common Mistakes
Creating a queue without the 'x-queue-mode' argument
The queue will keep messages in memory, causing high RAM usage and possible crashes.
Always add the argument 'x-queue-mode' set to 'lazy' when you want a lazy queue.
Setting 'x-queue-mode' to 'lazy' but not making the queue durable
Messages may be lost if RabbitMQ restarts because the queue is not durable.
Set the queue to durable=true along with lazy mode to keep messages safe across restarts.
Summary
Create a queue with the argument 'x-queue-mode' set to 'lazy' to store messages on disk.
Use 'rabbitmqadmin' to declare the lazy queue and 'rabbitmqctl list_queues' to verify it.
Lazy queues help reduce memory use and improve RabbitMQ stability for large message loads.