Event Hubs for streaming data in Azure - Time & Space Complexity
When using Event Hubs to stream data, it's important to understand how the time to process messages grows as more data arrives.
We want to know how the number of operations changes as the amount of streaming data increases.
Analyze the time complexity of sending multiple messages to an Event Hub.
// Create Event Hub client
var producer = new EventHubProducerClient(connectionString, eventHubName);
// Send batch of messages
using EventDataBatch batch = await producer.CreateBatchAsync();
foreach (var message in messages) {
batch.TryAdd(new EventData(message));
}
await producer.SendAsync(batch);
await producer.DisposeAsync();
This code sends a batch of messages to an Event Hub for streaming processing.
Look at what repeats as the number of messages grows.
- Primary operation: Adding each message to the batch with
TryAdd. - How many times: Once per message in the input list.
- Secondary operation: Sending the batch once with
SendAsync. - How many times: Exactly once, regardless of message count.
As the number of messages increases, the number of TryAdd calls grows directly with it, but sending happens once.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 calls to TryAdd, 1 call to SendAsync |
| 100 | 100 calls to TryAdd, 1 call to SendAsync |
| 1000 | 1000 calls to TryAdd, 1 call to SendAsync |
Pattern observation: The number of add operations grows linearly with messages, but sending stays constant.
Time Complexity: O(n)
This means the time to prepare the batch grows directly with the number of messages, while sending happens once.
[X] Wrong: "Sending each message separately takes the same time as sending a batch."
[OK] Correct: Sending messages one by one causes many send operations, increasing time much more than batching.
Understanding how streaming data operations scale helps you design efficient cloud solutions and shows you can think about performance in real systems.
"What if we sent each message individually instead of batching? How would the time complexity change?"