Azure Cosmos DB 中的一致性级别Consistency levels in Azure Cosmos DB

依赖于复制实现高可用性和/或低延迟的分布式数据库在读取一致性与可用性、延迟和吞吐量之间进行基本权衡。Distributed databases that rely on replication for high availability, low latency, or both, make the fundamental tradeoff between the read consistency vs. availability, latency, and throughput. 大多数商用分布式数据库都要求开发人员在两种极端一致性模型之间进行选择:非常一致和最终一致。 Most commercially available distributed databases ask developers to choose between the two extreme consistency models: strong consistency and eventual consistency. 非常一致性模型的可线性化是数据可编程性的黄金标准。The linearizability of the strong consistency model is the gold standard of data programmability. 但它导致的写入延迟代价较高(稳定状态下)且会降低可用性(遇到故障时)。But it adds a price of higher write latency (in steady state) and reduced availability (during failures). 另一方面,最终一致性可提供更高的可用性和性能,但会加大应用程序的编程难度。On the other hand, eventual consistency offers higher availability and better performance, but makes it hard to program applications.

Azure Cosmos DB 通过某种选择范围来实现数据一致性,而不会走两种极端。Azure Cosmos DB approaches data consistency as a spectrum of choices instead of two extremes. 开发人员可以使用这些选项在高可用性和性能方面做出精确的选择和细致的取舍。Developers can use these options to make precise choices and granular tradeoffs with respect to high availability and performance.

借助 Azure Cosmos DB,开发人员可以在一致性范围内从五个明确定义的一致性级别中进行选择。With Azure Cosmos DB, developers can choose from five well-defined consistency levels on the consistency spectrum. 这些级别包括“非常”、“有限过期”、“会话”、“一致前缀”和“最终”一致性。 These levels include strong, bounded staleness, session, consistent prefix, and eventual consistency. 这些级别具有明确的定义且非常直观,可用于特定的真实场景。The levels are well-defined and intuitive and can be used for specific real-world scenarios. 每个级别在可用性与性能方面各有利弊,并有 SLA 作为保障。Each level provides availability and performance tradeoffs and are backed by SLAs. 下图以范围区间形式显示了不同的一致性级别。The following image shows the different consistency levels as a spectrum.

范围形式的一致性

一致性级别与区域无关,无论在哪个区域为读取和写入操作提供服务、与 Azure Cosmos 帐户关联的区域数量是多少,或者帐户是配置了单个还是多个写入区域,都可以保证所有操作获得这种一致性。The consistency levels are region-agnostic and are guaranteed for all operations regardless of the region from which the reads and writes are served, the number of regions associated with your Azure Cosmos account, or whether your account is configured with a single or multiple write regions.

读取一致性的范围Scope of the read consistency

读取一致性适用于逻辑分区范围内的单个读取操作。Read consistency applies to a single read operation scoped within a logical partition. 读取操作可能由远程客户端或存储过程发出。The read operation can be issued by a remote client or a stored procedure.

配置默认一致性级别Configure the default consistency level

随时都可在 Azure Cosmos DB 帐户中配置默认的一致性级别。You can configure the default consistency level on your Azure Cosmos account at any time. 在帐户中配置的默认一致性级别适用于该帐户下的所有 Azure Cosmos 数据库和容器。The default consistency level configured on your account applies to all Azure Cosmos databases and containers under that account. 针对某个容器或数据库发出的所有读取和查询默认使用指定的一致性级别。All reads and queries issued against a container or a database use the specified consistency level by default. 有关详细信息,请参阅如何配置默认一致性级别To learn more, see how to configure the default consistency level.

与一致性级别关联的保证Guarantees associated with consistency levels

Azure Cosmos DB 提供的综合 SLA 可保证 100% 的读取请求满足所选任何一致性级别的一致性保证。The comprehensive SLAs provided by Azure Cosmos DB guarantee that 100 percent of read requests meet the consistency guarantee for any consistency level you choose. 如果满足与一致性级别关联的所有一致性保证,则读取请求满足一致性 SLA。A read request meets the consistency SLA if all the consistency guarantees associated with the consistency level are satisfied. azure-cosmos-tla GitHub 存储库中提供了 Azure Cosmos DB 中使用 TLA+ 规范语言精确定义的五个一致性级别。The precise definitions of the five consistency levels in Azure Cosmos DB using the TLA+ specification language are provided in the azure-cosmos-tla GitHub repo.

下面描述了五个一致性级别的语义:The semantics of the five consistency levels are described here:

  • 非常一致性:非常一致性提供可线性化保证。Strong: Strong consistency offers a linearizability guarantee. 可线性化是指并发处理请求。Linearizability refers to serving requests concurrently. 保证读取操作返回项的最新提交版本。The reads are guaranteed to return the most recent committed version of an item. 客户端永远不会看到未提交或不完整的写入。A client never sees an uncommitted or partial write. 始终保证用户读取最新确认的写入。Users are always guaranteed to read the latest committed write.

    下图以乐谱形式演示了非常一致性。The following graphic illustrates the strong consistency with musical notes. 将数据写入“中国北部 2”区域后,当你从其他区域读取这些数据时,将会获得最新的值:After the data is written to the "China North 2" region, when you read the data from other regions, you get the most recent value:

    video

  • 受限停滞一致性:保证读取操作遵循一致性前缀保证。Bounded staleness: The reads are guaranteed to honor the consistent-prefix guarantee. 读取操作可以滞后于写入操作最多 K 个项版本(即“更新”)或“T”时间间隔。 The reads might lag behind writes by at most "K" versions (that is, "updates") of an item or by "T" time interval. 换言之,如果选择有限过期,则可以通过两种方式配置“过期”:In other words, when you choose bounded staleness, the "staleness" can be configured in two ways:

  • 项的版本数 (K)The number of versions (K) of the item

  • 读取操作可以滞后于写入操作的时间间隔 (T)The time interval (T) by which the reads might lag behind the writes

有限过期在“过期窗口”之外提供全局整体顺序。Bounded staleness offers total global order outside of the "staleness window." 当客户端在接受写入的区域中执行读取操作时,有限过期一致性提供的保证与非常一致性的保证相同。When a client performs read operations within a region that accepts writes, the guarantees provided by bounded staleness consistency are identical to those guarantees by the strong consistency.

在过期窗口内,有限过期提供以下一致性保证:Inside the staleness window, Bounded Staleness provides the following consistency guarantees:

  • 对于单主帐户,同一区域中的客户端的一致性为“非常”Consistency for clients in the same region for a single-master account = Strong

  • 对于单主帐户,不同区域中的客户端的一致性为“一致前缀”Consistency for clients in different regions for a single-master account = Consistent Prefix

  • 对于多主帐户,向单个区域进行写入的客户端的一致性为“一致前缀”Consistency for clients writing to a single region for a multi-master account = Consistent Prefix

  • 对于多主帐户,向不同区域进行写入的客户端的一致性为“最终”Consistency for clients writing to different regions for a multi-master account = Eventual

    预期写入延迟较低,但需要保证全局整体顺序的多区域分布式应用程序经常选择“有限过期”。Bounded staleness is frequently chosen by multiple-regionally distributed applications that expect low write latencies but require total global order guarantee. 有限过期非常适合提供小组协作和共享、股票行情、发布-订阅/排队等功能的应用程序。下图以乐谱形式演示了有限过期一致性。Bounded staleness is great for applications featuring group collaboration and sharing, stock ticker, publish-subscribe/queueing etc. The following graphic illustrates the bounded staleness consistency with musical notes. 将数据写入“中国北部 2”区域后,“中国东部 2”和“澳大利亚东部”区域将会根据所配置的最大滞后时间或最大操作数目读取写入的值:After the data is written to the "China North 2" region, the "China East 2" and "Australia East" regions read the written value based on the configured maximum lag time or the maximum operations:

    video

  • 会话一致性:在单个客户端会话中,将保证读取操作遵循一致前缀、单调读取、单调写入、读取写入和读取后写入保证。Session: Within a single client session reads are guaranteed to honor the consistent-prefix, monotonic reads, monotonic writes, read-your-writes, and write-follows-reads guarantees. 这采用单个“写入器”会话,或者多个写入器共享会话令牌。This assumes a single "writer" session or sharing the session token for multiple writers.

在会话外部执行写入的客户端将获得以下保证:Clients outside of the session performing writes will see the following guarantees:

  • 对于单主帐户,同一区域中的客户端的一致性为“一致前缀”Consistency for clients in same region for a single-master account = Consistent Prefix

  • 对于单主帐户,不同区域中的客户端的一致性为“一致前缀”Consistency for clients in different regions for a single-master account = Consistent Prefix

  • 对于多主帐户,向单个区域进行写入的客户端的一致性为“一致前缀”Consistency for clients writing to a single region for a multi-master account = Consistent Prefix

  • 对于多主帐户,向多个区域进行写入的客户端的一致性为“最终”Consistency for clients writing to multiple regions for a multi-master account = Eventual

    “会话一致性”是最广泛用于单个区域以及多区域分布式应用程序的一致性级别。Session consistency is the most widely used consistency level for both single region as well as multiple-regionally distributed applications. 它不仅提供与最终一致性相当的写入延迟、可用性和读取吞吐量,还提供一致性保证,从而满足了编写为在用户上下文中运行的应用程序的需求。It provides write latencies, availability, and read throughput comparable to that of eventual consistency but also provides the consistency guarantees that suit the needs of applications written to operate in the context of a user. 下图以乐谱形式演示了会话一致性。The following graphic illustrates the session consistency with musical notes. “中国北部 2 写入器”和“中国北部 2 读取器”正在使用同一个会话(会话 A),因此它们会同时读取相同的数据。The "China North 2 writer" and the "China North 2 reader" are using the same session (Session A) so they both read the same data at the same time. 而“澳大利亚东部”区域正在使用“会话 B”,因此,它会稍后才会接收到数据,但接收顺序与写入顺序相同。Whereas the "Australia East" region is using "Session B" so, it receives data later but in the same order as the writes.

    video

  • 一致前缀:返回的更新包含所有更新的一些前缀,不带间隔。Consistent prefix: Updates that are returned contain some prefix of all the updates, with no gaps. 一致前缀一致性级别保证读取操作永远不会看到无序写入。Consistent prefix consistency level guarantees that reads never see out-of-order writes.

如果写入是按 A, B, C 顺序执行的,则客户端会看到 AA,BA,B,C,但永远不会看到类似于 A,CB,A,C 的失序排列情况。If writes were performed in the order A, B, C, then a client sees either A, A,B, or A,B,C, but never out-of-order permutations like A,C or B,A,C. 一致前缀的延迟、可用性和读取吞吐量与最终一致性相当,但还会提供顺序保证,以适应顺序非常重要的方案的需求。Consistent Prefix provides write latencies, availability, and read throughput comparable to that of eventual consistency, but also provides the order guarantees that suit the needs of scenarios where order is important.

下面是一致前缀的一致性保证:Below are the consistency guarantees for Consistent Prefix:

  • 对于单主帐户,同一区域中的客户端的一致性为“一致前缀”Consistency for clients in same region for a single-master account = Consistent Prefix

  • 对于单主帐户,不同区域中的客户端的一致性为“一致前缀”Consistency for clients in different regions for a single-master account = Consistent Prefix

  • 对于多主帐户,向单个区域进行写入的客户端的一致性为“一致前缀”Consistency for clients writing to a single region for a multi-master account = Consistent Prefix

  • 对于多主帐户,向多个区域进行写入的客户端的一致性为“最终”Consistency for clients writing to multiple regions for a multi-master account = Eventual

    下图以乐谱形式演示了一致前缀一致性。The following graphic illustrates the consistency prefix consistency with musical notes. 在所有区域中,读取操作永远不会看到无序写入:In all the regions, the reads never see out of order writes:

    video

  • 最终一致性:不保证读取的顺序。Eventual: There's no ordering guarantee for reads. 如果缺少任何进一步的写入,则副本最终会收敛。In the absence of any further writes, the replicas eventually converge.
    最终一致性是最弱的一致性形式,因为客户端可能会读取比之前读取的值还要旧的值。Eventual consistency is the weakest form of consistency because a client may read the values that are older than the ones it had read before. 最终一致性非常适合不需要任何顺序保证的应用程序。Eventual consistency is ideal where the application does not require any ordering guarantees. 示例包括推文、点赞或无回复评论的计数。Examples include count of Retweets, Likes, or non-threaded comments. 下图以乐谱形式演示了最终一致性。The following graphic illustrates the eventual consistency with musical notes.

    video

其他阅读材料Additional reading

若要详细了解一致性的概念,请阅读以下文章:To learn more about consistency concepts, read the following articles:

后续步骤Next steps

要详细了解 Azure Cosmos DB 中的一致性级别,请阅读以下文章:To learn more about consistency levels in Azure Cosmos DB, read the following articles: