Updated on 2024-11-27 GMT+08:00

Configuring Lazy Queues

By default, messages produced by RabbitMQ producers are stored in the memory. When the memory needs to be released, the messages will be paged out to the disk. Paging takes a long time, during which queues cannot process messages.

If production is too fast (for example during batch processing) or consumers cannot consume messages for a long time due to various reasons (for example when consumers are offline or broke down), a large number of messages will be stacked. Memory usage becomes high and paging is frequently triggered, which may affect message sending and receiving of other queues. In this case, you are advised to use lazy queues.

Lazy queues store as many messages to the disk as possible. Messages are loaded to the memory only when they are being consumed. This reduces memory consumption, but increases I/O and affects single-queue throughput. An important goal of lazy queues is to support long queues, that is, queues with many messages stored or stacked.

Lazy queues are recommended in the following scenarios:

  • Messages may be stacked in queues.
  • There is no high requirement on the queue performance (throughput), for example, less than 10,000 TPS.
  • Stable production and consumption are desired, without being affected by memory changes.

Lazy queues are not suitable in the following scenarios:

  • High RabbitMQ performance is expected.
  • Queues are always short (with no messages stacked).
  • The queue length limit is configured in a policy.

For more information about lazy queues, see Lazy Queues.

Available only for RabbitMQ 3.8.35.

Configuring a Lazy Queue

A queue has two modes: default and lazy. The default mode is default. To configure a queue to be lazy, you can use the channel.queueDeclare argument or a policy. If both these methods are used, the configuration set by the policy takes precedence.

  • The following example shows how to set a lazy queue by using channel.queueDeclare on a Java client.
    Map<String, Object> args = new HashMap<String, Object>();
    args.put("x-queue-mode", "lazy");
    channel.queueDeclare("myqueue", false, false, false, args);
  • The following figure shows how to use a policy to set a lazy queue on the RabbitMQ management UI.
    Figure 1 Using a policy to set a lazy queue