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
In a nutshell
  • 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.

Accountthe top levelDatabasegroups containersContainerholds itemsItemyour data

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

ModeHow it worksGood for
Provisioned, manualYou set a fixed number of RU per secondSteady, predictable load
Provisioned, autoscaleThe limit scales automatically up to a maximumVariable load
ServerlessNo throughput is set, and you pay for the RUs each request usesIntermittent or small workloads, development

Partitioning

Items that share a partition key value form one logical partition. Azure places logical partitions on physical partitions it manages, and one physical partition can host several logical partitions, such as Lisbon and Tokyo. 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.

LevelMeaning in short
StrongReads always see the latest committed write
Bounded stalenessReads lag behind writes by a set amount of time or number of versions
SessionA client always reads its own writes. This is the default
Consistent prefixReads never see writes out of order
EventualReads 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.

APIData modelPick it when
NoSQLJSON documents, a SQL like query languageBuilding a new application, the native choice
MongoDBDocumentsMoving an existing MongoDB application
PostgreSQLDistributed relational tablesYou want PostgreSQL with horizontal scale
Apache CassandraColumn familyMoving a Cassandra application
Apache GremlinGraph of nodes and edgesStoring and traversing relationships
TableKey and attribute entitiesMoving 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

ScenarioSensible level
A bank balance that must always be exactStrong
A user must see their own posts straight awaySession, the default
A social feed where a short delay is fineEventual
Events that must appear in the order writtenConsistent 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 DBAzure SQL Database
ModelDocuments, graph, key value, column familyRelational tables
SchemaFlexibleFixed
ScaleScales out across partitions and regionsScales up, with read replicas
Best forGlobal, low latency, flexible dataTransactions 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.
Exam tip

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.