Launch price: every exam for $29.99 a year, for the first 300 buyers. See the price

CertStreak
Menu

Design and select partition keys based on cardinality, access patterns, and write distribution requirements

A worked example

Single answer

Contoso is creating an Azure Cosmos DB for NoSQL container named orders for its storefront. Each order is stored as one item. The following facts apply:

  • About 2 million customers place orders, and the largest customer's order history is under 50 MB.
  • Orders are sold in 12 countries. Orders from one country are projected to reach about 300 GB within three years.
  • The customerId and countryCode values on an order never change.
  • The orderStatus property changes from Open to Shipped to Delivered during an order's life.
  • The order-tracking page is the highest-frequency operation. It runs the query in the exhibit with both parameter values supplied.

The partition key must keep the order-tracking query on a single logical partition, must use a property whose value never changes after the item is created, and must not let any single key value approach the logical partition storage limit.

SELECT * FROM c WHERE c.countryCode = @countryCode AND c.customerId = @customerId AND c.orderStatus = 'Open'

Which partition key path should you choose for the orders container?

Choose one answer
Why

Each candidate must satisfy three criteria at once: presence in the hottest equality filter, immutability, and per-value storage well below the 20 GB logical partition ceiling. The item ID meets the last two but is absent from the query, so the query fans out. The order status is in the query but changes during the order's life, and partition key values cannot be updated in place. The country code is in the query and stable, but one country's projected 300 GB far exceeds what a single logical partition can hold, whereas customerId meets all three requirements.

  • Not correct: /id. The item ID is high cardinality and never changes, but the order-tracking query does not filter on it, so the query fans out to every physical partition.
  • Not correct: /countryCode. The country code is in the filter and does not change, but one logical partition holds up to 20 GB, and writes for a value that reaches that ceiling stop, so the largest country would hit the limit.
  • Not correct: /orderStatus. The status is in the query filter, but partition key values cannot be updated in place; moving an order to a new status would require creating a new item and deleting the old one, which is not atomic across logical partitions.
  • Correct: /customerId. customerId is an equality filter in the hottest query, so that query routes to one logical partition; a customer's ID on an order does not change, and the largest customer's data is far below the 20 GB per-value ceiling.

Practise this topic until it's automatic.

Ten questions a day, every answer explained, and misses that come back until you beat them.

Start free