Events in Azure Event Hubs

Events are the fundamental units of data processed by Azure Event Hubs. They represent a discrete, immutable sequence of bytes that are captured and made available for consumption. Understanding the structure and properties of events is crucial for effectively building event-driven applications with Event Hubs.

Diagram showing event flow in Azure Event Hubs

Conceptual flow of events through Event Hubs.

Event Structure

An event, often referred to as an event payload or simply message, consists of the following key components:

System Properties

Event Hubs assigns several system properties to events when they are sent. These include:

Custom Properties

You can attach custom metadata to your events using key-value pairs. This is invaluable for adding business context, routing hints, or identifiers that your consumers can use to process events.

Event Serialization

While Event Hubs treats the event body as raw bytes, the way you serialize and deserialize this data is application-specific. Common serialization formats include:

Tip: Choose a serialization format that balances your needs for data size, performance, schema evolution, and ease of use across your applications.

Event Ordering

Event Hubs guarantees ordering of events within a partition. If you need strict ordering across all events, you should design your application to send all related events to the same partition, often by using a consistent PartitionKey.

Example: Sending an Event

Here's a conceptual example of how an event might be constructed and sent (using a hypothetical SDK syntax):


// C# Example (conceptual)
var eventData = new EventData(Encoding.UTF8.GetBytes("{\"sensorId\": \"abc-123\", \"temperature\": 25.5}"));
eventData.Properties.Add("contentType", "application/json");
eventData.Properties.Add("sourceSystem", "IoTDevice");
eventData.PartitionKey = "device-group-1"; // Ensures this event goes to the same partition as other events from this group

await eventHubProducerClient.SendEventAsync(eventData);
        

Note: The actual implementation details will vary depending on the specific Event Hubs SDK you are using for your chosen programming language.

Key Takeaways