Get message counters
This article shows you different ways of getting message counts for a queue or subscription. Knowing the active message count is useful in determining whether a queue builds up a backlog that requires more resources to process than what has currently been deployed.
Counter | Description |
---|---|
ActiveMessageCount | Number of messages in the queue or subscription that are in the active state and ready for delivery. This includes deferred messages. |
ScheduledMessageCount | Number of messages in the scheduled state. |
DeadLetterMessageCount | Number of messages in the dead-letter queue. |
TransferMessageCount | Number of messages pending transfer into another queue or topic. |
TransferDeadLetterMessageCount | Number of messages that failed to transfer into another queue or topic and have been moved into the transfer dead-letter queue. |
If an application wants to scale resources based on the length of the queue, it should do so with a measured pace. The acquisition of the message counters is an expensive operation inside the message broker, and executing it frequently directly and adversely impacts the entity performance.
Another useful metric to consider for scaling is the time between when the latest message was sent and when it was processed, also known as "critical time". This is helpful for scenarios where a queue may have thousands of messages in it, but the processing is fast enough to keep up, giving a "critical time" of only a couple of seconds, which may be more than enough for something like an email sending endpoint. Third-party libraries like NServiceBus emit this and other useful metrics via OpenTelemetry.
Note
The messages that are sent to a Service Bus topic are forwarded to subscriptions for that topic. So, the active message count on the topic itself is 0, as those messages have been successfully forwarded to the subscription. Get the message count at the subscription and verify that it's greater than 0. Even though you see messages at the subscription, they are actually stored in a storage owned by the topic. If you look at the subscriptions, then they would have non-zero message count, which contribute to the storage used by the topic.
Using Azure portal
Navigate to your namespace, and select the queue. You see message counters on the Overview page for the queue.
Navigate to your namespace, select the topic, and then select the subscription for the topic. You see message counters on the Overview page for the queue.
Using Azure CLI
Use the az servicebus queue show
command to get the message count details for a queue as shown in the following example.
az servicebus queue show --resource-group myresourcegroup \
--namespace-name mynamespace \
--name myqueue \
--query countDetails
Here's a sample output:
ActiveMessageCount DeadLetterMessageCount ScheduledMessageCount TransferMessageCount TransferDeadLetterMessageCount
-------------------- ------------------------ ----------------------- ---------------------- --------------------------------
0 0 0 0 0
Use the az servicebus topic subscription show
command to get the message count details for a subscription as shown in the following example.
az servicebus topic subscription show --resource-group myresourcegroup \
--namespace-name mynamespace \
--topic-name mytopic \
--name mysub \
--query countDetails
Using Azure PowerShell
With PowerShell, you can obtain the message count details for a queue as follows:
$queueObj=Get-AzServiceBusQueue -ResourceGroup myresourcegroup `
-NamespaceName mynamespace `
-QueueName myqueue
$queueObj.CountDetails
Here's the sample output:
ActiveMessageCount : 7
DeadLetterMessageCount : 1
ScheduledMessageCount : 3
TransferMessageCount : 0
TransferDeadLetterMessageCount : 0
you can obtain the message count details for a subscription as follows:
$topicObj= Get-AzServiceBusSubscription -ResourceGroup myresourcegroup `
-NamespaceName mynamespace `
-TopicName mytopic `
-SubscriptionName mysub
$topicObj.CountDetails
The returned MessageCountDetails
object has the following properties: ActiveMessageCount
, DeadLetterMessageCount
, ScheduledMessageCount
, TransferDeadLetterMessageCount
, TransferMessageCount
.
Next steps
Try the samples in the language of your choice to explore Azure Service Bus features.
- Azure Service Bus client library samples for .NET (latest)
- Azure Service Bus client library samples for Java (latest)
- Azure Service Bus client library samples for Python
- Azure Service Bus client library samples for JavaScript
- Azure Service Bus client library samples for TypeScript
Find samples for the older .NET and Java client libraries below:
- Azure Service Bus client library samples for .NET (legacy)
- Azure Service Bus client library samples for Java (legacy)
On 30 September 2026, we'll retire the Azure Service Bus SDK libraries WindowsAzure.ServiceBus, Microsoft.Azure.ServiceBus, and com.microsoft.azure.servicebus, which don't conform to Azure SDK guidelines. We'll also end support of the SBMP protocol, so you'll no longer be able to use this protocol after 30 September 2026. Migrate to the latest Azure SDK libraries, which offer critical security updates and improved capabilities, before that date.
Although the older libraries can still be used beyond 30 September 2026, they'll no longer receive official support and updates from Microsoft. For more information, see the support retirement announcement.