Microsoft Certified: Azure Developer Associate (AZ-204)Develop for Azure storageHard
A developer is creating an application that stores customer order data in Azure Cosmos DB. Each order document contains an 'orderId', 'customerId', and 'orderDate'. The application frequently queries orders by 'customerId' and 'orderDate' to display historical purchases, and also occasionally queries by 'orderId' for specific order details. To optimize query performance and distribution, which primary key (partition key and item ID) strategy should be implemented?
- APartition Key: /orderDate, Item ID: /orderId
- BPartition Key: /orderId, Item ID: /customerId
- CPartition Key: /customerId, Item ID: /orderId
- DPartition Key: /orderId, Item ID: /orderDate
Show answer & explanationAnswer & explanation
Correct answer: C. Partition Key: /customerId, Item ID: /orderId
Partitioning by '/customerId' ensures that all orders for a single customer are stored together on the same logical partition. This optimizes common queries by 'customerId' and 'orderDate' because these queries can be served from a single partition, reducing cross-partition queries. Using '/orderId' as the Item ID provides a unique identifier within each customer's partition.
Why the other options are wrong
- A. Partitioning by '/orderDate' would spread a customer's orders across many partitions, making 'customerId' queries inefficient. Item ID should be unique.
- B. Partitioning by '/orderId' would create a new partition for almost every order, leading to very poor distribution and hot partitions for popular customers. Item ID should be unique within the partition.
- D. Partitioning by '/orderId' is generally inefficient as it creates many single-item partitions. Item ID should be unique within the partition.
Cosmos DB Partitioning Strategy
The choice of partition key and item ID to distribute data across logical and physical partitions for optimal performance, scalability, and cost.
- Partition key determines logical partition for data distribution.
- Item ID (document ID) must be unique within a logical partition.
- Choose partition key based on frequently accessed query patterns to minimize cross-partition queries.
Memory trick: Partitioning is like organizing a library: group similar books for easy finding.