What is an Azure Service Bus?

Azure Service Bus is a fully managed enterprise message broker that enables reliable communication between decoupled applications and services, even when they aren't online at the same time.

Core Messaging Concepts

  • Queues — point-to-point messaging where each message is consumed by a single receiver
  • Topics and Subscriptions — publish-subscribe messaging where multiple receivers get a copy of each message
  • Sessions — enable ordered, related message processing
  • Dead-letter queues — hold messages that couldn't be delivered or processed successfully

Why Use Service Bus

  • Decouples producers and consumers so services can scale independently
  • Guarantees at-least-once (and optionally ordered) message delivery
  • Supports transactions across multiple messaging operations
  • Integrates with Azure Functions, Logic Apps, and most Azure compute services

Common Use Cases

  • Order processing systems that need reliable, asynchronous workflows
  • Load leveling between a fast producer and a slower consumer
  • Broadcasting events to multiple downstream systems via topics
Choose Service Bus over Storage Queues when you need advanced features like sessions, transactions, or publish-subscribe patterns; use Storage Queues for simpler, high-throughput scenarios.

Ready to master Microsoft Azure?

Join Microsoft Azure Training Course at Uncodemy and learn with hands-on projects and mentor support.

Explore the Course

How a Message Travels Through Service Bus

  1. A sender connects to a namespace and sends a message to a queue or topic.
  2. Service Bus stores the message durably until a receiver is ready.
  3. A receiver fetches the message. In peek-lock mode, the message is locked and hidden from other receivers for a period of time.
  4. If processing succeeds, the receiver calls complete and the message is removed.
  5. If processing fails, the receiver calls abandon, or the lock expires, and the message becomes available again.
  6. After too many failed delivery attempts, the message moves to the dead-letter queue for investigation.

The other mode, receive-and-delete, removes the message as soon as it is read. It is simpler but a crash during processing loses that message, so peek-lock is the safer default.

Sending and Receiving in Python

from azure.identity import DefaultAzureCredential
from azure.servicebus import ServiceBusClient, ServiceBusMessage

NAMESPACE = "my-namespace.servicebus.windows.net"
QUEUE = "orders"

with ServiceBusClient(NAMESPACE, DefaultAzureCredential()) as client:
    with client.get_queue_sender(QUEUE) as sender:
        sender.send_messages(ServiceBusMessage('{"orderId": 501}', message_id="501"))

    with client.get_queue_receiver(QUEUE, max_wait_time=5) as receiver:
        for msg in receiver:
            try:
                print("Processing", str(msg))
                receiver.complete_message(msg)
            except Exception:
                receiver.abandon_message(msg)

Using DefaultAzureCredential lets the app sign in with a managed identity in Azure, so no connection string with a key needs to be stored in code. Grant the identity the "Azure Service Bus Data Sender" or "Data Receiver" role on the queue or namespace.

Designing Reliable Consumers

Because delivery is at least once, the same message can arrive twice, for example after a lock expires mid-processing. Consumers should be idempotent: processing order 501 twice must not charge the customer twice. Store processed message IDs or use database constraints to ignore repeats.

  • Turn on duplicate detection on the queue and set a stable message_id so repeated sends within the detection window are dropped.
  • Watch the dead-letter queue; set an alert when it grows, and build a small tool to inspect and resubmit messages.
  • Use sessions when messages for the same customer or order must be handled in sequence.
  • Use scheduled messages for delayed work, such as a reminder sent later.

Service Bus, Event Grid or Event Hubs?

ServiceBest fit
Service BusBusiness commands and transactions that must not be lost, such as orders or payments
Event GridLightweight notifications that something happened, such as a blob being created
Event HubsLarge streams of telemetry or logs read by analytics pipelines

Check the tier before designing: topics and subscriptions are not available in the Basic tier, and features such as private endpoints depend on the tier you choose. Confirm current tier details in the Azure documentation.