Microsoft Certified: Azure Developer Associate (AZ-204)Develop for Azure storageMedium
A financial application stores transaction details in Azure Cosmos DB. Each transaction has a unique TransactionID and is associated with a CustomerID. Queries frequently involve retrieving all transactions for a given CustomerID and occasionally retrieving a specific transaction by its TransactionID. To optimize query performance and distribution, how should you configure the partitioning strategy for the Cosmos DB container?
- APartition by CustomerID.
- BPartition by a hash of TransactionID and CustomerID.
- CPartition by a composite key of CustomerID and TransactionID.
- DPartition by TransactionID.
Show answer & explanationAnswer & explanation
Correct answer: A. Partition by CustomerID.
Partitioning by CustomerID is best because it supports efficient retrieval of all transactions for a customer, which is a frequent query. While TransactionID queries are occasional, they can still be served efficiently with a filter.
Why the other options are wrong
- B. A hash of both would make both types of queries inefficient as it wouldn't group transactions by CustomerID and wouldn't allow direct lookup by TransactionID without knowing the CustomerID hash.
- C. A composite key would be less efficient for 'all transactions for a customer' queries as it would still require scanning values within a partition key range, and might not evenly distribute writes.
- D. Partitioning by TransactionID would scatter transactions for a single customer across many partitions, making 'all transactions for a customer' queries inefficient.
Cosmos DB Partitioning
The process of dividing data into smaller, more manageable logical and physical partitions based on a partition key to optimize data distribution and query performance.
- Essential for scalability and performance.
- Choosing the right partition key is crucial.
- Impacts cost and throughput (RU/s).
Memory trick: Partitioning is like organizing a library: put similar books together for easy finding.