A media streaming application stores user watch history in an Azure Cosmos DB Core (SQL) API container. The database must handle millions of active users who update their viewing progress every 30 seconds. The application must support transactional batch operations to update a user's viewing progress across multiple shows atomically. The most common query retrieves the complete viewing history for a single user to display on their dashboard.
Which partition key should you configure for the container to satisfy these requirements and avoid hot partitions?
- userIdAnswer
- BvideoId
- Cdate
- DuserId_videoId
Answer
userId
Selecting the user identifier is the correct choice. It provides high cardinality across millions of users, ensuring that data and throughput requests are evenly distributed across physical partitions. Furthermore, it colocates all viewing history for any single user within the same logical partition. This colocation makes the primary query for a user's dashboard a single-partition query and enables transactional batch operations, which require all involved documents to share the same partition key.
Step-by-Step Solution
Key Concept
Selecting a partition key in Azure Cosmos DB to distribute workload and storage, while supporting transactional boundaries and preventing hot partitions.