0
0
Azurecloud~5 mins

Message ordering and sessions in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes messages sent to a queue or topic need to be processed in the exact order they were sent. Azure Service Bus sessions help keep messages grouped and processed in order, like keeping a conversation thread organized.
When you have related messages that must be processed in the order they were sent, like steps in a workflow.
When multiple users send messages and you want to keep each user's messages separate and ordered.
When you want to ensure that only one receiver processes messages from the same group at a time.
When you want to maintain state or context across multiple messages in a session.
When you want to avoid message processing conflicts in distributed systems.
Config File - main.tf
main.tf
provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "East US"
}

resource "azurerm_servicebus_namespace" "example" {
  name                = "examplesbnamespace"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  sku                 = "Standard"
}

resource "azurerm_servicebus_queue" "example" {
  name                = "examplequeue"
  resource_group_name = azurerm_resource_group.example.name
  namespace_name      = azurerm_servicebus_namespace.example.name
  enable_session      = true
  max_size_in_megabytes = 1024
  requires_duplicate_detection = false
}

This Terraform file creates an Azure resource group, a Service Bus namespace, and a Service Bus queue with sessions enabled.

  • enable_session = true: This enables message sessions to keep messages ordered and grouped.
  • The queue is created in the Standard SKU namespace which supports sessions.
  • Other settings like max size and duplicate detection are set to common defaults.
Commands
This command initializes Terraform, downloading the Azure provider plugin and preparing the environment to create resources.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/azurerm... - Installing hashicorp/azurerm v3.64.0... - Installed hashicorp/azurerm v3.64.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command creates the Azure resources defined in the Terraform file, including the Service Bus queue with sessions enabled.
Terminal
terraform apply -auto-approve
Expected OutputExpected
azurerm_resource_group.example: Creating... azurerm_resource_group.example: Creation complete after 2s [id=/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources] azurerm_servicebus_namespace.example: Creating... azurerm_servicebus_namespace.example: Creation complete after 30s [id=/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources/providers/Microsoft.ServiceBus/namespaces/examplesbnamespace] azurerm_servicebus_queue.example: Creating... azurerm_servicebus_queue.example: Creation complete after 10s [id=/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources/providers/Microsoft.ServiceBus/namespaces/examplesbnamespace/queues/examplequeue] Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approves the apply step without prompting for confirmation
This Azure CLI command verifies the queue was created and shows its properties, including that sessions are enabled.
Terminal
az servicebus queue show --resource-group example-resources --namespace-name examplesbnamespace --name examplequeue
Expected OutputExpected
{ "autoDeleteOnIdle": "P10675199DT2H48M5.4775807S", "deadLetteringOnMessageExpiration": false, "defaultMessageTimeToLive": "P14D", "duplicateDetectionHistoryTimeWindow": "PT10M", "enableBatchedOperations": true, "enablePartitioning": false, "enableSession": true, "maxSizeInMegabytes": 1024, "name": "examplequeue", "requiresDuplicateDetection": false }
--resource-group - Specifies the Azure resource group name
--namespace-name - Specifies the Service Bus namespace name
--name - Specifies the queue name
Key Concept

If you remember nothing else from this pattern, remember: enabling sessions on a Service Bus queue groups related messages so they are processed in order by a single receiver.

Common Mistakes
Not enabling sessions on the queue but expecting ordered processing.
Without sessions enabled, Azure Service Bus does not guarantee message order across multiple receivers.
Set enable_session to true when creating the queue to enable ordered message processing.
Trying to process messages from multiple sessions at the same time with one receiver.
A single receiver can only process one session at a time, so processing multiple sessions concurrently requires multiple receivers.
Use multiple receivers or session-aware receivers to handle multiple sessions concurrently.
Summary
Create a Service Bus queue with sessions enabled to keep messages ordered and grouped.
Use Terraform to define and deploy the queue with enable_session set to true.
Verify the queue properties with Azure CLI to confirm sessions are enabled.