一致性级别和 Azure Cosmos DB APIConsistency levels and Azure Cosmos DB APIs

Azure Cosmos DB 为常用数据库提供对与线路协议兼容的 API 的本机支持。Azure Cosmos DB provides native support for wire protocol-compatible APIs for popular databases. 这些数据库包括 MongoDB、Apache Cassandra、Gremlin 和 Azure 表存储。These include MongoDB, Apache Cassandra, Gremlin, and Azure Table storage. 这些数据库既没有提供准确定义的一致性模型,也没有为一致性级别提供由 SLA 支持的保证。These databases do not offer precisely defined consistency models or SLA-backed guarantees for the consistency levels. 它们通常仅提供 Azure Cosmos DB 提供的五个一致性模型的一个子集。They typically provide only a subset of the five consistency models offered by Azure Cosmos DB.

使用 SQL API、Gremlin API 和表 API 时,会使用 Azure Cosmos 帐户上配置的默认一致性级别。When using SQL API, Gremlin API, and Table API, the default consistency level configured on the Azure Cosmos account is used.

使用 Cassandra API 或 Azure Cosmos DB 的 MongoDB API 时,应用程序会获得一整套分别由 Apache Cassandra 和 MongoDB 提供的一致性级别,其一致性和持续性保证甚至更强。When using Cassandra API or Azure Cosmos DB's API for MongoDB, applications get a full set of consistency levels offered by Apache Cassandra and MongoDB, respectively, with even stronger consistency and durability guarantees. 本文档介绍了与 Apache Cassandra 和 MongoDB 一致性级别对应的 Azure Cosmos DB 一致性级别。This document shows the corresponding Azure Cosmos DB consistency levels for Apache Cassandra and MongoDB consistency levels.

Apache Cassandra 与 Azure Cosmos DB 一致性级别之间的映射Mapping between Apache Cassandra and Azure Cosmos DB consistency levels

与 Azure Cosmos DB 不同,Apache Cassandra 本身并不提供精确定义的一致性保证。Unlike Azure Cosmos DB, Apache Cassandra does not natively provide precisely defined consistency guarantees. 与之相反,Apache Cassandra 提供一个写入一致性级别和一个读取一致性级别,以便进行高可用性、一致性和延迟方面的权衡。Instead, Apache Cassandra provides a write consistency level and a read consistency level, to enable the high availability, consistency, and latency tradeoffs. 使用 Azure Cosmos DB 的 Cassandra API 时:When using Azure Cosmos DB's Cassandra API:

  • Apache Cassandra 的写入一致性级别映射到在 Azure Cosmos 帐户上配置的默认一致性级别。The write consistency level of Apache Cassandra is mapped to the default consistency level configured on your Azure Cosmos account.

  • Azure Cosmos DB 会将 Cassandra 客户端驱动程序指定的读取一致性级别动态映射到根据读取请求动态配置的某个 Azure Cosmos DB 一致性级别。Azure Cosmos DB will dynamically map the read consistency level specified by the Cassandra client driver to one of the Azure Cosmos DB consistency levels configured dynamically on a read request.

下表演示了在使用 Cassandra API 时,如何将本机 Cassandra 一致性级别映射到 Azure Cosmos DB 的一致性级别:The following table illustrates how the native Cassandra consistency levels are mapped to the Azure Cosmos DB's consistency levels when using Cassandra API:

Cassandra 一致性模型映射Cassandra consistency model mapping

MongoDB 与 Azure Cosmos DB 一致性级别之间的映射Mapping between MongoDB and Azure Cosmos DB consistency levels

与 Azure Cosmos DB 不一样,本机 MongoDB 并不提供精确定义的一致性保证。Unlike Azure Cosmos DB, the native MongoDB does not provide precisely defined consistency guarantees. 与之相反,本机 MongoDB 允许用户配置下述一致性保证:写入关注、读取关注以及 isMaster 指令 - 目的是将读取操作定向到主副本或辅助副本,以便实现所需的一致性级别。Instead, native MongoDB allows users to configure the following consistency guarantees: a write concern, a read concern, and the isMaster directive - to direct the read operations to either primary or secondary replicas to achieve the desired consistency level.

使用 Azure Cosmos DB 的 API for MongoDB 时,MongoDB 驱动程序会将写入区域视为主副本,所有其他区域为读取副本。When using Azure Cosmos DB's API for MongoDB, the MongoDB driver treats your write region as the primary replica and all other regions are read replica. 可以选择将哪个与 Azure Cosmos 帐户关联的区域作为主副本。You can choose which region associated with your Azure Cosmos account as a primary replica.

在使用 Azure Cosmos DB 的 API for MongoDB 时:While using Azure Cosmos DB's API for MongoDB:

  • 写入关注映射到在 Azure Cosmos 帐户上配置的默认一致性级别。The write concern is mapped to the default consistency level configured on your Azure Cosmos account.

  • Azure Cosmos DB 会将 MongoDB 客户端驱动程序指定的读取关注动态映射到根据读取请求动态配置的某个 Azure Cosmos DB 一致性级别。Azure Cosmos DB will dynamically map the read concern specified by the MongoDB client driver to one of the Azure Cosmos DB consistency levels that is configured dynamically on a read request.

  • 可以将与 Azure Cosmos 帐户关联的特定区域批注为“主区域”,方法是将该区域设置为第一个可写区域。You can annotate a specific region associated with your Azure Cosmos account as "Master" by making the region as the first writable region.

下表演示了在使用 Azure Cosmos DB 的 API for MongoDB 时,如何将本机 MongoDB 写入/读取关注映射到 Azure Cosmos 的一致性级别:The following table illustrates how the native MongoDB write/read concerns are mapped to the Azure Cosmos consistency levels when using Azure Cosmos DB's API for MongoDB:

MongoDB 一致性模型映射MongoDB consistency model mapping

后续步骤Next steps

详细了解 Azure Cosmos DB API 与开源 API 之间的一致性级别和兼容性。Read more about consistency levels and compatibility between Azure Cosmos DB APIs with the open-source APIs. 请参阅以下文章:See the following articles: