Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 57 ยท Messaging and Integration

Event Grid and Event Hubs

Understand Azure Event Grid and Event Hubs: events versus messages, routing filters, partitions, consumer groups, throughput units and Capture.

Azure has several messaging services, and beginners often confuse them because they all move data between programs. The key is to ask what is being sent. A message is a unit of work the sender wants someone to do ("charge this card"); the sender cares that it is processed, and the broker keeps it until it is. An event is a statement that something happened ("the file invoice.pdf was created"); the sender does not care who reacts, or whether anyone does. Events come in two flavors: individual discrete events that trigger reactions, and continuous streams of events that are analyzed in bulk.

This lesson covers the two services built for events: Event Grid for discrete events and routing, and Event Hubs for high-volume streams. (Messages that represent work are the job of Service Bus.) The real services need an Azure account, so commands and SDK code are read-only. The runnable stand-ins are Node.js programs that model Event Grid's filtering and retries and Event Hubs' partitions, consumer groups and checkpoints.

Choosing the right service

Event Grid Event Hubs Service Bus
Core idea "Something happened" notifications, pushed to handlers A very high-volume, replayable stream Reliable work messages with settlement
Typical volume Many small events Millions of events per second Moderate, business-critical messages
Delivery Push to subscribers, retried Consumers pull and track their position Consumers pull and settle each message
Replay No (the event is delivered, then gone) Yes, within the retention period No (a completed message is deleted)
Examples Blob created, resource deployed, custom app events Telemetry, clickstreams, logs Orders, payments, commands

A single solution often uses several: a blob is uploaded (Event Grid event), a function receives it and puts a work item in a queue (Service Bus), and device telemetry streams through Event Hubs into analytics (see Data and Analytics Services).

Event Grid: routing events

Event Grid has four parts:

  • Event sources publish events. Many Azure services are sources out of the box, through system topics: for example a storage account emits Microsoft.Storage.BlobCreated. Your own applications publish to custom topics.
  • A topic is the endpoint events are sent to.
  • An event subscription says which events a handler wants, by filter, and where to send them.
  • Event handlers receive the events: Azure Functions, Logic Apps, Service Bus and Storage queues, Event Hubs, or any HTTPS webhook.
flowchart LR
    S["Storage account<br/>(source)"] --> T["System topic"]
    A["Your app<br/>(source)"] --> C["Custom topic"]
    T --> E1["Subscription:<br/>BlobCreated, subject ends .jpg"]
    T --> E2["Subscription:<br/>all events"]
    C --> E3["Subscription:<br/>OrderPlaced"]
    E1 --> F["Function: make thumbnail"]
    E2 --> Q["Queue: audit"]
    E3 --> W["Webhook"]

Sources publish to topics; each subscription filters and forwards a copy to its handler.

Events use a small envelope. A cut-down CloudEvents-style example (field values invented; check the current schema options):

JSON
{
  "specversion": "1.0",
  "id": "9f2c1e6a-0001",
  "type": "Microsoft.Storage.BlobCreated",
  "source": "/subscriptions/<sub>/resourceGroups/media/providers/Microsoft.Storage/storageAccounts/mediaacct",
  "subject": "/blobServices/default/containers/uploads/blobs/cat.jpg",
  "time": "2026-03-01T10:15:00Z",
  "data": { "contentType": "image/jpeg", "contentLength": 48213 }
}

An event is small and says what happened and where; it normally does not carry the whole file. The handler fetches the blob itself.

Filters decide which events a subscription receives: by event type, by subject (begins with or ends with, such as /blobs/ and .jpg), and by advanced filters on fields in the data. Creating a subscription with the CLI (read-only; parameters shown are the common ones, check --help):

Terminal
az eventgrid system-topic event-subscription create \
  --name thumbnails \
  --resource-group media \
  --system-topic-name mediaacct-topic \
  --endpoint-type azurefunction \
  --endpoint <function-resource-id> \
  --included-event-types Microsoft.Storage.BlobCreated \
  --subject-ends-with .jpg

Delivery, retries and dead-lettering

Event Grid pushes events and expects a success response from the handler. If delivery fails it retries with exponential backoff, by default for up to 24 hours and a limited number of attempts, then drops the event unless you configured a dead-letter destination (a blob container) to keep undelivered events. As with any at-least-once system, handlers must tolerate duplicates and out-of-order events.

The stand-in below routes events through subscriptions with filters, using a fake handler that fails twice for one subscriber to show retries with a growing delay. The delays are invented; Event Grid's real schedule differs.

Node.js Event routing and retry

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Event Grid also offers namespaces with MQTT support and pull-style delivery for newer scenarios; the push model above is the classic one.

Event Hubs: streaming at scale

An Event Hubs namespace contains one or more event hubs. An event hub is a distributed, append-only log: producers append events, and consumers read from the log at their own position without removing anything. Events stay for the retention period (from one day up to a week on Standard, longer on higher tiers) and can be read again, which makes replay and late-joining consumers possible.

Partitions

An event hub is divided into partitions, each an ordered sequence of events. Partitions provide parallelism: more partitions let more consumers read at once. Rules to remember:

  • Order is guaranteed only inside a partition, not across the hub.
  • Producers can pick a partition key; events with the same key hash to the same partition, so all events of one device or user stay in order.
  • Without a key, events are spread across partitions for balance.
  • The partition count is chosen at creation; on the lower tiers you cannot change it afterwards, so plan for peak consumer parallelism, not today's load.

Consumer groups and checkpoints

A consumer group is an independent view of the hub. Each group tracks its own position, so a real-time dashboard and a nightly archiver can read the same stream at different speeds without interfering. Within a group, each partition is read by one consumer at a time, and consumers record checkpoints (the offset of the last processed event, usually stored in Blob Storage) so a restarted consumer resumes rather than starting over. Processing is at-least-once: after a crash you may reprocess events since the last checkpoint.

Throughput

On the Standard tier you buy throughput units (TUs): each allows roughly 1 MB per second or 1,000 events per second of ingress and twice that of egress; autoinflate can raise the number automatically up to a limit. Premium uses processing units and dedicated clusters use capacity units. If you exceed capacity, senders are throttled, so watch the throttled-requests metric. Event Hubs also exposes a Kafka-compatible endpoint, so existing Kafka producers and consumers can often connect by changing configuration.

Capture

Capture automatically writes the stream to Blob Storage or Data Lake Storage as Avro files, in time or size windows you configure, with no code. It is the easiest way to keep a permanent raw archive (the bronze zone from the data lesson) next to your real-time processing.

Here is how producing and consuming look in the Node.js SDK (read-only; needs a namespace, @azure/event-hubs, and an Entra role such as Azure Event Hubs Data Sender):

JavaScript
import { EventHubProducerClient } from "@azure/event-hubs";
import { DefaultAzureCredential } from "@azure/identity";

const producer = new EventHubProducerClient(
  "mynamespace.servicebus.windows.net",
  "telemetry",
  new DefaultAzureCredential()
);

// All events with the same partitionKey land in the same partition, in order.
const batch = await producer.createBatch({ partitionKey: "device-17" });
batch.tryAdd({ body: { temp: 21.4, at: "2026-03-01T10:15:00Z" } });
batch.tryAdd({ body: { temp: 21.6, at: "2026-03-01T10:15:05Z" } });
await producer.sendBatch(batch);
await producer.close();

A partitioned log with consumer groups: a stand-in

This stand-in builds a three-partition log. A hash of the partition key picks the partition, each consumer group keeps its own per-partition checkpoint, and reading never deletes data. It is a model of the ideas, not the SDK.

Node.js Partitions, consumer groups and checkpoints

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Compare the two groups: each reads from its own position and neither removes events. Within one partition the offsets show a strict order (device-1's readings stay in order); across partitions there is no global order.

Common mistakes

  • Using Event Hubs as a work queue. It has no per-message settle, no dead-letter queue and no per-message locks; use Service Bus for commands.
  • Putting payloads in Event Grid events. Send a reference and let the handler fetch the data.
  • Too few partitions, which caps consumer parallelism and cannot be fixed later on lower tiers.
  • A hot partition key (one device sending most of the data) that overloads one partition.
  • Forgetting checkpoints, so a restart reprocesses everything.
  • No dead-letter destination in Event Grid, so persistent handler failures lose events silently.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A blob upload should trigger a thumbnail function. Which service is the natural first step?

    Blob Storage is an Event Grid source, and a filtered subscription can invoke a function whenever a matching blob is created.
  2. 2. In Event Hubs, where is event ordering guaranteed?

    Each partition is an ordered log. Using the same partition key for related events keeps them in one partition and therefore in order.
  3. 3. What does a consumer group give you?

    Each group tracks its own checkpoints, so several applications can read the same stream at different speeds.
  4. 4. Event Grid cannot deliver an event to a webhook that keeps returning errors. What happens by default?

    Event Grid retries with exponential backoff for a limited time. A dead-letter container keeps undelivered events for inspection.
  5. 5. What does Event Hubs Capture do?

    Capture archives the stream as Avro files in time or size windows, with no code.

Summary

  • Messages ask for work to be done; events report that something happened. Event Grid routes discrete events, Event Hubs ingests high-volume streams, Service Bus carries work messages.
  • Event Grid: sources publish to topics, subscriptions filter by type, subject or data and push to handlers, with retries and optional dead-lettering.
  • Event Hubs is an append-only log split into partitions; order exists only inside a partition, and a partition key keeps related events together.
  • Consumer groups give independent views, and checkpoints let consumers resume; reading does not delete events.
  • Throughput is bought in throughput units (Standard), processing units (Premium) or capacity units (dedicated); Capture archives the stream to storage.

Next: Logic Apps and API Management.