If you’re running applications in the cloud, you have the ability to use and pay only for resources that are needed to process the current workload.

In many cases, to optimize the usage of compute resources such as Azure Cloud Services (worker roles), Azure WebApps running WebJobs, or Azure Virtual Machine Scale Sets (VMSS), it is beneficial to adjust the number of instances according to demand.

In this article, we’ll show you how to automatically scale Azure compute resources based on queue-based workload using CloudMonix. If you are not familiar with CloudMonix, it is a cloud monitoring service that provides a set of sophisticated automation and monitoring capabilities for the Azure platform.

A popular way to distribute workloads among multiple compute instances is by using Azure Service Bus queues or topics. Coincidentally, observing the number of messages in the queues/topics provides useful signals for auto-scaling of compute resources.

When the number of messages in queues/topics exceeds a certain threshold, it is a good indication that services processing those messages are having trouble keeping up. Consistently, a small number of messages in queues/topics indicates that the number of compute instances can be scaled down to reduce costs.

To auto-scale compute resources such as Azure Cloud Services, WebApps running WebJobs or VM Scale Sets, based on the number of messages in Azure Service Bus (ASB) queues/topics or Azure Storage queues, you need to follow these four simple steps:

1. Run CloudMonix Setup Wizard to start monitoring ASB/Storage and Azure Compute resources

If you aren’t using CloudMonix yet, sign up for a free account, then authorize CloudMonix to view your Azure subscription and allow it to start monitoring of your Azure Service Bus and Compute resources. Learn more here.

2. Define a queue length metric

Once ASB/ASQ resource has been brought into CloudMonix, define a new metric that tracks the length of a particular queue or topic:

  • Open configuration dialog for the ASB/Storage resource
  • In the Metrics tab, add a new metric that will track messages in a given queue or topic.
    • For ASB queue, choose “AzureServiceBusActiveMessageCount” metric category
    • For ASB topic, choose “AzureServiceBusActiveMessageCountBatch” metric category
    • For Storage queue, choose “AzureStorageQueueLength” metric category
  • Specify a name and save

3. Link the queue length metric into the Compute resource

To define auto-scaling rules for compute resource (VMSSs, WebApps, or Cloud Roles), you need to have those resources “see” the newly defined queue length metric:

  • Open configuration dialog for Compute resource
  • In the Metrics tab, add a new metric of type LinkedMetric and point it to the metric previously defined in the queue resource
  • Specify a name and save

4. Define auto-scaling rules
Compute resource will need to have two rules to increase and decrease the number of workers based on the metric value. It is also a good idea to define limits to ensure the number of instances doesn’t exceed expected values:

  • Open configuration dialog for Compute resource
  • In the Scale Adjustments tab define “Scale up” rule that will add two instances when the queue length is higher than some threshold, e.g. 100 (screenshot below)
  • In the Scale Adjustments tab define “Scale down” rule that will remove an instance when the queue length is below some other threshold,e.g. 10 (screenshot below)
  • Scale expressions can be quite sophisticated, taking many metrics and time of day into account
  • In the Scale Ranges define a rule limiting the number of instances to 2 up to 10 (screenshot below)

CloudMonix supports many sophisticated auto-scaling scenarios. Learn more here.

The same approach can be used to auto-scale other Azure resources, such as Cloud Services or VM Scale Sets. CloudMonix can auto-scale resources based on any metric captured anywhere in your Azure environment, or according to a schedule.