Azure Service Bus
Modern applications often consist of multiple services that need to communicate with each other. If Service A directly calls Service B, what happens when Service B is temporarily down or overloaded? The entire operation fails. Azure Service Bus is a fully managed enterprise message broker that decouples services — allowing them to communicate reliably even when one side is unavailable.
What is a Message Broker?
A message broker is an intermediary that sits between services. Instead of Service A directly calling Service B, Service A sends a message to the broker and continues its work immediately. Service B picks up the message from the broker whenever it is ready. The two services are completely independent — they don't need to be running at the same time.
Direct Call vs Message Broker
DIRECT CALL (Tightly Coupled):
Order Service ──────────────► Payment Service
Problem: If Payment Service is down → Order Service fails
MESSAGE BROKER (Loosely Coupled):
Order Service ──► Service Bus Queue ──► Payment Service
Benefit: If Payment Service is down → Message waits in queue
Payment Service processes it when it recovers
Order Service succeeded without waiting
Service Bus Components
Namespace
A Service Bus Namespace is the top-level container — similar to a storage account for Service Bus. All queues and topics are created inside a namespace. The namespace provides the endpoint URL for connecting.
Queue
A queue holds messages in a first-in, first-out (FIFO) order. One producer sends messages; one consumer (or a pool of competing consumers) reads and processes them. Each message is processed exactly once — once consumed, it is removed from the queue.
Diagram – Service Bus Queue
Producer Queue Consumer(s)
┌─────────────┐ ┌──────────────────┐ ┌─────────────┐
│ Order │ │ order-queue │ │ Payment │
│ Service │─────────►│ │─────►│ Service │
│ │ message │ [msg1][msg2][msg3│ │ │
└─────────────┘ └──────────────────┘ └─────────────┘
Messages stored safely Processes one
until consumed at a time (FIFO)
Topic and Subscription
A topic is like a queue but supports multiple subscribers. One producer sends a message to a topic; multiple consumers each receive their own copy of the message through their subscriptions. This is a publish/subscribe (pub/sub) pattern.
Diagram – Service Bus Topic
Producer Topic Consumers
┌─────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ Order │ │ new-order-topic │──► │ Payment Service │
│ Service │─────────►│ │──► │ Inventory Svc │
│ │ 1 msg └──────────────────┘──► │ Notification Svc│
└─────────────┘ Message is copied Each gets its own
to each subscription copy of the message
Queue vs Topic
| Feature | Queue | Topic |
|---|---|---|
| Consumers | One consumer (or competing consumers) | Multiple subscribers — each gets the message |
| Pattern | Point-to-point | Publish/Subscribe (pub/sub) |
| Message Copy | One copy consumed once | One copy per subscription |
| Best For | Distributing work across multiple workers (load leveling) | Broadcasting an event to multiple interested services |
| Example | Order placed → Payment Service processes it | Order placed → Payment + Inventory + Email notification all react |
Service Bus Tiers
| Tier | Max Message Size | Features |
|---|---|---|
| Basic | 256 KB | Queues only, no topics, no sessions, no dead-letter |
| Standard | 256 KB | Queues, topics, sessions, dead-letter queue, transactions |
| Premium | 100 MB | All Standard features + dedicated resources, VNet integration, zone redundancy |
Advanced Features
Dead-Letter Queue (DLQ)
Every queue and topic subscription has an associated dead-letter queue. Messages that cannot be processed (due to processing failures, exceeded retry count, or message expiry) are automatically moved to the DLQ. The operations team can inspect, fix, and reprocess dead-letter messages without losing them.
Message Sessions
Sessions guarantee that all messages with the same session ID are processed in order by the same consumer. This is critical for scenarios where order matters — for example, all updates for a specific order must be processed in sequence.
Scheduled Messages
Messages can be scheduled to appear in the queue at a future time. For example, a reminder message can be scheduled to appear in a notification queue 24 hours before a user's appointment.
Message Deferral
A consumer can defer a message — set it aside temporarily — and come back to process it later. Deferred messages stay in the queue and must be retrieved explicitly by their sequence number.
Transactions
Multiple Service Bus operations (send to queue A, complete message from queue B) can be grouped into a single transaction. Either all succeed or all fail — ensuring consistency across multiple queues.
Service Bus vs Azure Storage Queues
| Feature | Azure Service Bus | Azure Storage Queue |
|---|---|---|
| Max message size | 100 MB (Premium) | 64 KB |
| Max queue size | 80 GB | Unlimited (storage account limit) |
| Ordering guarantee | FIFO with sessions | Best effort (no guarantee) |
| Topics/Pub-Sub | Yes | No |
| Dead-letter queue | Built-in | Manual implementation needed |
| Transactions | Yes | No |
| Cost | Higher | Very low |
| Best For | Enterprise messaging, complex workflows | Simple task queuing, large volume low-cost messaging |
Key Takeaways
- Azure Service Bus is a fully managed enterprise message broker that decouples services for reliable asynchronous communication.
- Queues implement point-to-point messaging (one producer, one consumer); Topics implement pub/sub (one producer, many subscribers).
- Dead-letter queues capture messages that fail processing — preventing data loss and enabling investigation.
- Sessions ensure ordered processing of related messages by the same consumer.
- Choose Service Bus over Storage Queues for enterprise scenarios requiring ordering, transactions, topics, and large messages.
