Thursday, August 22, 2019

Synthetic Partition Keys in Azure Cosmos DB


     Picking the partition for a container is one of the important decisions you need to make in CosmosDB. You want your logical partitions to be small and balanced. To do that, you need to pick a property that has unique values. Sometimes, data might not have a property like that. CosmosDB does not support multiple partition keys per container. What do you do then?

     How about generating your own partition key by concatenating multiple property values? This type of partition keys called synthetic partition keys. You need to concatenate multiple properties in client side (SDK, Rest API, Data Migration Tool) and add that value into a property which configured as partition key.  This is the half of the solution, if you want to use synthetic partition keys in your data, you want to be sure that you can generate this custom partition key to retrieve data too!

     Let's look at the following example. I generated a custom partition key by combining two property values.


"sensorId":"100020",
"sensorType":1
"partitionKey: "100020-1"

}

     partitionKey is a property that I created by combining values of SensorId and SensorType values. This might work great to distribute data evenly, but you might be in trouble when you try to read or query this data if you don't know any part of the partitionkey. Your data might be distributed well but your queries will cost a lot of Request Units if you cannot generate the partitionkey to read data.

No comments:

Post a Comment