Microsoft Docs

Azure Cosmos DB Documentation

Scaling Azure Cosmos DB

Azure Cosmos DB is a globally distributed, multi-model database service that allows you to scale throughput and storage elastically and independently across any number of geographic regions. This tutorial will guide you through the essential concepts and strategies for scaling your Azure Cosmos DB deployments effectively.

Understanding Throughput and Storage Scaling

Azure Cosmos DB offers two primary dimensions for scaling:

Request Units (RU/s)

Every operation in Azure Cosmos DB consumes a certain number of Request Units. The number of RUs consumed depends on factors like the type of operation, the size of the data accessed, and the consistency level. Understanding RU consumption is crucial for cost management and performance tuning.

Common RU Consumption Examples:

Scaling Strategies

1. Manual Throughput Provisioning

With manual throughput, you specify a fixed amount of RU/s for your database or container. This is suitable for predictable workloads where the throughput requirements are consistent.

// Example: Setting manual throughput for a container await container.Database.ChangeThroughputAsync(400); // Set to 400 RU/s

2. Autoscale Throughput Provisioning

Autoscale allows Azure Cosmos DB to automatically scale the provisioned throughput (RU/s) up and down based on your actual workload demands. This is ideal for variable workloads, helping you optimize costs while ensuring performance.

When you enable autoscale, you specify a maximum RU/s. Azure Cosmos DB will scale throughput between 10% of the maximum and the maximum value. For example, if you set the maximum to 4000 RU/s, it will scale between 400 RU/s and 4000 RU/s.

Recommendation: For most applications with fluctuating traffic, autoscale offers a cost-effective and performant solution.

3. Scaling at Different Levels

You can provision throughput at two levels:

Storage Scaling

Storage in Azure Cosmos DB scales automatically. As your data grows, Cosmos DB handles the underlying infrastructure to accommodate the increasing storage requirements. There's no manual intervention needed for storage expansion, ensuring your application remains available.

Global Distribution and Scaling

Azure Cosmos DB's global distribution capabilities allow you to scale your application's reach and availability by replicating data across multiple Azure regions. This ensures low-latency access for users worldwide and provides disaster recovery capabilities.

Monitoring and Optimization

Regularly monitoring your Cosmos DB performance is key to effective scaling. Key metrics to track include:

Use Azure Monitor and Cosmos DB's built-in diagnostics to identify bottlenecks and optimize your RU/s allocation or data partitioning strategies.

Tip: Properly partitioning your data is fundamental for achieving high scalability and performance in Azure Cosmos DB. Ensure your partition key choice distributes requests evenly.

Conclusion

Scaling Azure Cosmos DB involves understanding RU/s, choosing between manual and autoscale throughput, and leveraging global distribution. By monitoring your usage and adopting best practices, you can ensure your database scales seamlessly to meet your application's evolving demands.