在 Azure Cosmos DB 中优化开发和测试成本Optimize development and testing cost in Azure Cosmos DB

本文介绍免费使用 Azure Cosmos DB 进行开发和测试的各种选项,以及在开发或测试帐户中进行成本优化的各种技巧。This article describes the different options to use Azure Cosmos DB for development and testing for free of cost, as well as techniques to optimize cost in development or test accounts.

Azure Cosmos DB 模拟器(可以本地下载的版本)Azure Cosmos DB emulator (locally downloadable version)

Azure Cosmos DB 模拟器是一个本地的可下载版本,模拟 Azure Cosmos DB 云服务。Azure Cosmos DB emulator is a local downloadable version that mimics the Azure Cosmos DB cloud service. 即使没有网络连接,也可以编写并测试使用 Azure Cosmos DB API 的代码,不需支付任何费用。You can write and test code that uses the Azure Cosmos DB APIs even if you have no network connection and without incurring any costs. Azure Cosmos DB 模拟器提供了一个用于开发的具有云服务高保真特性的本地环境。Azure Cosmos DB emulator provides a local environment for development purposes with high fidelity to the cloud service. 可以在本地开发和测试应用程序,无需创建 Azure 订阅。You can develop and test your application locally, without creating an Azure subscription. 做好将应用程序部署到云的准备以后,即可更新连接到云中的 Azure Cosmos DB 终结点所需的连接字符串,不需进行其他的修改。When you're ready to deploy your application to the cloud, update the connection string to connect to the Azure Cosmos DB endpoint in the cloud, no other modifications are needed. 也可在 Azure DevOps 中通过 Azure Cosmos DB 模拟器生成任务设置 CI/CD 管道,以便运行测试。You can also set up a CI/CD pipeline with the Azure Cosmos DB emulator build task in Azure DevOps to run tests. 若要开始操作,可以参阅 Azure Cosmos DB 模拟器一文。You can get started by visiting the Azure Cosmos DB emulator article.

Azure Cosmos DB 免费层Azure Cosmos DB free tier

使用 Azure Cosmos DB 免费层,可以轻松上手、开发和测试应用程序,甚至免费运行小型生产工作负载。Azure Cosmos DB free tier makes it easy to get started, develop and test your applications, or even run small production workloads for free. 在帐户上启用免费层后,一开始你将在该帐户中免费获得 400 RU/秒的吞吐量和 5 GB 的存储空间。When free tier is enabled on an account, you'll get the first 400 RU/s and 5 GB of storage in the account free. 还可以创建包含 25 个容器的共享吞吐量数据库,这些容器在数据库级别的吞吐量均为 400 RU/秒,所有这些操作都可以通过免费层实现(免费层帐户的限制为 5 个共享吞吐量数据库)。You can also create a shared throughput database with 25 containers that share 400 RU/s at the database level, all covered by free tier (limit 5 shared throughput databases in a free tier account). 使用免费层时,如果预配的共享数据库的最小吞吐量为 400 RU/秒,则该数据库中的所有容器都可以共享吞吐量。When using the free tier, if you provision a shared database with a minimum throughput of 400 RU/s, all the containers within that database can share the throughput. 任何具有共享吞吐量的新数据库或具有专用吞吐量的容器都将按常规定价计费。Any new databases with shared throughput or containers with dedicated throughput are billed at the regular pricing.

备注

免费层仅可在“预配吞吐量”模式下使用。Free tier is only available in provisioned throughput mode.

免费层在帐户的生命周期内无限期地持续有效,并且附带常规 Azure Cosmos DB 帐户的所有优点和功能,包括无限制的存储和吞吐量(RU/秒)、SLA、高可用性以及在所有 Azure 中国区域进行统包式多区域分布等。Free tier lasts indefinitely for the lifetime of the account and comes with all the benefits and features of a regular Azure Cosmos DB account, including unlimited storage and throughput (RU/s), SLAs, high availability, turnkey multiple-region distribution in all Azure China regions, and more. 每个 Azure 订阅最多可以有一个免费层帐户,并且必须在创建帐户时选择加入使用。You can have up to one free tier account per Azure subscription and must opt-in when creating the account. 首先,在 Azure 门户中创建一个启用了免费层的新帐户 或使用 ARM 模板To get started, create a new account in Azure portal with free tier enabled or use an ARM Template. 如需更多详细信息,请参阅定价页See the pricing page for more details.

Azure 试用帐户Azure trial account

Azure Cosmos DB 包含在 Azure 试用帐户中,该帐户提供 Azure 信用额度和资源,可以免费使用特定的一段时间。Azure Cosmos DB is included in the Azure trial account, which offers Azure credits and resources for free for a certain time period. 特别对于 Azure Cosmos DB,该试用帐户提供 5 GB 的存储和 400 RU 的全年预配吞吐量。Specifically for Azure Cosmos DB, this trial account offers 5-GB storage and 400 RUs of provisioned throughput for the entire year. 任何开发人员都可以通过此体验轻松地测试 Azure Cosmos DB 的功能,或者将其与其他 Azure 服务集成,没有任何费用。This experience enables any developer to easily test the features of Azure Cosmos DB or integrate it with other Azure services at zero cost. 使用 Azure 试用帐户可获得 1500 元信用额度,该额度可以在前 30 天使用。With Azure trial account, you get a CNY 1500 credit to spend in the first 30 days. 在选择升级之前,即使开始使用服务也不会收费。You won't be charged, even if you start using the services until you choose to upgrade. 若要开始操作,请访问 Azure 试用帐户页。To get started, visit Azure trial account page.

Azure Cosmos DB 无服务器Azure Cosmos DB serverless

Azure Cosmos DB 无服务器让你以一种基于消耗的方式使用 Azure Cosmos 帐户。在这种方式下,你只需为数据库操作所消耗的请求单位和数据所消耗的存储空间付费。Azure Cosmos DB serverless lets you use your Azure Cosmos account in a consumption-based fashion where you are only charged for the Request Units consumed by your database operations and the storage consumed by your data. 在无服务器模式下使用 Azure Cosmos DB 时不涉及最低费用。There is no minimum charge involved when using Azure Cosmos DB in serverless mode. 因为它消除了预配容量的概念,所以它最适合于开发或测试活动,尤其是在数据库大部分时间处于空闲状态的情况下。Because it eliminates the concept of provisioned capacity, it is best suited for development or testing activities specifically when your database is idle most of the time.

使用共享吞吐量数据库Use shared throughput databases

共享吞吐量数据库中,数据库中的所有容器共享数据库的已预配吞吐量(RU/秒)。In a shared throughput database, all containers inside the database share the provisioned throughput (RU/s) of the database. 例如,如果为数据库预配了 400 RU/秒并且有四个容器,则所有四个容器共享此 400 RU/秒的吞吐量。For example, if you provision a database with 400 RU/s and have four containers, all four containers will share the 400 RU/s. 在开发或测试环境中,对每个容器的访问可能不是很频繁,因此,需求低于最小值 400 RU/秒,将容器置于共享吞吐量数据库中有助于优化成本。In a development or testing environment, where each container may be be accessed less frequently and thus require lower than the minimum of 400 RU/s, putting containers in a shared throughput database can help optimize cost.

例如,假设你的开发或测试帐户有四个容器。For example, suppose your development or test account has four containers. 如果创建具有专用吞吐量(最小为 400 RU/秒)的四个容器,则总吞吐量将为 1600 RU/秒。If you create four containers with dedicated throughput (minimum of 400 RU/s), your total RU/s will be 1600 RU/s. 相反,如果创建一个共享吞吐量数据库(至少 400 RU/秒)并将容器置于其中,则总吞吐量将只有 400 RU/秒。In contrast, if you create a shared throughput database (minimum 400 RU/s) and put your containers there, your total RU/s will be just 400 RU/s. 通常情况下,共享吞吐量数据库非常适合不需要在任何单个容器上保证吞吐量的方案。In general, shared throughput databases are great for scenarios where you don't need guaranteed throughput on any individual container. 详细了解共享吞吐量数据库Learn more about shared throughput databases.

后续步骤Next steps

可以按照以下文章的说明,开始使用模拟器或免费的 Azure Cosmos DB 帐户:You can get started with using the emulator or the free Azure Cosmos DB accounts with the following articles: