Throughput Configuration - (CosmosDB Partitioning and Throughput)
Syntax
To configure throughput for CosmosDB, you can follow the below syntax:
await client.CreateDatabaseIfNotExistsAsync(new Database { Id = "myDatabase" });
await client.CreateContainerIfNotExistsAsync(UriFactory.CreateDatabaseUri("myDatabase"),
new Container { Id = "myContainer", PartitionKeyPath = "/myPartitionKey" },
throughput: 400);
Example
Consider a scenario where we have a web application that captures user data and stores it in CosmosDB. Based on the growth of user data, we may need to adjust the throughput capacity. In such cases, we can use the CreateContainerIfNotExistsAsync
method to create a container with the desired throughput capacity.
private static async Task CreateContainerWithThroughput()
{
string connectionString = "your_connection_string_here";
string databaseName = "myDatabase";
string containerName = "myContainer";
CosmosClient client = new CosmosClient(connectionString);
DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName);
ContainerResponse container = await client.CreateContainerIfNotExistsAsync(
databaseName,
containerName,
$"/{partitionKey}",
throughput: 10000 // updating the throughput capacity
);
}
Output
On executing the above example, the CosmosDB container will be created with the specified throughput capacity.
Explanation
In CosmosDB, throughput is the provisioned capacity for read and write operations on a container. To configure throughput capacity for CosmosDB, you can use the CreateContainerIfNotExistsAsync
method. In this method, you can specify the desired throughput capacity, in terms of request units (RU) per second. Request units are the measure of throughput in CosmosDB, and each operation is assigned a certain amount of request units, depending on its complexity.
Use
Throughput configuration is an important aspect of performance tuning in CosmosDB. By adjusting the throughput capacity, you can optimize the performance of your CosmosDB application. By default, CosmosDB allocates a minimum of 400 RU/s, but you can scale the throughput capacity up to any desired level. You can monitor the resource utilization and adjust the throughput capacity accordingly, to ensure optimal performance of your CosmosDB application.
Important Points
- CosmosDB throughput capacity can be adjusted using the
CreateContainerIfNotExistsAsync
method. - Throughput capacity is measured in request units (RU) per second.
- Each operation in CosmosDB is assigned a certain number of request units, depending on its complexity.
- By adjusting the throughput capacity, you can optimize the performance of your CosmosDB application.
Summary
In this tutorial, we have discussed the process of configuring throughput capacity for CosmosDB. By adjusting the throughput capacity, you can optimize the performance of your CosmosDB application and ensure that it meets the desired level of scalability and performance.