Skip to content

Commit

Permalink
Adding write performance optimization section to Cosmos DB state stor… (
Browse files Browse the repository at this point in the history
#3934)

* Adding write performance optimization section to Cosmos DB state store documentation

Signed-off-by: Simon Headley <[email protected]>

* Applying suggestions as per PR review

Signed-off-by: Simon Headley <[email protected]>

* Switching from Markdown note to {{% alert %}} {$ /alert $}} as per PR review

Signed-off-by: Simon Headley <[email protected]>

* Putting example paragraph in a new line as per review

Signed-off-by: Simon Headley <[email protected]>

---------

Signed-off-by: Simon Headley <[email protected]>
Co-authored-by: Mark Fussell <[email protected]>
  • Loading branch information
KrylixZA and msfussell authored Jan 9, 2024
1 parent 174c510 commit 5c132e6
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,42 @@ az cosmosdb sql role assignment create \
--role-definition-id "$ROLE_ID"
```

## Optimizing Cosmos DB for bulk operation write performance

If you are building a system that only ever reads data from Cosmos DB via key (`id`), which is the default Dapr behavior when using the state management API or actors, there are ways you can optimize Cosmos DB for improved write speeds. This is done by excluding all paths from indexing. By default, Cosmos DB indexes all fields inside of a document. On systems that are write-heavy and run little-to-no queries on values within a document, this indexing policy slows down the time it takes to write or update a document in Cosmos DB. This is exacerbated in high-volume systems.

For example, the default Terraform definition for a Cosmos SQL container indexing reads as follows:

```tf
indexing_policy {
indexing_mode = "consistent"
included_path {
path = "/*"
}
}
```

It is possible to force Cosmos DB to only index the `id` and `partitionKey` fields by excluding all other fields from indexing. This can be done by updating the above to read as follows:

```tf
indexing_policy {
# This could also be set to "none" if you are using the container purely as a key-value store. This may be applicable if your container is only going to be used as a distributed cache.
indexing_mode = "consistent"
# Note that included_path has been replaced with excluded_path
excluded_path {
path = "/*"
}
}
```

{{% alert title="Note" color="primary" %}}

This optimization comes at the cost of queries against fields inside of documents within the state store. This would likely impact any stored procedures or SQL queries defined and executed. It is only recommended that this optimization be applied only if you are using the Dapr State Management API or Dapr Actors to interact with Cosmos DB.

{{% /alert %}}

## Related links

- [Basic schema for a Dapr component]({{< ref component-schema >}})
Expand Down

0 comments on commit 5c132e6

Please sign in to comment.