RabbitMQ
Message Queuing
Programming
Coding Tips
Software Development

Getting number of messages in a RabbitMQ queue

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

RabbitMQ is a widely-used open-source message broker, known for its robustness and the ability to handle complex messaging scenarios. When working with RabbitMQ, one of the common operational tasks is to check the number of messages queued in various queues. This information is crucial for monitoring and maintaining optimal performance and resource management. Let's explore ways to retrieve the count of messages in a RabbitMQ queue.

Understanding Queue Message Counts in RabbitMQ

1. RabbitMQ Management HTTP API

RabbitMQ ships with a built-in management plugin, which provides a comprehensive HTTP API. Among other capabilities, this API allows users to fetch details about specific queues, including the number of messages they contain.

To use the HTTP API, you need to enable the RabbitMQ management plugin if it hasn't been enabled already:

 
rabbitmq-plugins enable rabbitmq_management

Once enabled, you can access the API to get the message count. For instance, to get information about a queue named test_queue on the default virtual host, you'd use a curl command like this:

bash
curl -u user:password http://localhost:15672/api/queues/%2F/test_queue

This returns a JSON object containing various details about the queue, including messages_ready (messages ready to be delivered) and messages_unacknowledged (messages delivered but not yet acknowledged). The total number of messages (messages) in the queue is also included.

2. The rabbitmqctl Command-line Tool

For those who prefer using CLI, RabbitMQ provides the rabbitmqctl tool, which can also be used to fetch queue details:

bash
rabbitmqctl list_queues name messages

This command will list all queues with their names and the total number of messages. You can specify the name of a specific queue to get information for just that one:

bash
rabbitmqctl list_queues name messages -p my_vhost --formatter=json

Using the --formatter=json option prints the output in JSON format, which might be easier to integrate into scripts or other tools.

Tailoring the Data with rabbitmqctl

The tool's various flags and options, such as -p for specifying the virtual host, help tailor the command to your specific needs. Additionally, using the grep command can simplify searching for a specific queue among many:

bash
rabbitmqctl list_queues name messages | grep test_queue

Monitoring Over Time

For ongoing monitoring, especially in a dynamic environment where message rates can vary significantly, it's helpful to set up routine checks or integrate with monitoring tools like Prometheus, which can scrape data from the RabbitMQ Prometheus endpoint if enabled.

Performance Considerations

When querying queue message counts, especially in environments with a large number of queues or high message throughput, consider the potential impact on the performance of the broker and the accuracy of the data. Data fetched is essentially a snapshot at a specific time, and queues can change rapidly.

Summary Table

Here’s a quick reference guide that covers key points about getting the number of messages in a queue:

MethodTool/APICommand/EndpointData Provided
Management HTTP APIRabbitMQ Management HTTP APIGET /api/queues/%2F/[queue_name]JSON with messages, messages_ready, messages_unacknowledged
Command-linerabbitmqctlrabbitmqctl list_queues name messagesPlain text or JSON list of queues and message counts
Monitoring ToolPrometheus with RabbitMQ pluginConfigured via RabbitMQ Prometheus settingsContinuous monitoring data stream

Closing Thoughts

Retrieving the number of messages in RabbitMQ queues can be approached in several ways depending on your environment and requirements. Whether you prefer using an HTTP API for integration, a command-line tool for manual checks, or automated monitoring tools, RabbitMQ provides the necessary interfaces to support effective queue management and monitoring.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.