您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

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 or the strong consistency model is the gold standard of data programmability. 但其导致的延迟代价较高(稳定状态下)且会降低可用性(遇到故障时)。But it adds a price of higher 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. 尽管非常一致性和最终一致性处于该范围的两个极端,但在一致性的整个范围中,还有很多一致性选项。Strong consistency and eventual consistency are at the ends of the spectrum, but there are many consistency choices along the spectrum. 开发人员可以使用这些选项在高可用性和性能方面做出精确的选择和细致的取舍。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 models on the consistency spectrum. 从最强到最弱,这些模型包括“非常”、“有限过期”、“会话”、“一致前缀”和“最终”一致性。From strongest to more relaxed, the models include strong, bounded staleness, session, consistent prefix, and eventual consistency. 这些模型具有明确的定义且非常直观,可用于特定的真实场景。The models are well-defined and intuitive and can be used for specific real-world scenarios. 每个模型在可用性与性能方面各有利弊,并有 SLA 作为保障。Each model provides availability and performance tradeoffs and is backed by the 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 partition-key range or 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. Cosmos-tla GitHub 存储库中提供了使用 TLA + 规范语言 Azure Cosmos DB 中五个一致性级别的精确定义。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.

  • 有限过期性:保证读取操作遵循一致性前缀保证。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 (i.e., "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 except within the "staleness window." 过期窗口内部和外部的区域中提供单调读取保证。The monotonic read guarantees exist within a region both inside and outside the staleness window. 非常一致性的语义与有限过期提供的语义相同。Strong consistency has the same semantics as the one offered by bounded staleness. 过期窗口等于零。The staleness window is equal to zero. 有限过期也称为“延时可线性化”。Bounded staleness is also referred to as time-delayed linearizability. 当客户端在接受写入的区域中执行读取操作时,有限过期一致性提供的保证与非常一致性的保证相同。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.

  • 会话一致性:在单客户端会话中, 保证接受一致前缀 (假设单个 "写入器" 会话), 单调读取、单调写入、读写和读/写。Session: Within a single client session reads are guaranteed to honor the consistent-prefix (assuming a single “writer” session), monotonic reads, monotonic writes, read-your-writes, and write-follows-reads guarantees. 执行写入操作的会话之外的客户端将看到最终一致性。Clients outside of the session performing writes will see eventual consistency.

  • 一致前缀:返回的更新包含所有更新的一些前缀,不带间隔。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.

  • 最终一致性:不保证读取的顺序。Eventual: There's no ordering guarantee for reads. 如果缺少任何进一步的写入,则副本最终会收敛。In the absence of any further writes, the replicas eventually converge.

借助棒球解释一致性级别Consistency levels explained through baseball

让我们以棒球比赛场景为例。Let's take a baseball game scenario as an example. 想象一系列表示棒球比赛得分的写入。Imagine a sequence of writes that represent the score from a baseball game. Replicated data consistency through baseball(借助棒球阐释复制数据一致性)一文中描述了逐局得分。The inning-by-inning line score is described in the Replicated data consistency through baseball paper. 这场虚构的棒球比赛目前正处于第七局的中段。This hypothetical baseball game is currently in the middle of the seventh inning. 这是第七局的比赛。It's the seventh-inning stretch. 客队以 2 比 5 的比分落后,如下所示:The visitors are behind with a score of 2 to 5 as shown below:

11 22 33 44 55 66 77 88 99 本垒打次数Runs
客队Visitors 00 00 11 00 11 00 00 22
主队Home 11 00 11 11 00 22 55

Azure Cosmos 容器保存客队和主队的本垒打总次数。An Azure Cosmos container holds the run totals for the visitors and home teams. 当比赛正在进行时,不同的读取保证可能会导致客户端读取不同的分数。While the game is in progress, different read guarantees might result in clients reading different scores. 下表列出了使用每种(共五种)一致性保证读取客队和主队分数后可能返回的完整分数集。The following table lists the complete set of scores that might be returned by reading the visitors' and home scores with each of the five consistency guarantees. 首先列出客队的得分。The visitors' score is listed first. 不同的可能返回值以逗号分隔。Different possible return values are separated by commas.

一致性级别Consistency level 比分(客队、主队)Scores (Visitors, Home)
非常Strong 2-52-5
有限过期Bounded staleness 最多一个已过期的回合比分:2-3、2-4、2-5Scores that are at most one inning out of date: 2-3, 2-4, 2-5
会话Session
  • 写入者:2-5For the writer: 2-5
  • 写入者以外的任何人:0-0、0-1、0-2、0-3、0-4、0-5、1-0、1-1、1-2、1-3、1-4、1-5、2-0、2-1、2-2、2-3、2-4、2-5For anyone other than the writer: 0-0, 0-1, 0-2, 0-3, 0-4, 0-5, 1-0, 1-1, 1-2, 1-3, 1-4, 1-5, 2-0, 2-1, 2-2, 2-3, 2-4, 2-5
  • 读取 1-3 后:1-3、1-4、1-5、2-3、2-4、2-5After reading 1-3: 1-3, 1-4, 1-5, 2-3, 2-4, 2-5
一致前缀Consistent prefix 0-0、0-1、1-1、1-2、1-3、2-3、2-4、2-50-0, 0-1, 1-1, 1-2, 1-3, 2-3, 2-4, 2-5
最终Eventual 0-0、0-1、0-2、0-3、0-4、0-5、1-0、1-1、1-2、1-3、1-4、1-5、2-0、2-1、2-2、2-3、2-4、2-50-0, 0-1, 0-2, 0-3, 0-4, 0-5, 1-0, 1-1, 1-2, 1-3, 1-4, 1-5, 2-0, 2-1, 2-2, 2-3, 2-4, 2-5

其他阅读材料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: