Microsoft Azure · Data

Azure Cosmos DB

Cosmos DB is a globally distributed NoSQL database with single-digit-millisecond reads and writes at any scale.

Intermediate 5 min Lesson 12 of 28
Before you start:Azure SQL Database
0/28 in Microsoft Azure
On this page · 5 min · Intermediate

Learning objectives#

  • Model data as JSON documents
  • Choose a good partition key
  • Understand request units and consistency
  • Create and query items with the .NET SDK

Explanation#

Data lives in accounts → databases → containers → items (JSON documents). Different items in a container can have different shapes.

Every container has a partition key. Cosmos DB spreads data across physical partitions by this key, so choose one with many distinct values that queries usually include (for example customerId).

Throughput is measured in request units (RU/s). A 1 KB point read costs about 1 RU. Choose provisioned, autoscale or serverless capacity.

Five consistency levels — Strong, Bounded staleness, Session (default), Consistent prefix, Eventual — trade consistency for latency and availability.

Code example: Saving and reading orders#

C#Orders.cs Illustrative
using Azure.Identity;
using Microsoft.Azure.Cosmos;


var client = new CosmosClient("https://cosmos-shop.documents.azure.com:443/", new DefaultAzureCredential());
Container orders = client.GetContainer("shop", "orders");   // partition key: /customerId


var order = new Order("o-101", "c-1", 56400m, new[] { "Laptop", "Mouse" });
await orders.CreateItemAsync(order, new PartitionKey(order.customerId));


// Point read: id + partition key — the cheapest operation (~1 RU)
var read = await orders.ReadItemAsync<Order>("o-101", new PartitionKey("c-1"));
Console.WriteLine(
quot;
{read.Resource.id}: ₹{read.Resource.total} ({read.RequestCharge} RU)");
public record Order(string id, string customerId, decimal total, string[] items);
Expected output
o-101: ₹56400 (1 RU)

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. The client authenticates with Entra ID; no keys.
  2. Every item needs an id and its partition key value.
  3. A point read uses both, so Cosmos DB goes straight to the right partition.
  4. RequestCharge shows the RU cost of each operation — useful for tuning.

Real-world use case#

Shopping carts, user profiles, IoT telemetry and product catalogues that need fast reads worldwide.

Common mistakes#

A low-cardinality partition key

Using status (“pending”, “shipped”) puts most data in a few hot partitions. Use something like customerId.

Cross-partition queries everywhere

Queries without the partition key fan out to every partition and cost more RUs.

Best practices#

  • Pick the partition key from your most common queries
  • Prefer point reads (id + partition key)
  • Use Session consistency unless you need stronger
  • Watch RU charges and use autoscale

Remember this#

Partition key = how data is split. Choose many distinct values.

Point read (id + key) ≈ 1 RU — the cheapest operation.

Session consistency: you always read your own writes.

Practice exercise#

Your task: Which partition key is best for orders queried by customer: /status, /country or /customerId?

Show solution
Text Illustrative
/customerId — many distinct values and it matches the common query “orders for this customer”.

Mini quiz#

Azure Cosmos DB quizMultiple choice Question 1 of 1

What is the default consistency level in Cosmos DB?

Score so far: 0/0

Want more? Practise all Microsoft Azure MCQs · Microsoft Azure interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.