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.
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:
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:
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:
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:
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:
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:
| Method | Tool/API | Command/Endpoint | Data Provided |
| Management HTTP API | RabbitMQ Management HTTP API | GET /api/queues/%2F/[queue_name] | JSON with messages, messages_ready, messages_unacknowledged |
| Command-line | rabbitmqctl | rabbitmqctl list_queues name messages | Plain text or JSON list of queues and message counts |
| Monitoring Tool | Prometheus with RabbitMQ plugin | Configured via RabbitMQ Prometheus settings | Continuous 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
- Getting PreconditionFailed - inequivalent arg 'x-max-priority' for queue error when trying to set up priority queues with Celery+RabbitMQ
- Golang - RabbitMq channel/connection is not open
- Golang TLS with Kafka-Go and Certificates. No Connection
- Good practice when using kafka with jpa
- Got Pipelining of requests forbidden in c# rabbitmq client
- Gracefully restart a Reactive-Kafka Consumer Stream on failure
- Groups of chains with positional arguments in partial tasks using Celery
- Guaranteed delivery of multiple messages to Kafka cluster

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.