Event Grid subscriptions and filters in Azure - Time & Space Complexity
When using Event Grid subscriptions with filters, it's important to understand how the number of events and filters affects processing time.
We want to know how the system's work grows as more events and filters are involved.
Analyze the time complexity of filtering events for multiple subscriptions.
// Create multiple Event Grid subscriptions with filters
for (int i = 0; i < subscriptionCount; i++) {
var filter = new EventSubscriptionFilter() {
SubjectBeginsWith = $"device/{i}/",
IsSubjectCaseSensitive = false
};
await eventGridClient.EventSubscriptions.CreateOrUpdateAsync(
resourceGroupName,
topicName,
$"sub{i}",
new EventSubscription() { Filter = filter, Destination = destination }
);
}
// Events are published to the topic and filtered per subscription
This code creates many subscriptions, each with a filter that matches events starting with a specific prefix.
Look at what repeats as input grows:
- Primary operation: Filtering each incoming event against all subscription filters.
- How many times: For each event, the system checks all subscriptions' filters to decide delivery.
As the number of subscriptions grows, each event must be checked against more filters.
| Input Size (subscriptions) | Approx. Filter Checks per Event |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of filter checks grows directly with the number of subscriptions.
Time Complexity: O(n)
This means the work to filter events grows linearly with the number of subscriptions.
[X] Wrong: "Adding more subscriptions won't affect event processing time much."
[OK] Correct: Each event must be checked against every subscription's filter, so more subscriptions mean more checks and longer processing.
Understanding how filtering scales helps you design efficient event-driven systems and shows you can think about system behavior as it grows.
"What if filters were combined or shared among subscriptions? How would that change the time complexity?"