0
0
RabbitMQdevops~5 mins

Queue length limits in RabbitMQ - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes queues get too long and use too much memory. Queue length limits help stop queues from growing beyond a set size to keep the system stable.
When you want to prevent a queue from using too much memory by limiting how many messages it holds.
When you want to drop or reject new messages if the queue is full to avoid slowing down the system.
When you want to keep your RabbitMQ server responsive under heavy load by controlling queue size.
When you want to avoid message buildup that can cause delays in processing.
When you want to automatically remove old messages to make room for new ones.
Config File - queue-limit-policy.json
queue-limit-policy.json
{
  "vhost": "/",
  "name": "limit-queue-length",
  "pattern": "^my-queue$",
  "definition": {
    "max-length": 1000,
    "overflow": "reject-publish"
  },
  "priority": 0,
  "apply-to": "queues"
}

This JSON file defines a policy for RabbitMQ queues.

vhost: The virtual host where the policy applies.

name: The name of the policy.

pattern: Regex to match queue names this policy applies to.

definition: Contains max-length which limits queue size to 1000 messages, and overflow set to reject-publish which rejects new messages when full.

priority: Policy priority if multiple policies match.

apply-to: Specifies this policy applies to queues.

Commands
This command creates a policy named 'limit-queue-length' that limits queues named 'my-queue' to 1000 messages and rejects new messages when full.
Terminal
rabbitmqctl set_policy limit-queue-length '^my-queue$' '{"max-length":1000,"overflow":"reject-publish"}' --apply-to queues
Expected OutputExpected
Setting policy "limit-queue-length" for pattern "^my-queue$" to "{\"max-length\":1000,\"overflow\":\"reject-publish\"}" for queues in vhost "/" ...
--apply-to - Specifies the policy applies to queues
This command lists all queues with their names and how many messages are ready or unacknowledged, so you can check queue length.
Terminal
rabbitmqctl list_queues name messages_ready messages_unacknowledged
Expected OutputExpected
Listing queues ... my-queue 500 0 another-queue 10 2
This command removes the 'limit-queue-length' policy if you want to stop limiting the queue size.
Terminal
rabbitmqctl clear_policy limit-queue-length
Expected OutputExpected
Clearing policy "limit-queue-length" ...
Key Concept

If you remember nothing else from this pattern, remember: setting a max-length with overflow controls how many messages a queue holds and what happens when it is full.

Common Mistakes
Not using the correct regex pattern to match the queue name.
The policy won't apply if the pattern does not match the queue name exactly.
Use '^queue-name$' to match the exact queue name or a proper regex to match multiple queues.
Forgetting to specify --apply-to queues flag.
The policy may not apply to queues and will be ignored or applied incorrectly.
Always include --apply-to queues when setting queue policies.
Setting max-length too low without considering message flow.
Messages get rejected too often, causing message loss or errors.
Choose a max-length that balances memory use and message throughput.
Summary
Use rabbitmqctl set_policy with max-length and overflow to limit queue size and control message behavior when full.
Check queue lengths with rabbitmqctl list_queues to monitor message counts.
Remove policies with rabbitmqctl clear_policy when limits are no longer needed.