Menu

Queue Length Limit

Introduction

The maximum length of a queue can be limited to a set number of messages, or a set number of bytes (the total of all message body lengths, ignoring message properties and any overheads), or both.

For any given queue, the maximum length (of either type) can be defined by clients using the queue's arguments, or in the server using policies. In the case where both policy and arguments specify a maximum length, the minimum of the two values is applied.

Queue length settings also can be enforced by operator policies.

In all cases the number of ready messages is used; unacknowledged messages do not count towards the limit. The fields messages_ready and message_bytes_ready from rabbitmqctl list_queues and the management API show the values that would be limited.

Default Max Queue Length Behaviour

The default behaviour for RabbitMQ when a maximum queue length or size is set and the maximum is reached is to drop or dead-letter messages from the front of the queue (i.e. the oldest messages in the queue). To modify this behaviour, use the overflow setting described below.

Queue Overflow Behaviour

Use the overflow setting to configure queue overflow behaviour. If overflow is set to reject-publish, the most recently published messages will be discarded. In addition, if publisher confirms are enabled, the publisher will be informed of the reject via a basic.nack message. If a message is routed to multiple queues and rejected by at least one of them, the channel will inform the publisher via basic.nack. The message will still be published to all other queues which can enqueue it.

Define Max Queue Length Using a Policy

To specify a maximum length using policy, add the key max-length and / or max-length-bytes to a policy definition. For example:

rabbitmqctl
rabbitmqctl set_policy my-pol "^one-meg$" '{"max-length-bytes":1048576}' --apply-to queues
rabbitmqctl (Windows)
rabbitmqctl.bat set_policy my-pol "^one-meg$" "{""max-length-bytes"":1048576}" --apply-to queues

The my-pol policy ensures that the one-meg queue contains no more than 1MiB of message data. When the 1MiB limit is reached, the oldest messages are discarded from the head of the queue.

To define an overflow behaviour - whether to drop messages from head or to reject new publishes, add the key overflow to a policy definition. For example:

rabbitmqctl
rabbitmqctl set_policy my-pol "^two-messages$" '{"max-length":2,"overflow":"reject-publish"}' --apply-to queues
rabbitmqctl (Windows)
rabbitmqctl.bat set_policy my-pol "^two-messages$" "{""max-length"":2,""overflow"":""reject-publish""}" --apply-to queues

The my-pol policy ensures that the two-messages queue contains no more than 2 messages and all additional publishes are sent basic.nack responses as long as the queue contains 2 messages and publisher confirms are enabled.

Policies can also be defined using the management plugin, see the policy documentation for more details.

Define Max Queue Length Using x-arguments During Declaration

Maximum number of messages can be set by supplying the x-max-length queue declaration argument with a non-negative integer value.

Maximum length in bytes can be set by supplying the x-max-length-bytes queue declaration argument with a non-negative integer value.

If both arguments are set then both will apply; whichever limit is hit first will be enforced.

Overflow behaviour can be set by supplying the x-overflow queue declaration argument with a string value. Possible values are drop-head (default) or reject-publish

This example in Java declares a queue with a maximum length of 10 messages:

Map<String, Object> args = new HashMap<String, Object>();
args.put("x-max-length", 10);
channel.queueDeclare("myqueue", false, false, false, args);