Non Relational Data on Azure · Topic 3 of 3
Azure Cosmos DB
DP-900 Azure Cosmos DB: use cases, request units, partitioning, five consistency levels, global distribution, throughput modes and the six APIs compared.
- 6 min
- Includes a diagram
- Cosmos DB is a managed, globally distributed NoSQL database.
- Operations are priced in request units.
- Capacity modes are provisioned (manual or autoscale) and serverless.
What it is
Azure Cosmos DB is a fully managed, globally distributed NoSQL database. It is built for applications that need very fast responses, flexible data shapes and the ability to scale out, with data replicated close to users around the world.
It stores data as items in containers, and the API you choose decides the data model you use.
How it works
Use cases
Cosmos DB suits workloads where speed, scale and flexibility matter more than joins across many tables.
- Web, mobile and gaming back ends with many users in many regions.
- Internet of Things telemetry and device data.
- Retail catalogues where each product has different attributes.
- Personalisation and real time recommendations.
- Social networks and relationship data, using the graph API.
Items, containers and request units
You create an account, then databases, then containers. The items in a container are your data, such as JSON documents. Every property is indexed automatically by default, so queries are fast without index design.
The cost of every operation is measured in request units, or RUs. An RU is a blended measure of the CPU, memory and input and output that a read, write or query uses. A small read costs few RUs, and a large query costs more.
Throughput and capacity modes
| Mode | How it works | Good for |
|---|---|---|
| Provisioned, manual | You set a fixed number of RU per second | Steady, predictable load |
| Provisioned, autoscale | The limit scales automatically up to a maximum | Variable load |
| Serverless | No throughput is set, and you pay for the RUs each request uses | Intermittent or small workloads, development |
Partitioning
Cosmos DB scales by splitting data into partitions. You choose a partition key, a property such as customerId. Items that share a partition key value belong to the same logical partition. Azure places logical partitions on physical partitions and manages that for you, so one physical partition can host several logical ones. A good partition key spreads data and requests evenly.
Global distribution and consistency
You can add Azure regions to an account with a few clicks. Data is replicated automatically, and an account can accept writes in more than one region. Reads are served from the region nearest the user, which keeps latency low.
When data is copied between regions, you decide how fresh a read must be. Cosmos DB offers five consistency levels, from strongest to weakest.
| Level | Meaning in short |
|---|---|
| Strong | Reads always see the latest committed write |
| Bounded staleness | Reads lag behind writes by a set amount of time or number of versions |
| Session | A client always reads its own writes. This is the default |
| Consistent prefix | Reads never see writes out of order |
| Eventual | Reads may lag, with the lowest latency and cost |
Stronger consistency costs more latency across regions. Weaker consistency is faster.
The APIs
The API decides the data model and the way you talk to the database.
| API | Data model | Pick it when |
|---|---|---|
| NoSQL | JSON documents, a SQL like query language | Building a new application, the native choice |
| MongoDB | Documents | Moving an existing MongoDB application |
| PostgreSQL | Distributed relational tables | You want PostgreSQL with horizontal scale |
| Apache Cassandra | Column family | Moving a Cassandra application |
| Apache Gremlin | Graph of nodes and edges | Storing and traversing relationships |
| Table | Key and attribute entities | Moving a Table Storage application |
A worked scenario
A mobile game has players in Europe, Asia and North America, and each player profile is a small JSON document with different fields. The team picks the NoSQL API, uses playerId as the partition key, and replicates to all three regions so each player reads from a nearby copy. They keep the default session consistency, because a player must always see their own latest score. Because traffic peaks in the evening, they use autoscale throughput.
An item and a query
An item in the NoSQL API is a JSON document. The container's partition key could be /city.
{
"id": "p-1021",
"city": "Lisbon",
"name": "Ana Silva",
"tags": ["gold", "newsletter"]
}
You read it with a query that looks like SQL, where c stands for each item in the container.
SELECT c.name FROM c WHERE c.city = "Lisbon"
A query that includes the partition key value goes to one partition and costs fewer request units than one that must look in all of them.
Matching consistency to the need
| Scenario | Sensible level |
|---|---|
| A bank balance that must always be exact | Strong |
| A user must see their own posts straight away | Session, the default |
| A social feed where a short delay is fine | Eventual |
| Events that must appear in the order written | Consistent prefix |
Availability and failover
Cosmos DB replicates data inside a region and across the regions you add. If a region becomes unavailable, service managed failover can move the write role to another region automatically, so the application keeps working. Adding availability zones within a region gives extra protection against a datacenter failure. Multi region writes let users write to the nearest copy, with conflict resolution handled by the service.
Cosmos DB compared with Azure SQL Database
| Cosmos DB | Azure SQL Database | |
|---|---|---|
| Model | Documents, graph, key value, column family | Relational tables |
| Schema | Flexible | Fixed |
| Scale | Scales out across partitions and regions | Scales up, with read replicas |
| Best for | Global, low latency, flexible data | Transactions with joins and constraints |
What to remember for the exam
- Cosmos DB is a managed, globally distributed NoSQL database.
- Operations are priced in request units.
- Capacity modes are provisioned (manual or autoscale) and serverless.
- A partition key creates logical partitions, and Azure manages the physical ones.
- There are five consistency levels, and session is the default.
- The APIs are NoSQL, MongoDB, PostgreSQL, Apache Cassandra, Apache Gremlin and Table.
The wire protocol APIs exist to make migration easy. An existing MongoDB app points to the MongoDB API, and an existing Cassandra app points to the Cassandra API.
How the exam asks about this
- "A graph database for relationships." Apache Gremlin API.
- "Billed only for what each request consumes." Serverless.
- "Which unit measures the cost of database operations?" Request units.
- "Which level guarantees the latest committed read?" Strong consistency.
Common mistakes
Treating Cosmos DB as one data model. The API selects the model, from documents to graphs.
Choosing a poor partition key. A key that sends most traffic to one value creates a hot partition.
Assuming strong consistency is free. It adds latency, especially across regions.
Expecting joins like a relational database. Cosmos DB is built around items and partitions.
Check yourself
Which unit measures the cost of an operation in Cosmos DB?
The request unit, or RU.
Which consistency level is the default?
Session consistency.
Which API stores nodes and edges?
The Apache Gremlin API.
Topic 3 of 3 in Non Relational Data on Azure
Test yourself on Non Relational Data on Azure
The full Microsoft DP-900 question bank has 503 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Microsoft. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.