Sub topic 15
Monitoring queue health
This chapter assumes you know how producers, consumers, acknowledgements, retries, and dead-letter queues work. It covers which numbers show that a queue is healthy and which show that it is not.
Why queues need their own monitoring
A queue hides trouble. A producer keeps succeeding while the consumers fall behind or stop. Nothing returns an error. Messages pile up, and users notice only when results arrive late or not at all.
Monitor the queue itself, not just the services around it.
The core signals
| Signal | What it tells you | Typical cause when it looks bad |
|---|---|---|
| Queue depth (ready messages) | Work waiting for a consumer | Consumers are slow, down, or outnumbered |
| Unacknowledged messages | Work delivered but not finished | Slow handlers, stuck consumers, prefetch set too high |
| Message age (oldest message) | How late the work is | Backlog, or a poison message at the head |
| Publish rate vs. ack rate | Whether consumers keep up | Ack rate below publish rate for a long time |
| Redelivery rate | How often messages are retried | Failing handlers, crashes before ack |
| Dead-letter queue depth | Messages that gave up | Bugs, bad payloads, a broken dependency |
| Consumer count | Whether anyone is listening | Crashed or disconnected consumers |
Depth alone is misleading. A depth of 10,000 is fine if consumers drain it in seconds. A depth of 50 is a problem if those messages are an hour old. Message age and the trend of depth usually matter more than the absolute number.
Reading the trend
Look at how the numbers move over time.
- Depth rising while the consumer count is steady means the consumers are too slow for the load. Add consumers or speed up the handler.
- Depth rising while the ack rate is zero means consumers are stuck or gone.
- A high unacknowledged count with a low ack rate means handlers are hanging. Check for blocked calls to a database or an external API.
- A high redelivery rate means messages fail and return to the queue. Check the logs before you add capacity, because more consumers will not fix a bug.
- A DLQ that grows means messages are failing permanently. Any non-zero DLQ depth deserves a look.
Checking a RabbitMQ queue
A passive declare reads a queue's state without changing it. It returns the ready count and the consumer count.
import pika
conn = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
ch = conn.channel()
# passive=True fails if the queue does not exist, and never creates it
state = ch.queue_declare(queue="tasks", passive=True)
print("ready messages:", state.method.message_count)
print("consumers:", state.method.consumer_count)
conn.close()The message count here covers only ready messages, not unacknowledged ones. For unacknowledged counts, message rates, and age, use the management HTTP API or export metrics to a monitoring system such as Prometheus.
Alerts that work
Alert on conditions that need a human, and avoid alerts that fire on normal bursts.
- Oldest message age above your latency target, for example five minutes, sustained for several minutes.
- Consumer count of zero on a queue that should have consumers.
- DLQ depth above zero, or growing.
- Depth growing steadily over a window such as 15 minutes.
Exercise
A queue shows 40 ready messages, 0 consumers, and the oldest message is 20 minutes old. Publish rate is steady. Which signals show the problem, and what do you check first?
Show answer
Consumer count of 0 and rising message age show it. Depth is small, so a depth alert would miss it. Check first why the consumers are not connected: crashed processes, a failed deploy, or lost broker connections.
Exercise
Redelivery rate is high, and unacknowledged messages are high. Adding consumers did not reduce depth. What is a likely explanation?
Show answer
Handlers are failing or hanging, so messages are returned to the queue or held without an ack. More consumers repeat the same failure. Read the consumer logs, find the error or the blocked dependency, and fix it.