Event Hubs Partitions

Partitions are a fundamental concept in Azure Event Hubs that enable high throughput, parallel processing, and ordered delivery of events within a logical stream.

What is a Partition?

An Event Hub is divided into one or more partitions. Each partition is an ordered, immutable sequence of events. Events are appended to a partition in the order they are received. Event Hubs guarantees that events within a single partition are always processed in the order they were sent.

Conceptual Diagram:

Event Hubs Partition Diagram

Key Characteristics of Partitions:

Partitioning Strategy

Choosing the right number of partitions and using partition keys effectively are critical for optimal performance and cost management.

Number of Partitions:

The number of partitions determines the maximum parallel throughput you can achieve.

Partition Key Usage:

Use partition keys when you need to ensure that related events are processed in order.

If you don't specify a partition key, Event Hubs distributes events evenly across all partitions using a round-robin mechanism. This maximizes throughput but does not guarantee ordering for related events.

Important Note:

The number of partitions for an Event Hub is set at creation time and cannot be changed later. If you need to increase the number of partitions, you must create a new Event Hub with the desired configuration.

Partitions and Consumer Groups

Consumer groups are logical groupings of consumers. Within a consumer group, each partition is consumed by only one instance of a consumer at a time. This allows multiple consumer groups to independently read from the same Event Hub without interfering with each other. Each consumer group maintains its own state and offset, enabling independent consumption of the event stream.

Example: Publishing with a Partition Key

// Example using Azure SDK for .NET
        var producer = new EventHubProducerClient("YOUR_EVENTHUB_CONNECTION_STRING");
        var eventData = new EventData(Encoding.UTF8.GetBytes("{\"message\": \"Sensor reading\"}"));
        eventData.PartitionKey = "sensor-123"; // Events with this key go to the same partition

        await producer.SendAsync(eventData);
        

By setting the PartitionKey, you ensure that all events with the key "sensor-123" will be directed to the same partition, preserving their order.