Azure Service Bus
Azure Service Bus is a fully managed enterprise message broker. It sits between applications and lets them communicate asynchronously: a sender puts a message on the broker and moves on, and a receiver picks it up whenever it's ready. This decouples services in time (they don't need to be online together), in load (a burst of messages is buffered), and in technology (any client that speaks AMQP, or uses the SDKs, can participate).
Core concepts
- Namespace: the container for your messaging entities, with its own endpoint, pricing tier, and network and security settings.
- Queue: point-to-point messaging. Many senders can send, but each message is delivered to exactly one receiver (competing consumers share the load).
- Topic and subscriptions: publish/subscribe. A sender publishes to a topic, and each subscription gets its own copy of every matching message. Each subscription behaves like a virtual queue.
- Rules and filters: on a subscription, you can filter messages by properties (e.g.,
region = 'EU') so each subscriber only receives what it cares about. - Message: a body plus system properties (MessageId, SessionId, TimeToLive, etc.) and custom application properties.
How it works
- Send: the producer sends a message to a queue or topic. The broker stores it durably (replicated storage) before acknowledging.
- Store: the message waits until a consumer retrieves it or its time-to-live expires.
- Receive: the consumer receives the message using one of two modes:
- Peek-lock (default): the message is locked, not deleted. The consumer processes it, then completes it (removed). If processing fails, it can abandon it (becomes available again) or dead-letter it. If the lock expires without a response, the message returns to the queue.
- Receive-and-delete: the message is removed immediately on receipt. Faster, but it's lost if the consumer crashes mid-processing.
- Retry and dead-letter: each message has a delivery count. Once it exceeds
MaxDeliveryCount(default 10), it moves to the dead-letter queue (DLQ), a sub-queue where you can inspect and reprocess poison messages.
Key features
- Delivery guarantees: at-least-once by default, so consumers should be idempotent. Duplicate detection can discard resends within a time window.
- Sessions: group related messages with a SessionId to get FIFO ordering within the group and exclusive processing by one consumer at a time.
- Scheduled messages: enqueue now, deliver at a future time.
- Transactions: group operations (e.g., complete one message and send another) atomically.
- Auto-forwarding: chain queues or subscriptions together.
- Security: Microsoft Entra ID with RBAC (preferred over shared access keys), private endpoints, and customer-managed keys.
- Resilience: availability zones, plus Geo-Replication or Geo-Disaster Recovery on the Premium tier.
Tiers
- Standard: pay-as-you-go, shared infrastructure, messages up to 256 KB.
- Premium: dedicated resources (messaging units), predictable performance, VNet and private endpoint support, and larger messages (up to 100 MB).
Quick C# example
await using var client = new ServiceBusClient(
"mynamespace.servicebus.windows.net", new DefaultAzureCredential());
// Send
ServiceBusSender sender = client.CreateSender("orders");
await sender.SendMessageAsync(new ServiceBusMessage("Order #1234 placed"));
// Receive
ServiceBusReceiver receiver = client.CreateReceiver("orders");
ServiceBusReceivedMessage msg = await receiver.ReceiveMessageAsync();
Console.WriteLine(msg.Body.ToString());
await receiver.CompleteMessageAsync(msg);
For real workloads, ServiceBusProcessor handles concurrency, lock renewal, and error handling for you, and Azure Functions has a Service Bus trigger that does the same with almost no code.
When to use it
- Order processing, payments, and other workflows where messages must not be lost
- Decoupling microservices and smoothing out traffic spikes (load leveling)
- Fan-out of events to multiple downstream systems via topics
- Ordered processing per customer or entity using sessions
Service Bus vs. related services
| Service Bus | Storage Queues | Event Grid | Event Hubs | |
|---|---|---|---|---|
| Model | Enterprise broker (queues, topics) | Simple queue | Event routing (reactive) | High-throughput event streaming |
| Ordering, sessions, transactions | Yes | No | No | Ordering per partition |
| Best for | Business messages and commands | Basic, cheap queuing | "Something happened" notifications | Telemetry and log ingestion |
Rule of thumb: use Service Bus when a message represents work that must be processed reliably, Event Grid for lightweight notifications, and Event Hubs for massive streams of telemetry.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.