Event-Driven Architecture with Azure Event Grid

Azure Event Grid is the backbone of event-driven architectures on Azure. It’s a fully managed event routing service that uses a publish-subscribe model. Unlike messaging services (Service Bus, Event Hubs), Event Grid is optimized for reactive programming patterns where you want instant notifications of state changes.

Event Grid Architecture

flowchart LR
    subgraph Publishers["Event Publishers"]
        BLOB[Blob Storage]
        RG[Resource Groups]
        CUSTOM[Custom Topics]
        IOT[IoT Hub]
    end
    
    subgraph EventGrid["Azure Event Grid"]
        TOPIC[Topics]
        FILTER[Filters]
        ROUTE[Routing]
    end
    
    subgraph Handlers["Event Handlers"]
        FUNC[Azure Functions]
        LOGIC[Logic Apps]
        WH[Webhooks]
        QUEUE[Storage Queue]
        HUB[Event Hubs]
    end
    
    BLOB --> TOPIC
    RG --> TOPIC
    CUSTOM --> TOPIC
    IOT --> TOPIC
    TOPIC --> FILTER
    FILTER --> ROUTE
    ROUTE --> FUNC
    ROUTE --> LOGIC
    ROUTE --> WH
    ROUTE --> QUEUE
    ROUTE --> HUB
    
    style EventGrid fill:#FFF3E0,stroke:#E65100,stroke-width:2px
    style FUNC fill:#E8F5E9,stroke:#2E7D32
    style LOGIC fill:#E8F5E9,stroke:#2E7D32

Key Concepts

Topics are endpoints where publishers send events. Azure services have built-in topics (system topics), and you can create custom topics for your applications.

Event Subscriptions route events from topics to handlers. You can filter which events reach each subscription based on event type, subject pattern, or advanced filters on event data.

Event Handlers are endpoints that receive and process events. Functions and Logic Apps are the most common, but any webhook endpoint works.

Creating a Custom Topic

# Create custom topic
az eventgrid topic create \
  --name my-topic \
  --resource-group rg-events \
  --location westeurope

# Get endpoint and key
az eventgrid topic show --name my-topic -g rg-events --query endpoint
az eventgrid topic key list --name my-topic -g rg-events --query key1

Publishing Events

var client = new EventGridPublisherClient(
    new Uri(topicEndpoint),
    new AzureKeyCredential(topicKey));

var events = new List<EventGridEvent>
{
    new EventGridEvent(
        subject: "orders/12345",
        eventType: "Order.Created",
        dataVersion: "1.0",
        data: new { OrderId = 12345, CustomerId = "C001", Total = 99.99 })
};

await client.SendEventsAsync(events);

Handling Events with Azure Functions

[FunctionName("HandleOrderCreated")]
public static async Task Run(
    [EventGridTrigger] EventGridEvent eventGridEvent,
    ILogger log)
{
    var orderData = eventGridEvent.Data.ToObjectFromJson<OrderCreatedEvent>();
    log.LogInformation($"Order {orderData.OrderId} created for {orderData.CustomerId}");
    
    // Process the order...
    await SendConfirmationEmail(orderData);
    await UpdateInventory(orderData);
}

Advanced Filtering

Event subscriptions support advanced filters on the event data payload:

{
  "filter": {
    "advancedFilters": [
      {
        "operatorType": "NumberGreaterThan",
        "key": "data.Total",
        "value": 100
      },
      {
        "operatorType": "StringContains",
        "key": "data.Region",
        "values": ["EU", "UK"]
      }
    ]
  }
}

Key Takeaways

  • Event Grid enables reactive, event-driven architectures with push-based delivery
  • System topics for Azure services, custom topics for your applications
  • Multiple handlers can subscribe to the same events with different filters
  • Near real-time delivery (sub-second) with 24-hour retry for reliability

References


Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.