Azure Cosmos DB Documentation

Azure Cosmos DB: Partition Key Best Practices

Choosing the right partition key is crucial for the performance, scalability, and cost-effectiveness of your Azure Cosmos DB solution. A well-designed partition key distributes your data and request load evenly across physical partitions.

Understanding the Goal

The primary goals of a good partition key are:

Key Characteristics of a Good Partition Key

A good partition key typically has:

Common Partition Key Anti-Patterns to Avoid

Important: Cosmos DB has a default maximum of 100 GB per physical partition and 10,000 RU/s per physical partition. Exceeding these limits will cause issues.

Strategies for Choosing a Partition Key

1. Leverage Existing Properties with High Cardinality

Look for properties within your documents that naturally have a wide variety of values. Common examples include:

2. Composite Partition Keys

If a single property doesn't offer enough cardinality, consider combining two properties into a composite partition key. This is achieved by concatenating their values (often with a delimiter).

Example: Composite Partition Key (User + Date)


// Example data
{
    "id": "doc1",
    "userId": "user123",
    "eventDate": "2023-10-27T10:00:00Z",
    "eventType": "login",
    "details": "..."
}

// Composite partition key value: "user123#2023-10-27"
// This distributes data by user and then by date for that user.
                

3. Use a "Synthetic" Partition Key

If no suitable property exists, you can create a synthetic property that provides good distribution. This might involve hashing a value or using a combination of properties.

4. Partitioning for Specific Query Patterns

Design your partition key based on your most frequent and critical query patterns. If you often query by user ID and then by date, a composite key like userId#date can be effective.

Best Practices Summary

Migrating Partition Keys

Changing a partition key after data has been inserted is a complex operation. It typically involves creating a new container with the desired partition key, copying data from the old container to the new one, and then deleting the old container. Azure Cosmos DB provides tools and guidance for this process.

For detailed information and advanced scenarios, please refer to the official Azure Cosmos DB documentation on partitioning.