Lesson 2 of 3 Semi-Structured Data Solutions

Designing Cosmos DB Partition Keys and Capacity

This lesson explains request units, partition keys and capacity modes.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain request units and rate limiting.
  • Identify the characteristics of a good partition key.
  • Choose a capacity mode for a traffic pattern.

Request units

Cosmos DB normalizes the cost of operations into request units (RUs). Reading a 1 KB item by its ID and partition key costs about 1 RU, while writes and queries cost more, according to the size of items and the complexity of the query. Throughput is expressed as RUs per second (RU/s). If an application exceeds its provisioned throughput, Cosmos DB rate limits requests with an HTTP 429 status, and clients retry after a stated delay.

Thinking in RUs lets an architect estimate capacity: reading 1 KB items 1,000 times a second needs about 1,000 RU/s. Point reads are by far the cheapest operation, which is why good data models arrange for the most common reads to be point reads.

Fill in the value

Estimating RU/s

Exam skills 2.2.1

Estimate throughput from request unit costs.

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

Partition keys

Each container has a partition key, a property whose value assigns items to logical partitions. All items sharing a value form one logical partition, which can hold up to 20 GB. Cosmos DB maps logical partitions onto physical partitions, each providing up to 10,000 RU/s and 50 GB, and it adds physical partitions automatically as data and throughput grow.

Because throughput is divided across physical partitions, a key that concentrates traffic on one value creates a hot partition. That partition can be rate limited even though the container's total capacity appears sufficient. A good partition key has the following characteristics.

  • High cardinality: many distinct values, so data can spread widely. A user ID is usually better than a country.
  • Even distribution: requests and storage are spread across values, not concentrated on a few.
  • Used in common queries: queries that include the key go to one partition. Queries that omit it fan out to all partitions, costing more.
  • Stable: the value does not change during the life of the item, because changing it means rewriting the item.

Hierarchical partition keys allow up to three levels, such as tenant, then user, then session. They help a multi-tenant application in which a large tenant would otherwise exceed the 20 GB limit of a single logical partition, while queries for one tenant still target a limited subset of partitions. Transactions in stored procedures and batches apply within one logical partition, which also influences key choice.

The following diagram compares two candidate partition keys for a Cosmos DB container that receives frequent writes from 40,000 vehicles.

Choosing the partition key for 40,000 vehicles writing every 10 seconds. The delivery date puts all of today's writes in one logical partition, a hot partition that also hits the 20 GB limit. The vehicle ID has high cardinality, spreads writes evenly and matches the main query, recent events for one vehicle.
The scenario is illustrative.
Sort into groups

Good partition key?

Exam skills 2.2.1

Judge each candidate partition key for a high-traffic container.

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

Capacity modes

Three ways of paying for throughput suit different traffic patterns.

Provisioned throughput

Provisioned throughput reserves a fixed number of RU/s, billed by the hour, whether used or not. It suits steady, predictable workloads, and reserved capacity can reduce its cost substantially.

Autoscale

Autoscale sets a maximum RU/s, and the system scales between 10% of the maximum and the maximum according to demand. Billing is based on the highest level reached in each hour. It suits variable workloads that have real peaks but cannot tolerate rate limiting.

Serverless

Serverless charges for the RUs actually consumed, with nothing provisioned. It suits intermittent or low-traffic workloads, such as development or a small application with long idle periods. A serverless account runs in a single region only, and it lacks the throughput guarantees of the other modes.

The following comparison recaps the capacity modes.

Provisioned
Billing basisRU/s reserved
Traffic patternSteady
Multiple regionsYes
Choose when Predictable load
Autoscale
Billing basisHighest RU/s reached per hour
Traffic patternVariable with peaks
Multiple regionsYes
Choose when Unpredictable production load
Serverless
Billing basisRUs consumed
Traffic patternIntermittent
Multiple regionsNo
Choose when Small or sporadic workloads
Sort into groups

Choose the capacity mode

Exam skills 2.2.1

Match each workload with the Cosmos DB capacity mode that suits it.

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

Reducing cost and latency

Several other options reduce the RU bill. A custom indexing policy can exclude paths that are never queried, which lowers write cost, because each indexed property adds work for each write. A time to live setting removes old items automatically. The free tier gives 1,000 RU/s and 25 GB at no charge on one account per subscription. The change feed offers a stream of changes, which suits event-driven processing without polling the container.

Match the pairs

Cost levers in Cosmos DB

Exam skills 2.2.1

Match each Cosmos DB feature with how it reduces cost or effort.

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

Choosing a partition key

A logistics company stores delivery events in Cosmos DB. It has 40,000 vehicles, and each vehicle writes events every 10 seconds. Most queries retrieve recent events for one vehicle. Traffic varies by time of day, with a large peak in the early morning. An engineer proposes the delivery date as the partition key. What should you recommend?

The date is a poor key. All of today's writes would land in a single logical partition, creating a hot partition, and the 20 GB limit would soon apply. The vehicle ID has high cardinality, spreads writes evenly, and appears in most queries.

A vehicle could accumulate more than 20 GB over time, so a hierarchical key of vehicle ID and month, or a time to live on old events, should be added. Because traffic has a large daily peak, autoscale is the appropriate capacity mode. Serverless would be poor at this constant volume, and provisioned capacity sized for the peak would waste money for the rest of the day.

Check yourself

A container's total RU/s appears sufficient, yet clients receive HTTP 429. What is the likely cause?

Show answer

A hot partition: throughput is divided across physical partitions, and traffic is concentrated on one.

Exam Tips

Remember for the exam

  • A logical partition holds up to 20 GB; a physical partition serves up to 10,000 RU/s and 50 GB.
  • A partition key needs high cardinality, even distribution, query use and stability.
  • HTTP 429 means rate limiting, often from a hot partition.
  • Provisioned for steady traffic; autoscale for peaks; serverless for sporadic use in one region.