How to decide the primary key on CosmosDB ?

HimanshuSinha 19,527 Reputation points Microsoft Employee Moderator
2025-10-24T04:37:28.7966667+00:00

Hello ,How to decide the primary key on CosmosDB

Thanks
Himanshu

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anderson Lopes 75 Reputation points Microsoft Employee
    2025-10-27T16:16:40.6433333+00:00

    In Cosmos DB, the primary key is defined by the partition key and the unique ID (id) of each document:

    • id: A unique identifier for each item in a container.
    • partition key: Determines how data is distributed across partitions.

    ✅ How to Choose the Right Partition Key

    Understand Your Access Patterns

    • Choose a key that is frequently used in queries and filters.
      • Example: If you're building a marketplace like Trampo Square, and users often query by userId, then userId is a good candidate.
      Ensure High Cardinality
      - The partition key should have **many unique values** to avoid hot partitions.
      
         - Avoid keys like `country` or `status` if they have few distinct values.
      
         **Balance Read/Write Load**
      
            - Distribute traffic evenly across partitions.
      
               - If one key (e.g., `productId`) gets 90% of traffic, it will cause throttling.
      
               **Avoid Large Items in One Partition**
      
                  - Cosmos DB limits partition size to **20 GB**.
      
                     - If all items with the same key exceed this, you’ll hit limits.
      
    1. Consider Composite Keys (if needed)
      • You can simulate composite keys by concatenating values (e.g., userId|orderId).In Cosmos DB, the primary key is defined by the partition key and the unique ID (id) of each document:
      • id: A unique identifier for each item in a container.
      • partition key: Determines how data is distributed across partitions.
      ✅ How to Choose the Right Partition Key
      1. Understand Your Access Patterns
        • Choose a key that is frequently used in queries and filters.
        • Example: If you're building a marketplace like Trampo Square, and users often query by userId, then userId is a good candidate.
      2. Ensure High Cardinality
        • The partition key should have many unique values to avoid hot partitions.
        • Avoid keys like country or status if they have few distinct values.
      3. Balance Read/Write Load
        • Distribute traffic evenly across partitions.
        • If one key (e.g., productId) gets 90% of traffic, it will cause throttling.
      4. Avoid Large Items in One Partition
        • Cosmos DB limits partition size to 20 GB.
        • If all items with the same key exceed this, you’ll hit limits.
      5. Consider Composite Keys (if needed)
        • You can simulate composite keys by concatenating values (e.g., userId|orderId).
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.