Azure Cosmos DB 常见用例Common Azure Cosmos DB use cases

本文概述了 Azure Cosmos DB 的几个常见用例。This article provides an overview of several common use cases for Azure Cosmos DB. 本文中的建议可以作为使用 Cosmos DB 开发应用程序的一个起点。The recommendations in this article serve as a starting point as you develop your application with Cosmos DB.

阅读本文后,可以回答以下问题:After reading this article, you'll be able to answer the following questions:

  • Azure Cosmos DB 的常见用例有哪些?What are the common use cases for Azure Cosmos DB?
  • 对零售应用程序使用 Azure Cosmos DB 有什么好处?What are the benefits of using Azure Cosmos DB for retail applications?
  • 将 Azure Cosmos DB 用作物联网 (IoT) 系统的数据存储空间有什么好处?What are the benefits of using Azure Cosmos DB as a data store for Internet of Things (IoT) systems?
  • 对 Web 和移动应用程序使用 Azure Cosmos DB 有什么好处?What are the benefits of using Azure Cosmos DB for web and mobile applications?


Azure Cosmos DB 是世纪互联提供的多区域分布式数据库服务。Azure Cosmos DB is 21Vianet's multiple-regionally distributed database service. 该服务允许客户跨数目不限的地理区域弹性(独立)缩放吞吐量与存储。The service is designed to allow customers to elastically (and independently) scale throughput and storage across any number of geographical regions. Azure Cosmos DB 是在当前市场中推出的第一个多区域分布式数据库服务,其提供的综合服务级别协议涵盖吞吐量、延迟、可用性和一致性。Azure Cosmos DB is the first multiple-regionally distributed database service in the market today to offer comprehensive service level agreements encompassing throughput, latency, availability, and consistency.

Azure Cosmos DB 是在各种应用程序和用例中广泛使用的多区域分布式多模型数据库。Azure Cosmos DB is a multiple-region distributed, multi-model database that is used in a wide range of applications and use cases. 它适用于需要低延迟毫秒级响应时间和需要多区域快速缩放的任何无服务器应用程序。It is a good choice for any serverless application that needs low order-of-millisecond response times, and needs to scale rapidly and multiple-regionally. 它以可扩展的方式本机支持多种数据模型(键值、文档、图形和纵栏式)以及许多用于数据访问的 API,包括 Azure Cosmos DB 的 API for MongoDBSQL APIGremlin API表 APIIt supports multiple data models (key-value, documents, graphs and columnar) and many APIs for data access including Azure Cosmos DB's API for MongoDB, SQL API, Gremlin API, and Tables API natively, and in an extensible manner.

下面介绍了 Azure Cosmos DB 的几个属性,这些属性使 Cosmos DB 非常适用于想要实现多区域分布的高性能应用程序。The following are some attributes of Azure Cosmos DB that make it well-suited for high-performance applications with multiple-region ambition.

  • Azure Cosmos DB 采用本机方式对数据进行分区,以实现高可用性和可伸缩性。Azure Cosmos DB natively partitions your data for high availability and scalability. Azure Cosmos DB 为所有单区域帐户和具有松散一致性的所有多区域帐户提供对可用性、吞吐量和一致性的 99.99% 保证,为所有多区域数据库帐户提供 99.999% 的读取可用性。Azure Cosmos DB offers 99.99% guarantees for availability, throughput, low latency, and consistency on all single-region accounts and all multi-region accounts with relaxed consistency, and 99.999% read availability on all multi-region database accounts.
  • Azure Cosmos DB 采用由 SSD 提供支持的存储,具有低延迟毫秒级响应时间。Azure Cosmos DB has SSD backed storage with low-latency order-of-millisecond response times.
  • Azure Cosmos DB 支持最终、一致前缀、会话和受限停滞等一致性级别,从而实现充分的灵活性和较高的性价比。Azure Cosmos DB's support for consistency levels like eventual, consistent prefix, session, and bounded-staleness allows for full flexibility and low cost-to-performance ratio. Azure Cosmos DB 在级别一致性方面的灵活性超过任何其他数据库服务。No database service offers as much flexibility as Azure Cosmos DB in levels consistency.
  • Azure Cosmos DB 提供了灵活的数据友好型定价模式,独立测量存储和吞吐量。Azure Cosmos DB has a flexible data-friendly pricing model that meters storage and throughput independently.
  • 通过 Azure Cosmos DB 保留的吞吐量模型,可考虑读取/写入数量而不考虑基础硬件的 CPU/内存/IOP。Azure Cosmos DB's reserved throughput model allows you to think in terms of number of reads/writes instead of CPU/memory/IOPs of the underlying hardware.
  • Azure Cosmos DB 的设计允许扩展到每天约数万亿个请求的大规模请求量。Azure Cosmos DB's design lets you scale to massive request volumes in the order of trillions of requests per day.

这些特性对于需要低响应时间和需要处理大量读取和写入操作的 Web、移动、游戏和 IoT 应用程序尤为有益。These attributes are beneficial in web, mobile, gaming, and IoT applications that need low response times and need to handle massive amounts of reads and writes.

IoT 和远程信息处理IoT and telematics

IoT 用例在引入、处理和存储数据方法方面通常具有相同的模式。IoT use cases commonly share some patterns in how they ingest, process, and store data. 首先,这些系统需要引入各处设备传感器中的激增数据。First, these systems need to ingest bursts of data from device sensors of various locales. 其次,这些系统可以处理和分析流式传输数据,以获得实时见解。Next, these systems process and analyze streaming data to derive real-time insights. 然后将数据存档到闲置存储进行批量分析。The data is then archived to cold storage for batch analytics. Azure 提供可应用于 IoT 用例的多种服务,包括 Azure Cosmos DB、Azure 事件中心、Azure 流分析、Azure 通知中心、Azure 机器学习、Azure HDInsight 和 PowerBI。Azure offers rich services that can be applied for IoT use cases including Azure Cosmos DB, Azure Event Hubs, Azure Stream Analytics, Azure Notification Hub, Azure Machine Learning, Azure HDInsight, and PowerBI.

Azure Cosmos DB IoT 参考体系结构

由于 Azure 事件中心可以低延迟引入高吞吐量数据,因此它可以引入激增数据。Bursts of data can be ingested by Azure Event Hubs as it offers high throughput data ingestion with low latency. 可以将需要进行实时分析的引入数据传输至 Azure 流分析,以进行实时分析。Data ingested that needs to be processed for real-time insight can be funneled to Azure Stream Analytics for real-time analytics. 可将数据加载到 Azure Cosmos DB 中进行即席查询。Data can be loaded into Azure Cosmos DB for adhoc querying. 将数据加载到 Azure Cosmos DB 后,即可对其进行查询。Once the data is loaded into Azure Cosmos DB, the data is ready to be queried. 此外,可以在更改源中读取新数据以及对现有数据的更改。In addition, new data and changes to existing data can be read on change feed. 更改源是持久性的、仅限追加的日志,可按顺序存储对 Cosmos 容器的更改。Change feed is a persistent, append only log that stores changes to Cosmos containers in sequential order. Azure Cosmos DB 中的所有数据或者仅仅对数据所做的更改可用作实时分析中的参考数据。The all data or just changes to data in Azure Cosmos DB can be used as reference data as part of real-time analytics. 此外,通过将 Azure Cosmos DB 数据连接到 HDInsight 进行 Pig、Hive 或 Map/Reduce 作业,可进一步优化和处理数据。In addition, data can further be refined and processed by connecting Azure Cosmos DB data to HDInsight for Pig, Hive, or Map/Reduce jobs. 经过优化的数据随后被加载回 Azure Cosmos DB 用于报告。Refined data is then loaded back to Azure Cosmos DB for reporting.

有关使用 Azure Cosmos DB、EventHubs 和 Storm 的 IoT 解决方案示例,请参阅 GitHub 上的 hdinsight-storm-examples 存储库For a sample IoT solution using Azure Cosmos DB, EventHubs and Storm, see the hdinsight-storm-examples repository on GitHub.

零售和营销Retail and marketing

Azure Cosmos DB 广泛用于世纪互联旗下运行 Windows 应用商店和 XBox Live 的电子商务平台。Azure Cosmos DB is used extensively in 21Vianet's own e-commerce platforms, that run the Windows Store and XBox Live. 它还在零售业中用于存储目录数据,以及在订单处理管道中用于事件寻源。It is also used in the retail industry for storing catalog data and for event sourcing in order processing pipelines.

目录数据使用方案涉及存储和查询人员、地点、产品等实体的一组属性。Catalog data usage scenarios involve storing and querying a set of attributes for entities such as people, places, and products. 目录数据的部分示例包括用户帐户、产品目录、IoT 设备注册表和物料系统清单。Some examples of catalog data are user accounts, product catalogs, IoT device registries, and bill of materials systems. 此数据的属性可能会随时间的推移而发生变化和更改以满足应用程序要求。Attributes for this data may vary and can change over time to fit application requirements.

以汽车部件供应商的产品目录为例。Consider an example of a product catalog for an automotive parts supplier. 除所有部件共有的常见属性外,每个部件可能都具有其自己的属性。Every part may have its own attributes in addition to the common attributes that all parts share. 而且,某个特定部件的属性还可能会在发布新型号之后的第二年发生变化。Furthermore, attributes for a specific part can change the following year when a new model is released. Azure Cosmos DB 支持灵活的架构和分层数据,因此非常适用于存储产品目录数据。Azure Cosmos DB supports flexible schemas and hierarchical data, and thus it is well suited for storing product catalog data.

Azure Cosmos DB 零售目录参考体系结构

Azure Cosmos DB 通常用于事件寻源,可使用其更改源功能为事件驱动的体系结构提供动力。Azure Cosmos DB is often used for event sourcing to power event driven architectures using its change feed functionality. 更改源使下游微服务能够可靠地以增量方式读取针对 Azure Cosmos DB 所做的插入和更新(例如订单事件)。The change feed provides downstream microservices the ability to reliably and incrementally read inserts and updates (for example, order events) made to an Azure Cosmos DB. 利用此功能可以提供一个持久性的事件存储作为状态不断变化的事件的消息中转站,并驱动许多微服务之间的订单处理工作流(可实现为无服务器 Azure Functions)。This functionality can be leveraged to provide a persistent event store as a message broker for state-changing events and drive order processing workflow between many microservices (which can be implemented as serverless Azure Functions).

Azure Cosmos DB 订单管道参考体系结构

此外,Azure Cosmos DB 中存储的数据可与 HDInsight 集成,通过 Apache Spark 作业进行大数据分析。In addition, data stored in Azure Cosmos DB can be integrated with HDInsight for big data analytics via Apache Spark jobs. 此外,Azure Cosmos DB 中存储的数据可与 HDInsight 集成,通过 Apache Spark 作业进行大数据分析。In addition, data stored in Azure Cosmos DB can be integrated with HDInsight for big data analytics via Apache Spark jobs. 有关 Azure Cosmos DB 的 Spark 连接器的详细信息,请参阅使用 Cosmos DB 和 HDInsight 运行 Spark 作业For details on the Spark Connector for Azure Cosmos DB, see Run a Spark job with Cosmos DB and HDInsight.


数据库层是游戏应用程序的关键组件。The database tier is a crucial component of gaming applications. 新式游戏可在移动/控制台客户端执行图形处理,但需依赖云传输自定义的个性化内容,例如游戏中的统计数据、社交媒体集成以及高分排行榜。Modern games perform graphical processing on mobile/console clients, but rely on the cloud to deliver customized and personalized content like in-game stats, social media integration, and high-score leaderboards. 游戏通常需要单毫秒的读取和写入延迟,从而带来具有吸引力的游戏体验。Games often require single-millisecond latencies for reads and writes to provide an engaging in-game experience. 新游戏启动和功能更新期间,游戏数据库需要具备很快的速度,并且要能够处理请求速率中的大量峰值。A game database needs to be fast and be able to handle massive spikes in request rates during new game launches and feature updates.

Azure Cosmos DB 用于 Next Games 推出的 The Walking Dead:No Man's Land(行尸走肉:无人地带)和 Halo 5:Guardians(光环 5:守护者)等游戏。Azure Cosmos DB is used by games like The Walking Dead: No Man's Land by Next Games, and Halo 5: Guardians. 对于游戏开发人员,Azure Cosmos DB 具备以下优势:Azure Cosmos DB provides the following benefits to game developers:

  • Azure Cosmos DB 允许弹性提高或降低性能。Azure Cosmos DB allows performance to be scaled up or down elastically. 此优势使游戏可以通过执行单个 API 调用同时处理从几十到数百万不等的游戏配置文件和统计数据的更新。This allows games to handle updating profile and stats from dozens to millions of simultaneous gamers by making a single API call.

  • Azure Cosmos DB 支持毫秒读取和写入,有助于避免在玩游戏期间出现任何延迟。Azure Cosmos DB supports millisecond reads and writes to help avoid any lags during game play.

  • Azure Cosmos DB 的自动索引可对多个不同属性进行实时筛选,例如,通过内部玩家 ID、其 GameCenter ID 或基于玩家公会成员身份的查询对玩家进行定位。Azure Cosmos DB's automatic indexing allows for filtering against multiple different properties in real-time, for example, locate players by their internal player IDs, or their GameCenter IDs, or query based on player membership in a guild. 如果不构建复杂的索引或分区基础结构,就不可能实现这种操作。This is possible without building complex indexing or sharding infrastructure.

  • 采用灵活的架构,就更容易实现游戏内聊天消息、玩家公会成员身份、已完成的挑战、高分排行榜以及社交图谱等社交功能。Social features including in-game chat messages, player guild memberships, challenges completed, high-score leaderboards, and social graphs are easier to implement with a flexible schema.

  • Azure Cosmos DB 作为托管的平台即服务 (PaaS) 只需最少的设置和管理工作即可实现快速迭代并缩短上市时间。Azure Cosmos DB as a managed platform-as-a-service (PaaS) required minimal setup and management work to allow for rapid iteration, and reduce time to market.

Azure Cosmos DB 游戏参考体系结构

Web 和移动应用程序Web and mobile applications

Azure Cosmos DB 通常用于 Web 和移动应用程序内部,非常适用于社交互动建模、与第三方服务集成和积累丰富的个性化体验。Azure Cosmos DB is commonly used within web and mobile applications, and is well suited for modeling social interactions, integrating with third-party services, and for building rich personalized experiences. 可以使用 Cosmos DB SDK 构建使用常用 Xamarin 框架的丰富 iOS 和 Android 应用程序。The Cosmos DB SDKs can be used build rich iOS and Android applications using the popular Xamarin framework.

社交应用程序Social Applications

Azure Cosmos DB 的一个常见用例就是存储和查询 Web、移动和社交媒体应用程序的用户生成内容 (UGC)。A common use case for Azure Cosmos DB is to store and query user generated content (UGC) for web, mobile, and social media applications. UGC 的部分示例包括聊天会话、推文、博客文章、评级和评论。Some examples of UGC are chat sessions, tweets, blog posts, ratings, and comments. 通常情况下,社交媒体应用程序中的 UGC 混合了不受刚性结构约束的自由格式文本、属性、标记和关系。Often, the UGC in social media applications is a blend of free form text, properties, tags, and relationships that are not bounded by rigid structure. 可将聊天、评论和帖子等内容存储在 Cosmos DB 中,而无需转换或复杂对象关系映射层。Content such as chats, comments, and posts can be stored in Cosmos DB without requiring transformations or complex object to relational mapping layers. 可以轻易添加或修改数据属性以满足开发人员遍历应用程序代码的要求,从而促进快速开发。Data properties can be added or modified easily to match requirements as developers iterate over the application code, thus promoting rapid development.

与第三方社交网络集成的应用程序必须响应这些网络中不断更改的架构。Applications that integrate with third-party social networks must respond to changing schemas from these networks. 由于 Cosmos DB 中的数据默认自动编制索引,因此可以随时查询数据。As data is automatically indexed by default in Cosmos DB, data is ready to be queried at any time. 因此,这些应用程序可以根据其各自的需求灵活地检索投影。Hence, these applications have the flexibility to retrieve projections as per their respective needs.

许多社交应用程序以多区域规模运行并表现出不可预测的使用模式。Many of the social applications run at multiple-region scale and can exhibit unpredictable usage patterns. 因此,应用程序层能根据用量需求缩放时,缩放数据存储的灵活性至关重要。Flexibility in scaling the data store is essential as the application layer scales to match usage demand. 可通过在 Cosmos DB 帐户之下添加额外的数据分区来进行扩大。You can scale out by adding additional data partitions under a Cosmos DB account. 也可在多个区域中创建额外的 Cosmos DB 帐户。In addition, you can also create additional Cosmos DB accounts across multiple regions. 有关 Cosmos DB 服务区域可使用性的信息,请参阅 Azure 区域For Cosmos DB service region availability, see Azure Regions.

Azure Cosmos DB Web 应用参考体系结构


如今,新式应用程序都具有复杂的视图和体验。Nowadays, modern applications come with complex views and experiences. 它们通常采用动态设计,迎合对用户首选项或情绪以及品牌塑造的需求。These are typically dynamic, catering to user preferences or moods and branding needs. 因此,应用程序需要能够有效地检索个性化设置,以快速呈现 UI 元素和体验。Hence, applications need to be able to retrieve personalized settings effectively to render UI elements and experiences quickly.

Cosmos DB 支持的 JSON 格式是一种用于呈现 UI 布局数据的有效格式,它不仅轻量而且可由 JavaScript 轻松理解。JSON, a format supported by Cosmos DB, is an effective format to represent UI layout data as it is not only lightweight, but also can be easily interpreted by JavaScript. Cosmos DB 提供可调的一致性级别,可以实现快速读取和低延迟写入。Cosmos DB offers tunable consistency levels that allow fast reads with low latency writes. 因此,在 Cosmos DB 中将包括个性化设置的 UI 布局数据存储为 JSON 文档是获取网络数据的一种有效方法。Hence, storing UI layout data including personalized settings as JSON documents in Cosmos DB is an effective means to get this data across the wire.

Azure Cosmos DB Web 应用参考体系结构

后续步骤Next steps

  • 可以根据快速入门开始使用 Azure Cosmos DB,其中逐步讲解了如何创建帐户并开始使用 Cosmos DB。To get started with Azure Cosmos DB, follow our quick starts, which walk you through creating an account and getting started with Cosmos DB.

  • 如果要了解有关使用 Azure Cosmos DB 的客户的详细信息,请参阅客户案例研究页。If you'd like to read more about customers using Azure Cosmos DB, see the customer case studies page.