Road To AZ-204 - Developing Event Based Solutions

Intro

 
This article's intention is to explain the main skills measured in this sub-topic of the AZ-204 Certification. Azure Event Grid, Azure Notification Hub, and Azure Event hub are the main components that will have their fundamentals explained here alongside a practical example.
 
This certification is very extensive and this article approaches only the main topics, make sure you know those components in depth before taking the exam. Another great tip is doing exam simulators before the official exam in order to validate your knowledge.
 

What is the Certification AZ-204 - Developing Solutions for Microsoft Azure?

 
The AZ-204 - Developing Solutions for Microsoft Azure certification measures designing, building, testing, and maintaining skills of an application and/or service in the Microsoft Azure Cloud environment. It approaches, among others, those components,
  • Azure Virtual Machines;
  • Docker;
  • Azure Containers;
  • Service Web App;
  • Azure Functions;
  • Cosmos DB;
  • Azure Storage;
  • Azure AD;
  • Azure Key Vault;
  • Azure Managed Identities;
  • Azure Redis Cache;
  • Azure Logic App;
  • Azure Event Grid;
  • Azure Event Hub;
  • Azure Notification Hub;
  • Azure Service Bus;
  • Azure Queue Storage.
Target Audience
 
Any IT professional willing to improve his knowledge in Microsoft Azure is encouraged to take this certification, it is a great way to measure your skills within trending technologies. But, some groups of professionals are more keen to take maximum advantage of it,
  • Azure Developers, with at least 1 year of experience with Microsoft Azure;
  • Experienced Software Developers, looking for an Architect position in a hybrid environment;
  • Software Developers, working to move applications to the cloud environment.
Skills Measured
 
According to today's date, the skills that are measured in the exam are split as follows:

Benefits of Getting Certified

 
The main benefit here is having a worldwide recognized certification that proves that you have knowledge of this topic. Among intrinsic and extrinsic benefits, we have,
  • Higher growth potential, as certifications are a big plus;
  • Discounts and deals in Microsoft products and partners, like PluralSight and UpWork;
  • MCP Newsletters, with trending technologies;
  • Higher exposure on LinkedIn, as recruiters usually search for specific certifications;
  • Higher salary, you will be more valuable to your company;
  • Unique happiness when getting the result and you were approved, knowing that all your efforts were worth it;

Main skills Measured by this Topic

 
What is Azure Event Grid?
 
Azure Event Grid is an Azure service to manage routing events from any source to any destination. It provides a simple and strong customizable event delivery process where you can manage at a minimum level which type of events are going to be received as well as manage at a minimum level which subscribers are going to receive those events.
It works perfectly with event-based architectures where you can subscribe to events from Azure resources and publish those events to an event handler or webhook. With Custom Topics you can also create your custom events to be published in your Event Grid.
 
Azure Event Grid is grouped by Event Sources and Event Handlers. Event sources are the services that are sending information to the Event Grid while Event Handlers are the services that are going to be receiving. Nowadays Event Grid supports the following Event Sources and Event Handles,
  • Event Sources 
    • Azure App Configuration
    • Azure Blob Storage
    • Azure Communication Services
    • Azure Container Registry
    • Azure Event Hubs
    • Azure IoT Hub
    • Azure Key Vault
    • Azure Machine Learning
    • Azure Maps
    • Azure Media Services
    • Azure resource groups
    • Azure Service Bus
    • Azure SignalR
    • Azure subscriptions
  • Event Handlers
    • Azure Automation
    • Azure Functions
    • Event Hubs
    • Relay Hybrid Connections
    • Logic Apps
    • Power Automate 
    • Service Bus
    • Queue Storage
    • WebHooks
Azure Event Grid supports events fan-out with 24-hours retry reliability to make sure events are delivered, it is a serverless product to support dynamic scalability with a low cost where you only pay for the events used. In order for a better understanding of Event Grid particularities, some key concepts also have to be better explained as follows,
  • Domain, groups Event Grid topics related to the same application in order to better management;
  • Topic, the endpoint where events are sent
    • System Topics, built-in topics provided by Azure Services;
    • Custom Topics, applications and third-party topics;
    • Partner Events, provides a way for third parties SAAS to publish events;
  • Events, the event that happened in the service;
  • Publishers, the service that published the event;
  • Event Sources, where the event took place;
  • Event Subscriptions, the mechanism the routes the events;
  • Event Subscriptions Expiration
  • Event Handlers, the app or service receiving the events;
  • Event Delivery, batch or single events;
  • Batching, group of events sent in the same request;

What is Azure Event Hub?

 
Azure Event Hub is an event ingestor, being able to consume millions of events from anywhere and it is also able to process those events, in real-time or micro-batching, with the help of the auto-inflate feature, which scales automatically the number of throughput to attend your dynamic needs. It provides an easy way to connect with your Apache Kafka applications and clients.   
 
Azure Event Hub helps you build your big data pipeline in order to analyze logging, anomalies, user, and devices telemetry where you only pay for what you use. In order to a better understanding of Azure Event Hub, the following terminologies have to be better understood,
  • Namespace, an endpoint to receive your events and distribute to event hubs;
  • Event Hub, where your events are going to be delivered; 
  • Event Hub Cluster, dedicated Event Hub with a 99,99% SLA;
  • Event Hub Capture, enable automatically capture streaming events and save it;
  • Events Hubs for Apache Kafka, Event Hub endpoint compatible with Apache Kafka;
  • Event Publishers, applications or services that publish events to an Event Hub;
  • Publisher Policy, a unique id used to identify publishers;
  • Partitions ordered sequence of events in an Event Hub;
  • Event Consumer, applications or services reading events from an Event Hub;
  • Consumer Group, enables multiple consumers to each have a particular view from the consumed events;
  • Stream Offset, holds the position of an event inside a partition;
  • Checkpointing, the process to track events that are read from the ones that are not;

Azure Hub X Azure Grid

 
Azure Event Hub and Azure Event Grid both handle events but in a very different form and with a completely different type of usage as the following comparative table shows.
 
Road To AZ-204 - Developing Event Based Solutions
 

What is Azure Notification Hub?

 
Different from the previous topics, this is not a service to consume and publish events. Azure Notification Hub's focus is to send notifications to mobile apps in order to increase app engagement or app usage, you can filter the apps or users that are going to receive those notifications with the help of tags and those apps can be registered within Azure Notification Hub in two different models. 
 
In the installation model, the app sends an installation request to Azure Notification Hub with all required information to complete the installation process. This model is easier to implement and maintain.
 
In the registration model, the app sends a registration request to Azure Notification Hub with the PNS handlers and tags while the Azure Notification Hub answers with the registration id. Native or generic templates can be used in order to define the message. 
 
Azure Notification Hub main concepts:
  • Routing, the process to select the registrations to receive the push notification;
    • Broadcast, all registrations are going to receive the push notification;
    • Tag, registrations that contain the specific tag are going to receive the push notification;
    • Tag Expression, registrations that match the tag expressions are going to receive the push notification;
  • Notification Hub Namespace, groups one or more Notification Hub;
  • Tags used to label applications for better routing experience;
  • Templates define the format on how the client is going to receive the push notifications. Is platform-independent;
  • Platform Notification Service - PNS, each platform has its own PNS which is responsible to send the push notifications; 

Practical Examples
 
Azure Event Grid with Azure Function
 
Here we are going to configure an Event Subscription capturing subscribed to your Azure Subscription and having its events handles by an Azure Function. 
 
Pre-requisites:
  • Existent Azure Function App. Here named as "samplefunctioneventgrid" and with the .Net Core 3.1 runtime stack; 
Creating the Event Grid with Azure Portal
 
From your Azure Subscription, go to Events and then add an Event Subscription.
 
Road To AZ-204 - Developing Event Based Solutions
 
Configure the Event Subscription to collect every event that happens in your Azure Subscription and set the event handler as the Azure Function.
 
Road To AZ-204 - Developing Event Based Solutions
 
Result
 
Road To AZ-204 - Developing Event Based Solutions
 
Testing the Event Grid with Azure Function
 
Let's delete a resource and check the result.
 
Road To AZ-204 - Developing Event Based Solutions
 

Azure Event Hub with .Net Core

 
Create the Event Hub  
 
From your Azure Portal, go to Event Hub Namespace and create a new one. Here it's named as eventHubWithNetCore. 
 
Road To AZ-204 - Developing Event Based Solutions
 
Create a new Event Hub, from your Event Hub Namespace go to Entities and then Event Hub and add a new one. Here it's named sampleeventhub.
 
Road To AZ-204 - Developing Event Based Solutions
 
Result
 
Road To AZ-204 - Developing Event Based Solutions
 

Sending Events to your Azure Event Hub 

 
Similar to what we did before, we are going to create an Event Grid Subscription to get events related to our Azure Subscription but at this time we are going to use Azure Event Hub as an event handler.
 
Road To AZ-204 - Developing Event Based Solutions
 
.Net Core Application to consume events
Pre-Requisites
  • Existent .Net Core Project;
  • Event-Hub Connection String;
  • Existent Blob Container. Here named as a sample-blob container;
  • Blob Container Connection String;
  • NuGet Package Azure.Messaging.EventHubs;
  • NuGet Package Azure.Messaging.EventHubs.Processor; 
Retrieving your Event Hub Connection String. From your Event Hub Namespace go to Shared Access Signatures under Settings.
 
Road To AZ-204 - Developing Event Based Solutions
 
Connecting and handling Events from an Event Hub
  1. private const string ehubNamespaceConnectionString = "Endpoint=sb://eventhubwithnetcore.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=ARWoRUTA++GrBmNnumYvDBveo2Gkrs7cMFUCYH9euic=";  
  2.         private const string eventHubName = "sampleeventhub";  
  3.         private const string blobStorageConnectionString = "DefaultEndpointsProtocol=https;AccountName=storageaccountcloud8c65;AccountKey=oRVheUO8Xs/5PiimGjJoOwCA7Ee0YMCh4HQXAicCzg8noRYZMT8M2oWbyevLeodvmtv099Yj0UPJeFL23/K8RQ==;EndpointSuffix=core.windows.net";  
  4.         private const string blobContainerName = "sampleblobcontainer";  
  5.         static async Task Main(string[] args)  
  6.         {  
  7.             // Read from the default consumer group: $Default  
  8.             string consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName;  
  9.   
  10.             // Create a blob container client that the event processor will use   
  11.             BlobContainerClient storageClient = new BlobContainerClient(blobStorageConnectionString, blobContainerName);  
  12.   
  13.             // Create an event processor client to process events in the event hub  
  14.             EventProcessorClient processorClinet = new EventProcessorClient(storageClient, consumerGroup, ehubNamespaceConnectionString, eventHubName);  
  15.   
  16.             // Register handlers for processing events and handling errors  
  17.             processorClinet.ProcessEventAsync += ProcessEventHandler;  
  18.             processorClinet.ProcessErrorAsync += ProcessErrorHandler;  
  19.   
  20.             // Start processing  
  21.             await processorClinet.StartProcessingAsync();  
  22.   
  23.             // Processing time  
  24.             DateTime finishProcessing = DateTime.Now.AddMinutes(10);  
  25.               
  26.             while (DateTime.Now < finishProcessing)  
  27.             {  
  28.                 // Wait for 10 seconds for the events to be processed  
  29.                 await Task.Delay(TimeSpan.FromSeconds(10));  
  30.             }  
  31.   
  32.             // Stop processing  
  33.             await processorClinet.StopProcessingAsync();  
  34.         }  
Consuming a successful event
  1. static async Task ProcessEventHandler(ProcessEventArgs eventArgs)  
  2.      {  
  3.          // Write the body of the event to the console window  
  4.          Console.WriteLine("\tRecevied event: {0}", Encoding.UTF8.GetString(eventArgs.Data.Body.ToArray()));  
  5.   
  6.          // Update checkpoint in the blob storage so that the app receives only new events the next time it's run  
  7.          await eventArgs.UpdateCheckpointAsync(eventArgs.CancellationToken);  
  8.      }  
Consuming an event with error
  1. static Task ProcessErrorHandler(ProcessErrorEventArgs eventArgs)  
  2.     {  
  3.         // Write details about the error to the console window  
  4.         Console.WriteLine($"\tPartition '{ eventArgs.PartitionId}': an unhandled exception was encountered.");  
  5.         Console.WriteLine(eventArgs.Exception.Message);  
  6.         return Task.CompletedTask;  
  7.     }   
Result 
 
From the console window
 
Road To AZ-204 - Developing Event Based Solutions
 
From the Blob Container 
 
Road To AZ-204 - Developing Event Based Solutions
 
.Net Core Application to publish batch of events
Pre-Requisites
  • Existent .Net Core Project;
  • Event-Hub Connection String;
  • Nuget Package Azure.Messaging.EventHubs;
Sending a random batch of events
  1. private const string connectionString = "Endpoint=sb://eventhubwithnetcore.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=ARWoRUTA++GrBmNnumYvDBveo2Gkrs7cMFUCYH9euic=";  
  2.   private const string eventHubName = "sampleeventhub";  
  3.   static async Task Main(string[] args)  
  4.   {  
  5.       Random random = new Random();  
  6.       int numberOfEvents = random.Next(5, 10);  
  7.       await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName))  
  8.       {  
  9.           // Create a batch of events   
  10.           using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();  
  11.   
  12.   
  13.           // Add events to the batch. An event is represented by a collection of bytes and metadata.   
  14.           for (int i = 0; i < numberOfEvents; i++)  
  15.           {  
  16.               eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes($"Event number {i}")));  
  17.           }  
  18.   
  19.           // Use the producer client to send the batch of events to the event hub  
  20.           await producerClient.SendAsync(eventBatch);  
  21.           Console.WriteLine($"A batch of {numberOfEvents} events has been published.");  
  22.       }  
  23.   }  
The result from the console client
 
Road To AZ-204 - Developing Event Based Solutions
 
From Event Hub
 
Road To AZ-204 - Developing Event Based Solutions
 
Azure Notification Hub
 
In order to demonstrate an Azure Notification Hub an existent mobile app is required, and my friend Prasanna Murali gives an excellent walkthrough on how to use it: