Concepts
When working with Microsoft Azure Cosmos DB, you have the flexibility to choose between two types of provisioned throughput: container-level and database-level. In this article, we will focus on understanding when to use database-level provisioned throughput and how it can benefit your native applications.
Azure Cosmos DB Overview
Azure Cosmos DB is a globally distributed, multi-model database service that provides automatic scaling, high availability, and low latency. It offers various APIs to cater to different application needs, such as SQL API, MongoDB API, Cassandra API, Gremlin API, and Table API. Regardless of the chosen API, you can control the throughput at both the database and container levels.
Database-Level Provisioned Throughput
Database-level provisioned throughput allows you to allocate and manage throughput at the database level, rather than at the individual container level. This means that all containers within a database share the provisioned throughput. So, when should you consider using database-level provisioned throughput?
1. Cost-Efficiency:
If you have multiple containers within a database that experience varying workload patterns, using database-level provisioned throughput can be cost-efficient. Instead of provisioning throughput for each individual container, you can allocate a higher overall throughput at the database level and let the containers share it based on their varying usage patterns. This approach can help you optimize costs while still providing sufficient performance.
2. Shared Access Patterns:
Database-level provisioned throughput is suitable when your containers have similar access patterns or when their request rates and sizes are highly correlated. By having a shared throughput, Azure Cosmos DB can efficiently allocate resources based on the overall demand across your containers. This can lead to better resource utilization and improved performance for your application.
Using Database-Level Provisioned Throughput
To use database-level provisioned throughput, you need to create a database with provisioned throughput and add containers to it. Let’s take a look at an example of how to accomplish this using the SQL API and C#.
using Microsoft.Azure.Cosmos;
string endpointUri = "your_endpoint_uri";
string primaryKey = "your_primary_key";
CosmosClientOptions options = new CosmosClientOptions
{
ConnectionMode = ConnectionMode.Gateway
};
CosmosClient client = new CosmosClient(endpointUri, primaryKey, options);
string databaseId = "your_database_id";
string containerId1 = "your_container_id1";
string containerId2 = "your_container_id2";
// Create the database with provisioned throughput
await client.CreateDatabaseAsync(databaseId, ThroughputProperties.CreateManualThroughput(10000));
Database database = client.GetDatabase(databaseId);
// Create the containers within the database
await database.CreateContainerAsync(containerId1, "/partitionKey", ThroughputProperties.CreateAutoscaleThroughput(4000));
await database.CreateContainerAsync(containerId2, "/partitionKey", ThroughputProperties.CreateAutoscaleThroughput(6000));
In the above code, we first create a CosmosClient
instance by providing the Cosmos DB endpoint URI and primary key. Then, we create a CosmosClientOptions
object and set the connection mode to Gateway
.
Next, we create the database and allocate provisioned throughput of 10,000 request units per second (RU/s). Afterward, we create two containers within the database and assign them different provisioned throughput values (4,000 RU/s and 6,000 RU/s). These containers will share the provisioned throughput allocated at the database level.
By using the above approach, you can effectively manage and allocate provisioned throughput at the database level for your Azure Cosmos DB native applications. Remember to consider factors like cost-efficiency and shared access patterns when deciding whether to use database-level provisioned throughput.
Conclusion
Azure Cosmos DB provides flexibility in managing provisioned throughput at both the database and container levels. By leveraging database-level provisioned throughput, you can optimize costs and improve resource utilization, especially when you have containers with similar access patterns.
Answer the Questions in Comment Section
When should you choose database-level provisioned throughput in Azure Cosmos DB?
a) When your application has varying workload patterns and requires rapid scaling
b) When you want to optimize for cost-efficiency and prefer on-demand capacity
c) When you need fine-grained control over the throughput at the database level
d) When you want to store data in multiple regions for high availability
Correct answer: c) When you need fine-grained control over the throughput at the database level
True or False: Database-level provisioned throughput in Azure Cosmos DB is suitable for unpredictable or bursty workloads.
Correct answer: False
What is the key benefit of using database-level provisioned throughput in Azure Cosmos DB?
a) High availability and automatic scaling
b) Lower cost compared to other throughput options
c) Fine-grained control over resource allocation
d) Seamless integration with other Azure services
Correct answer: c) Fine-grained control over resource allocation
When using provisioned throughput at the database level, how is the throughput allocated to containers within the database?
a) Each container has its own provisioned throughput capacity
b) The throughput is evenly distributed among all containers within the database
c) Throughput allocation is based on the container’s size and usage pattern
d) Containers within the same database cannot have different throughput levels
Correct answer: b) The throughput is evenly distributed among all containers within the database
Which factors should you consider when choosing between database-level provisioned throughput and container-level provisioned throughput in Azure Cosmos DB? (Select all that apply)
a) Workload predictability
b) Cost optimization
c) Fine-grained access control
d) Multi-region replication
Correct answers: a) Workload predictability and b) Cost optimization
True or False: You can switch between database-level provisioned throughput and container-level provisioned throughput without any downtime in Azure Cosmos DB.
Correct answer: False
Which SLA is applicable when using database-level provisioned throughput in Azure Cosmos DB?
a) 99% availability for reads and writes
b) 999% availability for reads and writes
c) 9% availability for reads and writes
d) SLA is not applicable for database-level provisioned throughput
Correct answer: c) 9% availability for reads and writes
What happens if the provisioned throughput for a database in Azure Cosmos DB is exceeded?
a) Additional throughput is automatically provided at no extra cost
b) Requests are throttled until the throughput is increased
c) The exceeding requests are rejected and need to be retried
d) Throughput cannot be exceeded as it is automatically adjusted
Correct answer: b) Requests are throttled until the throughput is increased
Which metrics can be used to monitor and optimize the performance of a database-level provisioned throughput in Azure Cosmos DB? (Select all that apply)
a) Request Units (RUs) consumed
b) CPU utilization
c) Network throughput
d) Storage capacity
Correct answers: a) Request Units (RUs) consumed and b) CPU utilization
True or False: Database-level provisioned throughput in Azure Cosmos DB is suitable for small-scale applications with low traffic.
Correct answer: False
This article really helped clarify when to use provisioned throughput in Cosmos DB!
Can someone explain how provisioned throughput differs from autoscale in terms of cost?
Thanks for the awesome post!
Great article! It helped me understand the scalability options.
Is it true that provisioned throughput is not recommended for unpredictable workloads?
Just passed my DP-420 exam! Your articles were invaluable.
How does one monitor the usage effectively if they choose provisioned throughput?
Thank you for this detailed guide!