B
Brian_Kemboi
Optimizing Azure cosmos DB involves using strategies and best practices to reduce the overall spending on the service while maintaining and improving performance and availability.
In this blog, I’ll explore actionable strategies and tools for reducing your Azure Cosmos DB costs without sacrificing the speed, scalability, or reliability of your database. Whether you're managing large-scale applications or developing on a budget, these insights will help you make the most out of Azure Cosmos DB.
The main sub-topics we shall dive into includes:
- Pricing model
- Free development
- Plan for Optimization
1. Pricing model
Azure cosmos DB bills for three types of usage: compute, storage, and bandwidth. What does this mean? Let us break it down.
- Compute
Azure Cosmos DB bills based on Request Units (RU) measured per second (RU/s). Request Units are currency for throughput. This represents the cost of operations on your database which can either be read, write, and query. These operations consume a certain number of RUs (Request Units) based on their complexity and size.
- Storage
Azure Cosmos DB bills for consumed storage, rounded up to the next gigabyte (GB) per container, collection, table, or graph per region.
- Bandwidth
Data transfer between Azure Cosmos DB and other Azure services or the internet incurs additional costs. The exact pricing depends on the amount of data transferred.
Types Azure Cosmos DB account
You get charged depending on the type of account you have. Azure cosmos DB account can be either be Provisioned Throughput or a Serverless Account.
Provisioned Throughput.
Think of provisioned throughput as the “speed limit” for your Azure Cosmos DB database and containers. It determines how many requests (or operations) your database or container can handle per second. Just like a highway with different speed limits, you allocate a specific amount of throughput to your database or container.
Based on your workloads, you can scale either upwards or downwards. However, there is a minimum throughput requirement to guarantee SLAs. You will be charged for the provisioned throughput even when you do not run any workloads, this is because the model dedicates resources to your container or your database.
What is the difference between Azure cosmos DB database and azure cosmos DB containers?
Azure cosmos DB database is a storage area where you can organize a set of containers. You can create multiple containers to hold different types of data.
Azure cosmos DB Containers are individual storage compartments within a database. They help organize data effectively. An advantage you get is that you can set different throughput for each based on the workload.
Example/ Illustration
Imagine you are developing an e-commerce application. Your database is called: OnlineStore. The containers it contains include:
- Products – to store product details
- Customers – to store customer information
- Orders – to store orders places
Database level and Container level Throughput in Azure cosmos DB.
When you set throughput at the database level, it applies to all containers within that database. You can choose between two options for provisioned throughput.
- Standard (Manual) Throughput: This is a fixed speed limit for a specific container.
- Autoscale Throughput: The speed limit adjusts automatically based on demand.
The throughputs are evenly distributed to all the partitions. If a container gets more requests than it is allocated, it might slow down.
Throughput on a container.
Throughput provisioned for a container is evenly distributed among its physical partitions while it assumes the logical partitions of the container. At times, the logical partitions consume more than the throughput allocated to the underlying physical partition. If such happens, operations will be rate limited. This will need you to reprovision the throughput for the entire container. Learn more about Partitioning and horizontal scaling in Azure Cosmos DB
The image shows how a physical partition hosts one or more logical partitions of a container:
Throughput on a database.
As earlier stated, when you provision throughput on a database, it will be shared across all the containers unless you specify that the throughput should be used in some containers.
It is recommended that when you configure throughput for your database, it should be across all the containers and not any parti container.
The image below demonstrates how a physical partition can host one or more logical partitions that belong to different containers within a database:
Learn more provisioned throughput in Azure Cosmos DB
Serverless Account
The serverless account type in Azure Cosmos DB is designed for scenarios where you want to pay only for the resources you use. With the serverless option, you’re charged based on the request units (RUs) consumed by your database operations and the storage used by your data.
Uses Cases of serverless Account
- You are in development or testing a service
- When there is unpredicted traffic
- When integrating with serverless compute services, like Azure Functions.
Learn more about Azure Cosmos DB serverless account type
2. Free development
The second way to optimize your services on Azure cosmos Db is free development. There are two ways to develop for free: You can use Free tier or use the emulator.
Free tier
The Azure Cosmos DB free tier makes it easy to get started, develop, test your applications, or even run small production workloads for free.
When you enable the free tier on an Azure Cosmos DB account, you’ll receive the following benefits:
- First 1000 RU/s (Request Units per second): You get the first 1000 RU/s for free.
- 25 GB of Storage: You also receive 25 GB of storage at no cost.
Beyond these limits, any additional throughput or storage consumed is billed at regular prices.
The free tier is available for all API accounts with provisioned throughput, autoscale throughput, single, or multiple write regions.
An illustration is shown in our previous blog Exploring the Capabilities of Azure Cosmos DB for MongoDB using Open MongoDB shell.
With the free tier, you do not accrue any cost, however, you do not have the option for high availability.
Note that free tier is not available for serverless accounts.
You can have up to one free tier Azure Cosmos DB account per Azure subscription. If you don’t see the option to apply the free tier discount, another account in the same subscription has already been enabled with the free tier.
Learn more about Azure Cosmos DB lifetime free tier
Use the emulator
The Azure Cosmos DB emulator is a powerful tool that provides a local environment for emulating the Azure Cosmos DB service. It is mostly used for development and Testing. With the emulator, you can develop without the need for Azure subscriptions.
The emulator's Data Explorer pane is only supported in the API for NoSQL and API for MongoDB. Understand the differences between the emulator and cloud service
Please also check a blog that will guide on Local Development using Azure Cosmos DB Emulator at no Cost
3. Plan for Optimization
Estimate Costs Before Creating Resources:
Use the Azure Cosmos DB capacity calculator to estimate your workload cost before creating any resources. This tool allows you to input details such as the number of regions, data stored, and anticipated operations volume.
It provides an estimate of storage costs and provisioned throughput based on your workload parameters.
Here is an image to show how to use the capacity calculator. Visit the Documentation on Estimating RU/s using the Azure Cosmos DB capacity planner to learn how to use it
Understand the Full Billing Model
Azure Cosmos DB runs on Azure infrastructure, and costs accrue when you deploy new resources. Be aware that the costs for Azure Cosmos DB are only a portion of your monthly Azure bill, which includes all azure services and resources used in your subscription, including third-party services
Understand the full billing model
Monitor Costs
As you use resources with Azure Cosmos DB, you incur costs. Regularly monitor your usage and spending to stay within budget.
You can view this on Azure portal.
Step 1: Login to your azure portal and go to your resource.
Step 2: Go to the overview section
Step 3: At the bottom, click on monitoring to show the estimate chart., you can estimate the usage at an interval of 1hr, 24hrs,7days and 30days. You will be able to see the estimates on the chart.
Create Budgets
Set budgets in Azure Cost Management to manage costs effectively. Create alerts that will notify of spending anomalies and overspending risks.
Here are images to show you how you can achieve that.
Step 1: In your resource, expand on monitoring and select alerts and click on add rule.
Step 2: Select a signal, I will select CPU Credits consumed.
Step 3: Set the threshold that will trigger the actions
Step 4: On the sections tab, select actions. I will go with 'Use quick actions' a feature with is on preview. It will send me an email to alert me. Give the group name and a display name for it.
Step 5: On Details Tab, add a name to the alert then review and create the alert.
Conclusion.
We have covered several ways you can optimize your resources on Azure cosmos DB. Following the good practices, you can optimize costs while your services are still running as expected. To learn more, you can visit the links shared below.
Read More
Total Cost of Ownership (TCO) with Azure Cosmos DB
Best practices for scaling provisioned throughput (RU/s)
Provisioned throughput in Azure Cosmos DB
Learn more on how to plan and manage costs for Azure Cosmos DB
Optimize request cost in Azure Cosmos DB
Optimize storage cost in Azure Cosmos DB
Optimize multi-region cost in Azure Cosmos DB
Optimize development and testing cost in Azure Cosmos DB
Continue reading...