Cosmos Db is not a relational database, but we can use it for relational data, for example, eCommerce data.
A few key patterns for successful data modeling in Cosmos DB are
- Select partition key wisely: Consider max document size, max partition size, and the most common query pattern. Choose the partition to let the query be point read or query within the single partition.
- Use denormalization: Store id and name together in the same document. In relational DB, names usually stored in the reference tables separately. But, in document DB, it is better to store it together even though it is a repetition. Because it can reduce the need to join, which is expensive in Cosmos DB.
- Sync denormalized value, e.g. name: Use Cosmos DB change feed feature, which will work as a trigger in relational DB and update all the reference data when name changes. A flow will be like this. Cosmos change feed -> Azure Function -> Update relevant data.
- Share a partition among data objects: For example, put all reference tables in the same container and share the partition called ‘type’, which will differentiate among reference tables.
– single document size 2MB
– single logical partiion size 20GB
Data Model for eCommerce
– customerId // partition key, same value as Customer.id
– type: customer
– customerId // partition key, save value as Customer.customerId. Customer and SalesOrder shares partition key in the same container.
– type: salesorder // use type field to differentiate Customer and SalesOrder model.
– Tag // embed for M to M relation. Change of tag name can be updated by Cosmos DB change feed to Azure function app.
– type: Category // partition key. reference/lookup data can be put in the same partition key to be read at a low cost.
– type: tag // partition key. ProductionCategory and ProductKey share the partition key and placed it in the same container.
Full details of the Cosmos DB data modeling lecture at Pluralsight can be found at the link.