Azure Cosmos DB Documentation

Microsoft Docs

Introduction to Partitioning in Azure Cosmos DB

Azure Cosmos DB is a globally distributed, multi-model database service. To achieve its high availability, massive scalability, and low latency, it employs a sophisticated partitioning model. Understanding how partitioning works is crucial for optimizing the performance and cost-effectiveness of your Cosmos DB solutions.

Partitioning, also known as sharding, is the process of horizontally scaling your database. Data is divided into smaller, more manageable chunks called partitions. Each partition is hosted on a set of dedicated storage and compute resources. Cosmos DB automatically manages the partitioning and rebalancing of data across these physical resources.

Benefits of Partitioning:

Choosing a Partition Key

The partition key is the most critical element in designing a scalable and performant Cosmos DB solution. It's a property within your items (documents) that Cosmos DB uses to determine which partition an item belongs to. A good partition key distributes requests and data evenly across all logical partitions, preventing hot partitions.

Characteristics of a Good Partition Key:

Important Consideration:

The partition key is immutable once chosen. You cannot change it after creating your container. Therefore, careful planning is essential.

Partitioning Strategies

Cosmos DB supports various APIs, and the partitioning strategy can depend on the API you choose. However, the core concepts of choosing a partition key remain consistent.

Common Partitioning Strategies:

Understanding Physical Partitions

Cosmos DB automatically manages physical partitions. A physical partition is a unit of physical storage and throughput. The number of physical partitions is determined by Cosmos DB based on your throughput provisioned and the amount of data stored. Cosmos DB dynamically scales the number of physical partitions to accommodate your workload.

Note:

You don't directly manage physical partitions. Cosmos DB handles their creation, deletion, and rebalancing.

Understanding Logical Partitions

Logical partitions are the fundamental unit of scaling in Cosmos DB. All items that share the same partition key value belong to the same logical partition. A single physical partition can host multiple logical partitions. Cosmos DB aims to distribute logical partitions evenly across physical partitions.

Logical Partition Size Limit:

Each logical partition has a maximum storage limit of 20 GB and a maximum throughput limit of 10,000 Request Units per second (RU/s). If a logical partition exceeds either of these limits, Cosmos DB will automatically split it into new logical partitions.

Best Practices for Partitioning

Adhering to these best practices will help you maximize the benefits of Cosmos DB partitioning:

  1. Choose partition keys wisely: This is the single most important factor. Analyze your data and query patterns before selecting a partition key.
  2. Avoid hot partitions: A hot partition occurs when a disproportionate amount of traffic is directed to a single partition, leading to throttling and poor performance. This is usually a symptom of a poorly chosen partition key.
  3. Utilize high cardinality keys: Keys with many unique values are generally better for distributing data.
  4. Consider your query patterns: Design your partition key to align with your most frequent and performance-critical queries.
  5. Understand logical partition limits: Be aware of the 20 GB storage and 10,000 RU/s limits per logical partition.
  6. Monitor your partitions: Use Azure Monitor to track request units per partition and identify potential hot spots.
  7. Iterate if necessary: While changing a partition key is not straightforward, it's sometimes necessary to re-evaluate and potentially migrate data to a new container with a better partition key strategy.

Effective partitioning is key to unlocking the full potential of Azure Cosmos DB. By carefully considering your data, access patterns, and the characteristics of partition keys, you can build highly scalable, performant, and available applications.