Distributed Message Service RabbitMQ

Lazy Queue

2024-06-27 06:38:05

Application Scenario

RabbitMQ introduced the concept of Lazy Queue starting from version 3.6.0. Lazy queues store messages on disk as much as possible and are not loaded into memory until the corresponding message is consumed by the consumer. One of its important design objectives is to be able to support longer queues, that is, to support more message storage. Lazy queues are necessary when consumers are unable to consume messages for a long time due to various reasons, such as consumer offline, downtime, or shutdown due to maintenance

By default, when the producer sends a message to RabbitMQ, the messages in the queue are stored in memory as much as possible, so that the message can be sent to the consumer more quickly. Even persistent messages will have a backup residing in memory while being written to the disk. When RabbitMQ needs to free up memory, it will page the messages in memory to the disk, which will take a long time and block the queue operation, making it unable to receive new messages. Although the developers of RabbitMQ have been upgrading related algorithms, the results are still not ideal, especially when the message volume is particularly large.

The lazy queue will directly store the received message into the file system, regardless of whether it is persistent or non-persistent, which can reduce the consumption of memory, but will increase the use of I/O. If the message is persistent, such I/O operation is inevitable, and the lazy queue and persistent message can be described as "best partners". Note that if non persistent messages are stored in the lazy queue, memory usage will remain stable, but messages will still be lost after restarting.

Set up Lazy Queue

The queue has two modes: default and lazy, and the default mode is "default". Lazy mode is the mode of lazy queue, which can be set in the parameters when calling the channel.queueDeclare method.

The following example demonstrates setting up a lazy queue on the Java client by calling channel.queueDeclare.

Map<String, Object> args = new HashMap<String, Object>();

args.put("x-queue-mode","lazy"); channel.queueDeclare("test_queue", false, false, false, args);


.3oE07YkCi2z