DP-900Chapter 8 of 101Objective 2.4

Azure Cosmos DB

Azure Cosmos DB is a globally distributed, multi-model database service designed for low-latency, high-availability applications. For the DP-900 exam, this chapter covers approximately 10-15% of questions related to non-relational data stores, focusing on Cosmos DB's core features: global distribution, consistency models, throughput provisioning, and API support. Understanding Cosmos DB is essential because it represents the primary NoSQL database service on Azure, and the exam tests your ability to choose the right data store for given requirements.

25 min read
Intermediate
Updated May 31, 2026

Cosmos DB as a global document factory

Imagine a global document factory with multiple distribution centers in cities worldwide. Each center can produce and store copies of documents (data) for local customers. When a customer requests a document, the nearest center can serve it instantly. If a center receives a change to a document, it updates its local copy and sends the change to a central replication system. That system ensures all other centers eventually receive the update, but with a slight delay—maybe a few seconds. The factory offers different consistency levels: strong means all centers wait for confirmation from every other center before confirming the write (high latency, but everyone sees the same data instantly); eventual means updates propagate asynchronously, and readers might see old data for a while. The factory also supports multiple document formats (JSON, MongoDB, Cassandra, etc.) through different APIs, each tailored to a specific customer community. You can choose the consistency level per request, balancing speed versus accuracy. The factory automatically scales by adding more centers or more capacity within a center, and you only pay for the storage and throughput you use, measured in Request Units per second (RUs). This mirrors Cosmos DB's global distribution, multiple consistency models, multi-API support, and RU-based pricing.

How It Actually Works

What is Azure Cosmos DB?

Azure Cosmos DB is a fully managed NoSQL database service that provides turnkey global distribution, elastic scaling of throughput and storage, and multiple consistency models. It is designed for applications that require low-latency reads and writes (<10 ms at the 99th percentile), high availability (99.999% SLA for multi-region writes), and the ability to handle globally distributed data. Unlike traditional relational databases, Cosmos DB is schema-agnostic and stores data as items (documents) in containers, which are analogous to tables. Each item is automatically indexed without requiring schema or secondary indexes.

How It Works Internally

Cosmos DB uses a distributed storage engine based on a log-structured merge-tree (LSM tree) and a write-optimized indexing subsystem. Data is partitioned horizontally across physical partitions, each responsible for a range of partition key values. The partition key is a property you choose when creating a container; it determines how data is distributed. Each physical partition is replicated across multiple replicas within a region, and for global distribution, replicas are also maintained in other regions. Writes are committed to a quorum of replicas in the write region (for single-region writes) or to the local region (for multi-region writes) and then replicated asynchronously to other regions. The consistency level you choose affects how quickly replicas converge.

Key Components and Defaults

Account: The top-level resource. It can be created with a single region or multiple regions. Default consistency is set at the account level but can be overridden per request.

Database: A logical container for one or more containers. It has no performance impact.

Container: The unit of scalability. Each container has a provisioned throughput (RUs) and a partition key. Items in a container are automatically indexed.

Item: A JSON document (or other format depending on API) stored in a container. The maximum item size is 2 MB.

Request Unit (RU): A normalized measure of throughput cost. 1 RU = 1 KB read (strong consistency) or 2 KB read (eventual consistency). A write of a 1 KB item costs 5 RUs. You can provision RUs per container or per database.

Partition Key: A required property for each container. It must have high cardinality (many distinct values) to ensure even distribution. The maximum partition size is 20 GB.

Consistency Levels: Five levels: Strong, Bounded Staleness, Session, Consistent Prefix, Eventual. Default is Session. Strong ensures linearizability but increases write latency. Eventual provides the lowest latency but no ordering guarantees.

Configuration and Verification

You can create a Cosmos DB account using the Azure portal, CLI, or PowerShell. Example CLI command to create an account:

az cosmosdb create --name mycosmosdb --resource-group myrg --kind GlobalDocumentDB --locations regionName=westus failoverPriority=0 isZoneRedundant=false

To create a container:

az cosmosdb sql container create --account-name mycosmosdb --resource-group myrg --database-name mydb --name mycontainer --partition-key-path /id --throughput 400

To verify throughput:

az cosmosdb sql container throughput show --account-name mycosmosdb --resource-group myrg --database-name mydb --name mycontainer

Interaction with Related Technologies

Cosmos DB integrates with Azure Functions for event-driven processing via change feed. The change feed is a persistent log of changes to items in a container, ordered by modification time. You can also use Azure Synapse Link for near-real-time analytics on Cosmos DB data without impacting transactional workloads. Cosmos DB supports multiple APIs: SQL (native), MongoDB, Cassandra, Gremlin (graph), and Table. Each API uses the same underlying engine but different wire protocols and SDKs.

Throughput Provisioning

You can provision throughput at the container level (dedicated) or database level (shared). Shared throughput is cost-effective for many small containers but cannot exceed 1000 RUs per container. Minimum throughput for a container is 400 RUs. You can also use autoscale, which scales between 10% and 100% of the maximum RUs you set. The RU cost of a query depends on the query complexity, index usage, and consistency level.

Global Distribution

You can add or remove regions at any time with no downtime. For multi-region writes, you enable "Enable multiple write regions" at the account level. Conflict resolution policies (last-writer-wins or custom) handle simultaneous writes to the same item in different regions. The SLA for multi-region writes is 99.999% read and write availability.

Security

Cosmos DB provides encryption at rest and in transit. Access is controlled via primary/secondary keys, read-only keys, or Azure Active Directory (AAD) integration (for SQL API). Firewall rules and virtual network service endpoints are supported. Data is encrypted at rest with Microsoft-managed or customer-managed keys (CMK).

Walk-Through

1

Create a Cosmos DB account

In the Azure portal, navigate to 'Create a resource' > 'Databases' > 'Azure Cosmos DB'. Choose the API (e.g., Core (SQL) for native JSON). Select a resource group, account name, location, and capacity mode (provisioned throughput or serverless). For global distribution, add additional regions later. Set default consistency to Session (recommended for most apps). Click Review + Create. The account creation takes a few minutes. Under the hood, Azure provisions physical replicas in the selected regions and configures the replication engine.

2

Create a database and container

Inside the Cosmos DB account, click 'Data Explorer' > 'New Container'. Specify a database ID (or use existing), container ID, partition key (e.g., /userId), and throughput (e.g., 400 RUs). The partition key must be a property present in every item. Cosmos DB will hash the partition key value to assign items to physical partitions. If you choose shared throughput at the database level, the throughput is shared among all containers in that database, with each container getting a minimum of 100 RUs.

3

Add items to the container

In Data Explorer, select the container, click 'Items' > 'New Item'. Enter a JSON document. The document must include the partition key property (e.g., "userId": "123"). Cosmos DB automatically generates an id property if not provided. Click 'Save'. The item is written to the partition corresponding to the partition key value. The write operation consumes RUs based on item size and consistency level. You can also upload multiple items using the SDK.

4

Query items using SQL API

In Data Explorer, click 'New SQL Query'. Write a query like: SELECT * FROM c WHERE c.userId = '123'. Cosmos DB uses a SQL-like syntax. Queries are executed against the index. For best performance, include the partition key in the WHERE clause to limit the query to a single partition. Otherwise, the query may fan out across all partitions (cross-partition query), consuming more RUs. You can view the RU charge for each query in the Query Results pane.

5

Scale throughput or add regions

In the Cosmos DB account, under 'Scale & Settings', you can adjust throughput for a container (increase or decrease) or add/remove regions. Scaling throughput is instantaneous but may require repartitioning if the partition size exceeds 20 GB. Adding a region replicates all data to the new region asynchronously. For multi-region writes, enable the option in 'Replicate data globally'. Conflict resolution policies must be configured.

What This Looks Like on the Job

Enterprise Scenario 1: Global E-Commerce Product Catalog

A multinational retailer uses Cosmos DB to store product information (name, price, inventory) across North America, Europe, and Asia. The application requires <10 ms read latency for users worldwide. They deploy Cosmos DB with multi-region writes enabled in three regions: West US, West Europe, and Southeast Asia. The partition key is /productId, which has high cardinality. Throughput is set to 10,000 RUs per container, with autoscale to handle traffic spikes during sales. They use Session consistency for user-facing reads (to see their own writes) and Eventual consistency for background analytics. A common misconfiguration is choosing a partition key with low cardinality (e.g., /category), causing hot partitions and throttling. The solution requires careful partition key design and monitoring via Azure Monitor metrics.

Enterprise Scenario 2: IoT Telemetry Ingestion

A manufacturing company collects sensor data from thousands of devices. They use Cosmos DB with the MongoDB API to store time-series data. Each sensor sends a JSON document every second. The partition key is /deviceId, and they use a time-to-live (TTL) of 30 days to automatically expire old data. They provision 50,000 RUs for the container to handle peak ingestion. Since data is append-only, they use Eventual consistency for writes to minimize latency. The change feed triggers an Azure Function that aggregates data into a separate container for reporting. A common issue is exceeding the 20 GB partition size limit if a single device sends too much data; they mitigate by adding a synthetic partition key (e.g., /deviceId + /date).

Enterprise Scenario 3: Real-Time Leaderboard for Gaming

A gaming company uses Cosmos DB with the Gremlin (graph) API to manage player relationships and leaderboards. The graph contains vertices (players) and edges (friendships, scores). They use Strong consistency to ensure leaderboard accuracy. The partition key is /gameId. Throughput is set to 5,000 RUs. They use multi-region writes to support players in the US and EU. A misconfiguration is using the default Session consistency, which can cause stale leaderboard reads. They also need to handle conflict resolution for simultaneous score updates; they use last-writer-wins with a timestamp.

How DP-900 Actually Tests This

What DP-900 Tests on Cosmos DB

The DP-900 exam (Objective 2.4: Describe non-relational data services on Azure) focuses on Cosmos DB's core features: global distribution, consistency models, throughput (RUs), partition keys, and multi-API support. Expect 3-5 questions. Specific objective codes: 2.4.1 (identify appropriate data store for workloads), 2.4.2 (describe features of Cosmos DB).

Common Wrong Answers and Why

1.

Choosing 'Relational database' for globally distributed low-latency reads – Candidates confuse Cosmos DB with Azure SQL Database. Cosmos DB is NoSQL and designed for global distribution; SQL DB is relational and primarily single-region.

2.

Selecting 'Strong consistency' for all scenarios – Strong consistency increases write latency and is not needed for many apps. The exam tests that Session consistency is the default and most common.

3.

Thinking partition key is optional – Every container must have a partition key. It is required at creation time. Some candidates think it can be added later or is only for performance.

4.

Assuming all APIs behave identically – While the underlying engine is the same, each API has its own syntax and features. For example, the MongoDB API uses MongoDB wire protocol, not SQL.

Specific Exam Values and Terms

RU (Request Unit): 1 RU = 1 KB read (strong) or 2 KB read (eventual). Write of 1 KB = 5 RUs.

Minimum throughput per container: 400 RUs.

Maximum item size: 2 MB.

Maximum partition size: 20 GB.

Consistency levels: Strong, Bounded Staleness (staleness limit: K versions or T time), Session, Consistent Prefix, Eventual.

Default consistency: Session.

SLA: 99.999% read/write availability for multi-region writes.

APIs: SQL, MongoDB, Cassandra, Gremlin, Table.

Edge Cases and Exceptions

Serverless mode: No provisioned throughput; pay per request. Suitable for low-traffic or dev/test. Not covered in depth but may appear as a comparison.

Autoscale: Scales between 10% and 100% of max RUs. Exam may ask about cost implications.

Change feed: Only available for SQL API and MongoDB API (via change streams). Not for Cassandra or Table.

How to Eliminate Wrong Answers

If a question asks for a globally distributed, low-latency NoSQL database, eliminate relational options (Azure SQL, MySQL). If the requirement includes multi-model support (document, graph, key-value), Cosmos DB is the answer. If the requirement is for a simple key-value store with low cost, consider Table storage instead. For consistency, if the app needs immediate consistency across reads, choose Strong; if it can tolerate stale reads, choose Eventual. Session is the default and provides read-your-writes consistency.

Key Takeaways

Cosmos DB is a globally distributed, multi-model NoSQL database service.

It offers five consistency levels: Strong, Bounded Staleness, Session, Consistent Prefix, and Eventual. Session is the default.

Throughput is measured in Request Units (RUs). 1 RU = 1 KB read (strong) or 2 KB read (eventual). Write of 1 KB = 5 RUs.

Every container requires a partition key, which cannot be changed after creation. Choose a high-cardinality key to avoid hot partitions.

Maximum item size is 2 MB; maximum logical partition size is 20 GB.

Cosmos DB supports multiple APIs: SQL, MongoDB, Cassandra, Gremlin, and Table.

For multi-region writes, enable the option and configure conflict resolution (last-writer-wins or custom).

Change feed is available for SQL and MongoDB APIs to trigger Azure Functions on data changes.

Minimum provisioned throughput per container is 400 RUs.

Serverless mode is pay-per-request, suitable for low-traffic workloads.

Easy to Mix Up

These come up on the exam all the time. Here's how to tell them apart.

Cosmos DB SQL API

Multi-model: supports document, graph, key-value, column-family

Global distribution with multiple consistency levels

Low latency (single-digit milliseconds) at scale

Automatic indexing of all properties

Richer query capabilities with SQL-like syntax

Azure Table Storage

Key-value store only

Simple replication with eventual consistency only

Higher latency for global access

No automatic indexing; requires manual partition key design

Limited query: only by partition key and row key

Watch Out for These

Mistake

Cosmos DB is a relational database.

Correct

Cosmos DB is a NoSQL, multi-model database. It does not support relational schemas, joins, or ACID transactions across multiple items (except for transactional batch within a single partition). It stores JSON documents and is schema-agnostic.

Mistake

You can change the partition key after creating a container.

Correct

The partition key is set at container creation and cannot be changed. To use a different partition key, you must create a new container and migrate data. This is a common exam trap.

Mistake

Strong consistency is always the best choice for consistency.

Correct

Strong consistency increases write latency because it requires a quorum of replicas to acknowledge the write. It also reduces availability during network partitions. Most applications use Session consistency, which provides read-your-writes guarantee with lower latency.

Mistake

Cosmos DB only supports the SQL API.

Correct

Cosmos DB supports multiple APIs: SQL (Core), MongoDB, Cassandra, Gremlin (graph), and Table. Each API allows you to use existing SDKs and tools while leveraging Cosmos DB's global distribution and consistency models.

Mistake

Provisioned throughput is the only capacity mode.

Correct

Cosmos DB offers two capacity modes: provisioned throughput (manual or autoscale) and serverless. Serverless is pay-per-request and ideal for intermittent or low-traffic workloads. The exam may test the difference.

Do You Actually Know This?

Reveal each answer, then mark whether you got it right. Score 60%+ to unlock the next chapter.

Frequently Asked Questions

What is the difference between Cosmos DB and Azure SQL Database?

Cosmos DB is a globally distributed NoSQL database designed for low-latency, schema-agnostic workloads. Azure SQL Database is a relational database with fixed schema, ACID transactions, and strong consistency. Cosmos DB is ideal for IoT, gaming, and real-time apps; SQL DB is for traditional OLTP applications.

How do I choose the right partition key for Cosmos DB?

Choose a property that has high cardinality (many distinct values) and evenly distributes read/write requests. Avoid keys with few values (e.g., /status) that cause hot partitions. Also, ensure the partition key is present in every item. The exam may test that a good partition key prevents throttling.

What is the difference between provisioned throughput and autoscale?

Provisioned throughput allows you to set a fixed number of RUs per second. Autoscale automatically scales based on traffic, between 10% and 100% of the maximum RUs you set. Autoscale is cost-effective for variable workloads but has a higher per-RU cost than manual provisioning.

Can I use Cosmos DB with MongoDB tools?

Yes, Cosmos DB supports the MongoDB API, which uses the MongoDB wire protocol. You can use existing MongoDB drivers and tools (e.g., MongoShell, Compass) to connect to Cosmos DB. However, some MongoDB features (e.g., aggregation pipelines, indexes) may have limitations.

What is the change feed in Cosmos DB?

The change feed is a persistent, ordered log of changes (inserts and updates) to items in a container. It can be consumed by Azure Functions or custom processors to trigger actions (e.g., sending notifications, updating a cache). It is available for SQL and MongoDB APIs.

How does Cosmos DB achieve high availability?

Cosmos DB replicates data across multiple replicas within a region and across regions for global distribution. With multi-region writes, you get 99.999% read and write availability. Automatic failover ensures continuity during regional outages.

What is the cost model for Cosmos DB?

You pay for provisioned throughput (RUs per hour) and consumed storage (GB per month). Data transfer costs apply for egress. Serverless mode charges per request (RU) and storage. The exam may ask about cost differences between provisioned and serverless.

Terms Worth Knowing

Ready to put this to the test?

You've just covered Azure Cosmos DB — now see how well it sticks with free DP-900 practice questions. Full explanations included, no account needed.

Done with this chapter?