Azure Event Hubs Documentation

Producer and Consumer Concepts

Understanding the roles of producers and consumers is fundamental to working with Azure Event Hubs. Event Hubs acts as a highly scalable data streaming platform, enabling you to ingest and process millions of events per second.

Producers

Producers are applications or services that send event data to an Event Hub. They are responsible for generating and publishing events. Event Hubs supports various protocols for producers, including AMQP, Kafka, and HTTPS.

Producer App 1 Producer App 2 Azure Event Hubs

Consumers

Consumers are applications or services that read event data from an Event Hub. They subscribe to an Event Hub and process the incoming stream of events. Consumers typically work within the context of a consumer group.

Azure Event Hubs Consumer Group A (App 1) Consumer Group A (App 2)
Consumer Group B (App 3)

Consumer Groups

Consumer groups are a key concept for enabling scalable event consumption. Event Hubs allows multiple applications, or different instances of the same application, to read from an Event Hub concurrently without blocking each other.

The Flow

The general data flow in Event Hubs is as follows:

  1. Producers send events to an Event Hub.
  2. Event Hubs distributes these events across its partitions.
  3. Consumers, belonging to a specific consumer group, read events from the partitions. Each consumer within a group might read from one or more partitions.
  4. Consumers process the events and update their position (offset) within each partition.

This architecture provides a robust and scalable solution for handling high-throughput, real-time data streams.

Example Code Snippet (Conceptual)

Here's a conceptual look at how producers and consumers might interact:

Producer Example (Conceptual C#)


using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Producer;
using System;
using System.Text;
using System.Threading.Tasks;

// ... connection string and hub name setup ...

await using var producerClient = new EventHubProducerClient(connectionString, eventHubName);

try
{
    using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();

    for (int i = 0; i < 5; i++)
    {
        string eventBody = $"{{ \"message\": \"Event #{i}\" }}";
        if (!eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes(eventBody))))
        {
            throw new Exception($"The event {i} is too large for the batch.");
        }
    }

    await producerClient.SendAsync(eventBatch);
    Console.WriteLine("Sent batch of events.");
}
catch (Exception ex)
{
    Console.WriteLine($"Error sending batch: {ex.Message}");
}
            

Consumer Example (Conceptual C#)


using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Consumer;
using System;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;

// ... connection string, hub name, consumer group name setup ...

var consumerClient = new EventHubConsumerClient(
    EventHubConsumerClient.DefaultConsumerGroupName,
    connectionString,
    eventHubName);

Console.WriteLine("Listening for events...");

await foreach (PartitionEvent partitionEvent in consumerClient.ReadEventsAsync())
{
    Console.WriteLine($"Received event: {Encoding.UTF8.GetString(partitionEvent.Data.EventBody.ToArray())} from partition {partitionEvent.Partition.Id}");
    // Process the event...
    // Consumer group's offset is automatically managed by the SDK by default
}