Lesson 11 / 25

Service Bus, Event Grid and Event Hubs

Choose between commands, discrete events and event streams.

Three messaging services, three jobs

Azure Service Bus is an enterprise message broker for commands and business messages that must not be lost: queues (one consumer gets each message) and topics with subscriptions (each subscription gets a copy). It offers peek-lock delivery, dead-letter queues, sessions for ordered processing and scheduled messages. Azure Event Grid routes discrete events ("a blob was created", "a resource group was deleted") from a source to handlers by push, with filtering and retries; it is how you react to things happening. Azure Event Hubs ingests high-volume event streams (telemetry, clickstreams, logs) into partitions that consumers read at their own pace and can replay; it also exposes a Kafka-compatible endpoint. Ask: is this an instruction someone must act on (Service Bus), a notification of a fact (Event Grid), or a firehose to analyse (Event Hubs)?

Sending and receiving with Service Bus

Peek-lock: the message is completed only after successful processing, otherwise it returns to the queue and eventually dead-letters.

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

NS = "sb-shop-dev.servicebus.windows.net"
with ServiceBusClient(NS, DefaultAzureCredential()) as client:
    with client.get_queue_sender("payments") as sender:
        sender.send_messages(ServiceBusMessage('{"orderId": 42}', message_id="order-42"))

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

Courier, notice board and CCTV recorder

Service Bus is a registered courier: a parcel for one recipient with a receipt. Event Grid is a notice board that pings everyone subscribed when a notice goes up. Event Hubs is a CCTV recorder: a continuous stream you can rewind and watch again.

Quick check: IoT devices send millions of telemetry readings per minute that several analytics jobs must read and replay. Which service fits?

  • Service Bus queue
  • Event Hubs
  • Event Grid
  • Azure Files
Answer

Event Hubs — Event Hubs is built for partitioned, high-throughput streams that consumers can read independently and replay.