Changing Partition Key for Azure Cosmos DB Container - Best Practices and Steps

Important Considerations When Changing Partition Key for Azure Cosmos DB Container

Question

You have an Azure Cosmos DB account named Account1. Account1 includes a database named DB1 that contains a container named Container1. The partition key for Container1 is set to /city.

You plan to change the partition key for Container1.

What should you do first?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

B

The good news is that there are two features, the Change Feed Processor and Bulk Executor Library, in Azure Cosmos DB that can be leveraged to achieve a live migration of your data from one container to another. This allows you to re-distribute your data to match the desired new partition key scheme, and make the relevant application changes afterwards, thus achieving the effect of "updating your partition key".

Incorrect Answers:

A: It is not possible to "update" your partition key in an existing container.

https://devblogs.microsoft.com/cosmosdb/how-to-change-your-partition-key/

If you plan to change the partition key for a container in Azure Cosmos DB, you should first create a new container with the new partition key and then migrate the data from the old container to the new container.

Therefore, the correct answer is B. Create a new container in DB1.

Here are the steps you should follow to change the partition key for Container1:

  1. Create a new container in DB1 with the new partition key.

  2. Use the Azure Cosmos DB SDK or Data Migration Tool to migrate the data from the old container to the new container.

  3. Update the application to use the new container and partition key.

  4. Delete the old container.

Note that regenerating the keys for Account1 (option A) has nothing to do with changing the partition key for a container. Regenerating the keys will create new primary and secondary keys that can be used to access the Cosmos DB account.

Deleting Container1 (option C) is not a recommended approach as it will result in data loss.

Implementing the Azure Cosmos DB .NET SDK (option D) is not necessary to change the partition key for a container. The SDK is used to interact with Cosmos DB and perform operations on the data.