What is Partition Key?

  • Azure Cosmos DB provides you the opportunity to store a huge amount of that.​

  • When there is a provision to store a huge amount of data if you are not thinking of storing the data in a proper manner then while querying the data from the huge amount, the performance will definitely go down.​

  • Azure Cosmos DB provides you with the concept of partitioning where the items in a container are group into different partitions or you can also call those as unique subsets.​


What is Partition Key?​

  • Partition key plays an important role here. It is associated with each of the items inside a container and based on it’s value, the subsets or the partitions are formed. Another important point to remember here is every items inside a specific container have the same partition key.​
  • The partition key is nothing but a JSON property that is responsible for distributing data among different partitions.​
  • Behiend the scene, Partition key only decides where to place which document.​
  • Another important point to remember here is, once you set the partition key, it’s not possible to change the Partition key again.​
  • It is suggested to keep a partition key with many distinct values.​


Logical and Physical Partitions

Logical Partition​

  • The items present inside the containers are divided into logical partitions.​
  • Logical partitions are again based on the Partition key which is associated with all the items inside the container.​
  • Partition key value is same for all the items that are present in a logical partition.​
  • As of now, a logical partition can have a limit to 20 GB.​


Physical Partition​

  • If you are thinking of the physical partitions, actually, behind the scene, these logical partitions are mapped to physical partitions and basically one or more than one logical partitions are mapped to one physical partition.​
  • As of now, each physical partition can store up to 50 GB of data and can able to provide throughput up to 10,000 RU/s.
  • Once the physical partition limit reaches 50 GB, immediately, Azure Cosmos Db creates another brand new physical partition.


How Does Partition Work exactly?

  • Initially, one default partition gets created by Azure.​
  • Then, when you are trying to insert a new document, based on the Partition key-value, Azure cosmos DB decides in which partition, it has to store the item.​
  • In case, there are more items and it reached the maximum size of the partition, immediately, Azure creates a brand new physical partition and moves that particular logical partition to that brand new physical partition.​
  • For better query performance, you can provide a partition key.


​Cosmos DB Partition key Best Practices

  • Below are a few lists of best practices that you need to consider while choosing the partition key in Azure Cosmos DB.​
  • An item ID in a container is one of the best choice as the partition key because it is unique and has a wide range of possible values and no chance of duplication.​
  • You should choose a partition key that must contain a wide range of distinct values.​
  • The value of the partition key shouldn’t change.​
  • It should have spread throughput across logical partitions.​
  • Choose the one that can be used with your queries as filter condition.​
  • Primary key also can be used as one of the best choice for the Partition key.​