Azure Cosmos DB Synthetic Partition Key

In previous post, we discussed about querying Cosmos DB containers using partition keys. It is especially important to select a good partition key that distributes the data evenly across multiple partitions. However, if there is no suitable column with properly distributed values, we can create a Synthetic Partition Key. There are three ways to create a synthetic partition key:

  1. Concatenate Properties– We can combine multiple property values to form a synthetic partition key. For example, suppose we have the following document:
"deviceId": "ade-123",
"date": 2020

The two properties deviceId and date can be combined into a synthetic primary key as shown below:

"deviceId": "ade-123",
"date": 2020,
"partitionKey": "ade-123-2020"

2. Random Suffix: In this method, a random number is added to the end of the partition key value. For example, if a partition key is of date datatype and we choose a random number between 1 and 500. After random concatenation, the date values arranged in ascending order will look like : 2020-07-05.1, 2020-07-05.2, 2020-07-05.3 and so on. Since we chose a random number, the write operations for a given date will be evenly distributed across partitions.

3. Pre-calculated Suffix: Using the above random suffix method makes the write operations faster but it doesn’t improve the read performance, since we do not know which partition to go to when we want to query a specific value.

To improve the read performance, we can use the pre-calculated suffix strategy. For example, if we have a partition key with date values and we have Vehicle Identification Number (VIN) which is frequently used to query the data. We can use a hash function that uses the VIN as input and returns a number within a range, say between 1 and 500. We can then use this random number as a suffix to the date value for creating the synthetic partition key. This way when we get a specific VIN value in the query, we can use the hash function to determine the partition suffix and go straight to the partition containing the particular VIN value.


2 thoughts on “Azure Cosmos DB Synthetic Partition Key

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: