Skip to content
Related Articles

Related Articles

Microsoft Azure – Choosing a Partition Key in Cosmos DB

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 16 Mar, 2022

Azure Cosmos DB makes use of partitioning to scale individual containers in a database subsequently enhancing the performance needs of your application. In partitioning, the items in a container are divided into distinct subsets called logical partitions. Logical partitions are created based on the value of a partition key that is linked with each item inside a container. All the items in a logical partition have the same partition key value. 

In this article, we will look into how to choose a partition key in Azure Cosmos DB. To optimize the scalability and performance of Cosmos DB, you need to choose the right partition key for your container.

Firstly, look for a container, for instance, that has a partition key for the property firstname.

Let’s see what that looks like in the data. It is this property, firstname. This is a good key because it is in every document in the container and it has a wide variety of possible values. We can’t change the partition key for this container anymore. You can only set it when you create a new container. 

If you do need to change a partition key, you need to create a new container and migrate your data to that one. 

A partition key consists of a path. This can be something like /firstname or /name/first, or a nested property, as long as it is a JSON property from the documents in the container. 

The JSON property will have a value like Evelyn. A good key has a value for its property in every document in the container. Like, it could be firstname or the built-in property ID, which is auto-generated and available in every document.

Here, are some best practices to look into before choosing the right partition key:

  1. The value of the key shouldn’t change. The last name might not be a good one as that might change when people get married or divorced.
  2. The key should have a large range of values. The first name will be different for many people, and something like ID works very well when a unique value is used. This optimizes the use of partitions and enhances performance.
  3. A partition key can have values of string or numeric types, and once you’ve created a key for a container, you can’t change it anymore.

Note: There’s one exception. If your container is large and read-heavy and large, that means it has 30,000 or more RUs assigned to it or it is larger than 100 gigabytes.

If that’s the case, then the partition key should be something that your queries filter on a lot. If your queries filter on user Id a lot, that might be a great partition key. When you choose the right partition key for your Azure Cosmos DB container, you optimize performance. 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!