0
0
Azurecloud~5 mins

Event Grid vs Service Bus decision in Azure - CLI Comparison

Choose your learning style9 modes available
Introduction
When building cloud apps, you often need to send messages or events between parts of your system. Azure Event Grid and Azure Service Bus are two tools that help with this, but they work differently and fit different needs.
When you want to react quickly to changes like file uploads or database updates, use Event Grid.
When you need to send messages reliably between different parts of your app with guaranteed delivery, use Service Bus.
When you want to connect many different services with simple event notifications, Event Grid is a good choice.
When you need to process messages in order or handle complex workflows, Service Bus is better.
When you want a simple publish-subscribe model for events, Event Grid works well.
Commands
This command creates an Event Grid topic where events can be sent and subscribers can listen for those events.
Terminal
az eventgrid topic create --name example-topic --resource-group example-rg --location eastus
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.EventGrid/topics/example-topic", "location": "eastus", "name": "example-topic", "resourceGroup": "example-rg", "type": "Microsoft.EventGrid/topics" }
--name - Sets the name of the Event Grid topic
--resource-group - Specifies the resource group to create the topic in
--location - Sets the Azure region for the topic
This command creates a Service Bus namespace, which is a container for queues and topics for messaging.
Terminal
az servicebus namespace create --name example-namespace --resource-group example-rg --location eastus
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.ServiceBus/namespaces/example-namespace", "location": "eastus", "name": "example-namespace", "resourceGroup": "example-rg", "type": "Microsoft.ServiceBus/namespaces" }
--name - Sets the name of the Service Bus namespace
--resource-group - Specifies the resource group to create the namespace in
--location - Sets the Azure region for the namespace
This command creates a subscription to the Event Grid topic so that events are sent to the specified endpoint.
Terminal
az eventgrid event-subscription create --name example-subscription --source-resource-id /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.EventGrid/topics/example-topic --endpoint https://myapp.azurewebsites.net/api/events
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.EventGrid/topics/example-topic/eventSubscriptions/example-subscription", "name": "example-subscription", "provisioningState": "Succeeded", "type": "Microsoft.EventGrid/topics/eventSubscriptions" }
--name - Sets the name of the event subscription
--source-resource-id - Specifies the Event Grid topic to subscribe to
--endpoint - Sets the URL where events will be delivered
This command creates a queue inside the Service Bus namespace where messages can be sent and received.
Terminal
az servicebus queue create --name example-queue --namespace-name example-namespace --resource-group example-rg
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.ServiceBus/namespaces/example-namespace/queues/example-queue", "name": "example-queue", "resourceGroup": "example-rg", "type": "Microsoft.ServiceBus/namespaces/queues" }
--name - Sets the name of the queue
--namespace-name - Specifies the Service Bus namespace
--resource-group - Specifies the resource group
Key Concept

If you remember nothing else from this pattern, remember: Event Grid is for lightweight event notifications, and Service Bus is for reliable message delivery with advanced features.

Common Mistakes
Using Event Grid when message order and guaranteed delivery are required
Event Grid does not guarantee message order or delivery, which can cause lost or out-of-order messages
Use Service Bus queues or topics when you need ordered and reliable message processing
Using Service Bus for simple event notifications without needing advanced features
Service Bus adds complexity and cost when simple event routing would suffice
Use Event Grid for simple event-driven scenarios with many subscribers
Summary
Create an Event Grid topic to send and receive lightweight events.
Create a Service Bus namespace and queue for reliable message delivery.
Subscribe to Event Grid topics to react to events via endpoints.
Choose Event Grid for simple event notifications and Service Bus for guaranteed message handling.