0
0
Azurecloud~5 mins

Cosmos DB overview and use cases in Azure - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Cosmos DB overview and use cases
O(n)
Understanding Time Complexity

We want to understand how the time to perform operations in Cosmos DB changes as we work with more data or requests.

Specifically, how does the number of operations or API calls grow when using Cosmos DB for common tasks?

Scenario Under Consideration

Analyze the time complexity of inserting multiple documents into a Cosmos DB container.


// Insert multiple items into Cosmos DB container
for (int i = 0; i < itemCount; i++) {
    await container.CreateItemAsync(items[i]);
}
    

This code inserts each item one by one into the Cosmos DB container.

Identify Repeating Operations

Look at what repeats as we insert items:

  • Primary operation: The CreateItemAsync API call to add one document.
  • How many times: Once for each item in the input list.
How Execution Grows With Input

As the number of items grows, the number of insert calls grows the same way.

Input Size (n)Approx. API Calls/Operations
1010 calls
100100 calls
10001000 calls

Pattern observation: The number of calls grows directly with the number of items.

Final Time Complexity

Time Complexity: O(n)

This means the time to insert items grows linearly with how many items you add.

Common Mistake

[X] Wrong: "Inserting multiple items at once will take the same time as inserting one item."

[OK] Correct: Each item requires a separate API call, so more items mean more calls and more time.

Interview Connect

Understanding how Cosmos DB operations scale helps you design efficient data solutions and shows you can think about performance in cloud services.

Self-Check

"What if we used batch operations to insert multiple items at once? How would the time complexity change?"