Azure Event Hub Python SDK - is the order of adding events to an EventDataBatch also the order in which the events will be written to the eventhub?

Boomgaard, Guusje 0 Reputation points
2025-04-22T16:32:38.1+00:00

Hi,

I would like to use the python EventHubProducerClient to send events in batches to an eventhub while guaranteeing the order of events within their batch. My plan is to create an EventDataBatch first and then add events to it in my desired order.

Is the order in which I add events to the EventDataBatch object the same order in which the events will be written to the eventhub? (i.e. "My first event", "My second event", "My third event" - see example below)


   import os
   from azure.eventhub import EventHubProducerClient, EventData
   from azure.identity import DefaultAzureCredential

   eventhub_fully_qualified_namespace = os.environ["EVENT_HUB_HOSTNAME"]
   eventhub_name = os.environ["EVENT_HUB_NAME"]

   producer = EventHubProducerClient(
       fully_qualified_namespace=eventhub_fully_qualified_namespace,
       eventhub_name=eventhub_name,  # EventHub name should be specified if it doesn't show up in connection string.
       credential=DefaultAzureCredential(),
   )
   try:
       event_data_batch = producer.create_batch()

       while True:
           try:
               event_data_batch.add(EventData("My first event"))
               event_data_batch.add(EventData("My second event"))
               event_data_batch.add(EventData("My third event"))
             
           except ValueError:
               # EventDataBatch object reaches max_size.
               # New EventDataBatch object can be created here to send more data
               break

       producer.send_batch(event_data_batch)
   finally:
       # Close down the producer handler.
       producer.close()
Azure Event Hubs
Azure Event Hubs
An Azure real-time data ingestion service.
711 questions
{count} votes

1 answer

Sort by: Most helpful
  1. J N S S Kasyap 1,715 Reputation points Microsoft External Staff
    2025-04-22T17:53:07.19+00:00

    Hi @Boomgaard, Guusje

    Is the order of adding events to an EventDataBatch also the order in which the events will be written to the eventhub?

    Yes, when using the Azure Event Hubs Python SDK, the order in which you add events to an EventDataBatch is preserved when the batch is sent. This means that if you add events in a specific sequence, they will be sent to the Event Hub in that same order.​

    The EventDataBatch class allows you to add events using the add method until the maximum batch size limit is reached. Once the batch is created and events are added, you can send the entire batch using the send_batch method of the EventHubProducerClient
    However, it's important to note that while the order is preserved within a single batch, Azure Event Hubs does not guarantee the order of events across multiple batches or partitions. If maintaining the order of events is critical for your application, you should consider sending events to a specific partition by specifying a partition_id or using a consistent partition_key. This ensures that all related events are sent to the same partition, preserving their order.

    Please refer the below Microsoft documentation for the more information
    https://learn.microsoft.com/en-us/python/api/azure-eventhub/azure.eventhub.eventdatabatch?view=azure-python
    https://learn.microsoft.com/en-us/python/api/azure-eventhub/azure.eventhub?view=azure-python

    I hope this information helps. Please do let us know if you have any further queries.

    Kindly consider upvoting the comment if the information provided is helpful. This can assist other community members in resolving similar issues.

    Thank you.

    2 people found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.