The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +772K followers.

Follow publication

Member-only story

Data Modeling and Partitioning Best Practice in Partitioned Repository Pattern With Cosmos DB

Discuss Data Modeling and Partitioning Best Practice for Cosmos DB and its implementation using Partitioned Repository Pattern with ASP.NET Core.

Shawn Shi
The Startup
Published in
8 min readNov 29, 2020

UPDATE April 10, 2022: all projects in the GitHub repo have been upgraded to .NET 5.

Even though Cosmos DB is schema-free and supports storing and querying unstructured data, it is still critical to spend good efforts in the planning phase on data modeling and partitioning. Particularly, poorly designed data models and partition keys would hurt the performance and also restrict the scalability.

In this article, we will discuss:

  • how partitioning and logical partitions work in Cosmos DB
  • how data is stored in different logical partitions in Cosmos DB
  • how data is read from Cosmos DB, including Point Reads, Single-partition Query and Cross-partition Query
  • how to design partition keys for better performance
  • how partitioning can be implemented in Partitioned Repository Pattern using .NET SDK
Image by Author

How is data stored in different partitions in a Cosmos DB container?

If you come from a SQL background, here is a quick table to compare the terminology between a SQL DB and Cosmos DB.

Image by Author

For every container, a partition key must be pre-defined. The partition key is used to divide a container into many subsets, AKA logical partitions, and each logical partition is used to store items with the same partition key values. An example will make this much more straightforward.

For example, if a Todo list container has a partition key of “/Category”, the following todo items for grocery category will be stored in one logical partition:

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

The Startup
The Startup

Published in The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +772K followers.

Shawn Shi
Shawn Shi

Written by Shawn Shi

Senior Software Engineer at Microsoft. Ex-Machine Learning Engineer. When I am not building applications, I am playing with my kids or outside rock climbing!

Responses (1)

Write a response