Lesson 1 of 4 Developing with Azure Cosmos DB for NoSQL

Understanding Azure Cosmos DB for NoSQL

This lesson explains the Cosmos DB resource model, partitioning, request units and the throughput options.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe accounts, databases, containers and items.
  • Explain how to choose a partition key.
  • Describe request units and throttling.
  • Compare provisioned, autoscale and serverless throughput.

The resource model

Azure Cosmos DB offers several APIs; the exam covers the NoSQL API, which stores schema-free JSON documents and queries them with a SQL-like language. Resources form a hierarchy. An account has an endpoint such as https://ACCOUNT.documents.azure.com, regions and a default consistency level. An account contains databases, which are namespaces; a database contains containers, which hold the data and are the unit of throughput and partitioning; a container contains items, the JSON documents.

Every item has an id that is unique within its partition key value, and the service adds system properties such as _ts (last modified time in Unix seconds) and _etag (a version identifier used for optimistic concurrency). Items in one container do not need the same shape, so a product container can hold items with different attributes, and new properties need no schema change. An item can be up to 2 MB.

Put in order

The Cosmos DB hierarchy

Exam skills 2.1.1

Arrange the Cosmos DB for NoSQL resources from the largest to the smallest.

Log in to try this activity Interactive activities are free with an account.

Partitioning

Every container has a partition key, a property path such as /customerId, chosen when the container is created and not changeable afterwards. Items with the same partition key value form a logical partition, which can hold up to 20 GB. The service groups logical partitions onto physical partitions, each served by a set of replicas, and adds physical partitions automatically as data and throughput grow.

The partition key decides scalability and cost. A good key has many distinct values, so that data and requests spread evenly, and it appears in the most frequent queries, so that they can be served by one partition. For an AI chat application, /userId or /sessionId fits well; a key such as /country with a few values concentrates traffic in hot partitions. Queries that include the partition key are single-partition queries; queries without it fan out to every physical partition and cost more.

The following diagram compares the candidate partition keys.

Candidate partition keys for chat history and their effect. /conversationId makes the most frequent query, loading a conversation, a single-partition query, and hierarchical keys of /userId then /conversationId also serve listing a user's conversations. /userId alone creates large, hot partitions for heavy users; /messageId makes every conversation load cross-partition; /date puts each day's writes in one hot partition.
The access patterns are illustrative.

When one property is not enough

When no single property has enough distinct values, a synthetic key that combines properties, or hierarchical partition keys with up to three levels such as tenant, then user, can spread data while keeping related items together.

Scenario questions

Choosing a partition key

Exam skills 2.1.1 · 2.1.2

Choose the best partition key for each container.

Log in to try this activity Interactive activities are free with an account.

Request units

Cosmos DB expresses the cost of every operation in request units (RUs), a normalized measure of CPU, memory and I/O. A point read of a 1 KB item by its id and partition key costs about 1 RU; writing the same item costs about 5 RUs or more, depending on indexing; queries cost more as they read and return more data. Each response reports its charge in the x-ms-request-charge header, which makes cost measurable during development.

Throughput is reserved or consumed in RUs per second. When requests exceed the available throughput, the service returns HTTP 429 Too Many Requests with a retry-after value. The SDKs retry 429 responses automatically a few times, so occasional throttling is harmless, but sustained throttling raises latency and eventually surfaces as errors.

Fill in the value

Request unit arithmetic

Exam skills 2.1.2

Estimate request unit costs from the figures in the lesson.

Log in to try this activity Interactive activities are free with an account.

Throughput options

Throughput can be configured in three ways, and the choice depends on how predictable the workload is.

Provisioned (manual)
How it worksA fixed RU/s is reserved and billed hourly
Best forSteady, predictable traffic
LimitsPays for idle capacity
Choose when Traffic is steady
Autoscale
How it worksRU/s scales between 10% and 100% of a set maximum
Best forVariable or unpredictable traffic with peaks
LimitsBilled for the highest RU/s reached each hour
Choose when Traffic varies but has a known peak
Serverless
How it worksNo reservation; each RU consumed is billed
Best forDevelopment, tests and spiky, low-volume apps
LimitsSingle region; lower per-container throughput and storage limits
Choose when Traffic is low or intermittent

Throughput can be set on a container, dedicating it to that container, or on a database, shared by its containers. Shared database throughput suits many small containers, but some features have restrictions on shared-throughput accounts; for example, vector indexing is not supported on accounts that use shared throughput. A Cosmos DB free tier account includes a limited amount of throughput and storage at no charge, which is useful for learning.

Sort into groups

Which throughput option?

Exam skills 2.1.2

Choose the throughput option that fits each workload.

Log in to try this activity Interactive activities are free with an account.
Worked example

Choosing a partition key for chat history

An AI assistant stores every message of every conversation in Cosmos DB. The most frequent operation is loading the last 50 messages of one conversation; the second is listing a user's conversations. There are millions of users, and some users have thousands of conversations. Which partition key should the container use?

/conversationId fits the most frequent query: loading a conversation's messages becomes a single-partition query, the key has millions of distinct values, and no logical partition approaches 20 GB. Listing a user's conversations can be served by a small separate container keyed by /userId, or by hierarchical partition keys of /userId then /conversationId, which keeps each user's data together while still spreading heavy users across physical partitions.

/userId alone would make the heaviest users' partitions grow large and hot. /messageId would spread data perfectly but make loading a conversation a cross-partition query every time. A key such as /date would put each day's writes into one hot partition.

Check yourself

What does an HTTP 429 response from Cosmos DB mean, and how do the SDKs react to it?

Show answer

The request exceeded the available throughput (RU/s). The SDKs retry 429 responses automatically a few times after the retry-after interval; sustained 429s require more throughput or cheaper operations.

Exam Tips

Remember for the exam

  • Account, database, container, item; the container is the unit of partitioning and throughput.
  • Choose a partition key with many values that appears in the most frequent queries; it cannot be changed later.
  • A 1 KB point read costs about 1 RU; HTTP 429 means throughput was exceeded.
  • Provisioned for steady load, autoscale for peaks, serverless for low or intermittent load.