Using Azure Event Grid for your own event-driven cloud solution
    • Dark
      Light
    • PDF

    Using Azure Event Grid for your own event-driven cloud solution

    • Dark
      Light
    • PDF

    Article Summary

    There has been quite the hype on creating microservices and event-driven architectures for the past couple of years. Working in the cloud sure helps a lot in designing & implementing these kinds of solutions.

    With all the services offered in the cloud, it is hard to find the ‘correct’ service for your problem. One of the things I see happen quite often is the usage of Service Bus Topics to send events throughout the entire cloud solution. I cannot blame these teams; Service Bus has been around forever, and most of the time, it’s capable of handling all these events in a timely fashion.

    However, nowadays, we can use a different service for our event-driven solution, Event Grid.

    As you might know, Event Grid has built-in support for events coming from Azure services, like events happening on your storage accounts, resource groups, and even your subscriptions.

    Many people don’t know the possibility of creating your own ‘Custom Topics’ on Event Grid. Depending on the size of your solution, you can suffice with just one Custom topic, or if you need to isolate certain events in large solutions, multiple Custom topics can be created.

    Event handlers can decide which custom topic they want to subscribe to and what the filters of the event(s) should be.

    You can check the event schema on the Azure Event Grid documentation, but the key thing is your data is placed in the data property. The most useful for your event-driven application is probably the subject and eventType. All other fields are standard and used on all other system topics as well.

    These are two fields you will probably create your subscriptions on.

    The dataVersion is very useful also for when an event is changed. By checking the dataVersion field, you will be able to define how the data object can be deserialized (when using a strong typed language).

    When using C#, it is possible to use the Microsoft.Azure.EventGrid package to send events to Event Grid. The code you will be using will look like the following sample.

    var eventGridEventCollection = new List<EventGridEvent>();
    foreach (var @event in eventCollection)
    {
        var eventGridEvent = new EventGridEvent(
            id: Guid.NewGuid().ToString("N"),
            subject: @event.Subject,
            dataVersion: @event.DataVersion,
            eventType: @event.EventType,
            data: @event.Data,
            eventTime: DateTime.UtcNow
        );
        eventGridEventCollection.Add(eventGridEvent);
    }
    
    
    if (eventGridEventCollection.Any())
    {
        using (var eventGridClient = new EventGridClient(topicCredentials))
        {
            await eventGridClient.PublishEventsAsync(
                topicHostname,
                eventGridEventCollection,
                cancellationToken);
        }
    }
    

    You might notice, a topic needs to be specified over here, along with the appropriate credentials. To get these details, you need to create your own Custom Topic. These can be found on the Event Grid Topics page in the Azure Portal.
    image001

    Clicking on one of these custom topics will show you the topic endpoint you’ll be needing in the code above (topicHostname).
    image003

    The ‘Access keys’ blade contains the necessary keys, two in total, to publish events to this specific custom topic.

    If you are using ARM templates, it is also possible to retrieve these values during deployment to be added to your service's configuration settings.

    {
        "endpoint": "[reference(resourceId(variables('eventgridTopic').resourceGroup,'Microsoft.EventGrid/topics', variables('eventgridTopic').instanceName), '2017-06-15-preview').endpoint]",
        "key": "[listkeys(resourceId(variables('eventgridTopic').resourceGroup,'Microsoft.EventGrid/topics', variables('eventgridTopic').instanceName), '2017-06-15-preview').key1]"
    }
    

    The pricing of Azure Event Grid is also something that makes the service quite interesting. According to the Azure pricing calculator, you are getting 100,000 operations for free (publish & delivery combined) and only pay about $0.60 per 1,000,000 operations, which makes this service relatively cost-efficient.

    Service Bus Topics, which need the Standard tier, cost at least ~$10, which is more expensive. Still, paying about $10 for such a service is peanuts.

    In short, if you don’t need all the enterprise features which are offered in Service Bus Topics, please use Azure Event Grid as your event service as it’s much more lightweight and is the recommended service for handling events.

    Event-grid-2.png


    Was this article helpful?