Event Grid for event-driven architecture in Azure - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using Event Grid, it's important to understand how the number of events affects processing time.
We want to know how the system handles more events as they come in.
Analyze the time complexity of sending multiple events to Event Grid and processing them.
// Create Event Grid client
var client = new EventGridPublisherClient(endpoint, credential);
// Prepare a list of events
var events = new List<EventGridEvent>();
for (int i = 0; i < n; i++) {
events.Add(new EventGridEvent("subject", "eventType", "1.0", BinaryData.FromObject(new { Message = "Hello" })));
}
// Send events in a batch
await client.SendEventsAsync(events);
This code sends a batch of n events to Event Grid for delivery to subscribers.
Look at what happens repeatedly when sending events.
- Primary operation: Sending each event in the batch to Event Grid.
- How many times: Once per event, but grouped in a batch call.
As the number of events increases, the total work grows roughly in proportion.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 1 batch call sending 10 events |
| 100 | 1 batch call sending 100 events |
| 1000 | 1 batch call sending 1000 events |
Pattern observation: The number of events grows, but sending is done in one batch call, so API calls stay constant while processing work grows with events.
Time Complexity: O(n)
This means the time to process events grows linearly with the number of events sent.
[X] Wrong: "Sending events in a batch means time stays the same no matter how many events."
[OK] Correct: Even though the API call is one, Event Grid still processes each event, so total work grows with event count.
Understanding how event volume affects processing helps you design scalable systems and explain trade-offs clearly.
"What if we sent each event with a separate API call instead of batching? How would the time complexity change?"
Practice
Solution
Step 1: Understand Event Grid's role
Event Grid is designed to route events from sources to handlers quickly and efficiently.Step 2: Compare options with Event Grid's function
Only To connect event sources to event handlers for instant reactions describes connecting event sources to handlers for instant reactions, which matches Event Grid's purpose.Final Answer:
To connect event sources to event handlers for instant reactions -> Option BQuick Check:
Event routing = Connect sources to handlers [OK]
- Confusing Event Grid with data storage services
- Thinking Event Grid manages virtual machines
- Mixing Event Grid with identity management
mySub for a topic myTopic?Solution
Step 1: Identify correct CLI syntax for event subscription creation
The command requires the full resource ID for the source topic, not just the name.Step 2: Analyze options for correct parameters
az eventgrid event-subscription create --name mySub --source-resource-id /subscriptions/{subId}/resourceGroups/{rg}/providers/Microsoft.EventGrid/topics/myTopic uses the full resource ID format, which is required. az eventgrid event-subscription create --name mySub --source-resource-id myTopic uses only the topic name, which is invalid. Options C and D use incorrect commands or parameters.Final Answer:
az eventgrid event-subscription create --name mySub --source-resource-id /subscriptions/{subId}/resourceGroups/{rg}/providers/Microsoft.EventGrid/topics/myTopic -> Option AQuick Check:
Full resource ID needed for source in CLI [OK]
- Using only topic name instead of full resource ID
- Confusing topic creation with subscription creation
- Using wrong CLI commands like 'subscription create'
az eventgrid event-subscription create --name sub1 --source-resource-id /subscriptions/123/resourceGroups/rg1/providers/Microsoft.EventGrid/topics/topic1 --endpoint https://myapp.com/api/events
What happens after running this command?
Solution
Step 1: Understand the command purpose
The command creates an event subscription named 'sub1' for the existing topic 'topic1'.Step 2: Analyze the effect of parameters
The endpoint URL is where events will be delivered. The command does not create a topic or register an endpoint as a source.Final Answer:
An event subscription named 'sub1' is created to send events from 'topic1' to the specified endpoint -> Option CQuick Check:
Event subscription sends events to endpoint [OK]
- Thinking the command creates a new topic
- Assuming endpoint is an event source
- Believing resource group is mandatory in this command
az eventgrid event-subscription create --name sub2 --source-resource-id myTopic --endpoint https://myapp.com/api/events
But it fails with an error. What is the most likely cause?
Solution
Step 1: Check the source-resource-id parameter format
The source-resource-id must be the full Azure resource ID, not just the topic name.Step 2: Validate other parameters
The endpoint can be any valid HTTPS URL. Subscription names can include numbers. Resource group is not required here if full resource ID is used.Final Answer:
The source resource ID must be a full Azure resource ID, not just the topic name -> Option AQuick Check:
Full resource ID needed for source-resource-id [OK]
- Using only topic name instead of full resource ID
- Thinking endpoint must be Azure URL
- Believing subscription names cannot have numbers
Solution
Step 1: Identify event source and event type
The Storage Account emits blob created events when new blobs upload.Step 2: Choose subscription and endpoint
Creating an Event Grid subscription for blob created events with your app's webhook endpoint enables instant reaction.Step 3: Evaluate other options
Polling or timer triggers cause delays. Subscribing to blob deleted events does not detect new uploads.Final Answer:
Create an Event Grid subscription on the Storage Account's blob created event, with your app's webhook as endpoint -> Option DQuick Check:
Blob created event subscription = instant reaction [OK]
- Using polling instead of event-driven subscription
- Subscribing to wrong event type like blob deleted
- Relying on timer triggers causing delays
