冲突类型和解决策略Conflict types and resolution policies

如果为 Azure Cosmos DB 帐户配置了多个写入区域,则适用冲突和冲突解决策略的概念。Conflicts and conflict resolution policies are applicable if your Azure Cosmos DB account is configured with multiple write regions.

对于配置了多个写入区域的 Azure Cosmos 帐户,当多个写入者同时更新多个区域中的同一项目时,可能会发生更新冲突。For Azure Cosmos accounts configured with multiple write regions, update conflicts can occur when writers concurrently update the same item in multiple regions. 更新冲突可以分为以下三种类型:Update conflicts can be of the following three types:

  • 插入冲突:当应用程序同时在两个或更多个区域插入两个或更多个具有相同唯一索引的项目时,可能会发生此类冲突。Insert conflicts: These conflicts can occur when an application simultaneously inserts two or more items with the same unique index in two or more regions. 例如,可能会因为 ID 属性而发生此冲突。For example, this conflict might occur with an ID property.

  • 替换冲突:当应用程序同时在两个或更多个区域中更新同一项目时,可能会发生此类冲突。Replace conflicts: These conflicts can occur when an application updates the same item simultaneously in two or more regions.

  • 删除冲突:如果应用程序在一个区域删除某个项目,同时在另一区域更新该项目,则可能会发生此类冲突。Delete conflicts: These conflicts can occur when an application simultaneously deletes an item in one region and updates it in another region.

冲突解决策略Conflict resolution policies

Azure Cosmos DB 提供了灵活的策略驱动型机制来解决写入冲突。Azure Cosmos DB offers a flexible policy-driven mechanism to resolve write conflicts. 可以针对 Azure Cosmos 容器,从以下两种冲突解决策略中进行选择:You can select from two conflict resolution policies on an Azure Cosmos container:

  • 最后写入者胜出 (LWW) :此解决策略默认情况下使用系统定义的时间戳属性。Last Write Wins (LWW): This resolution policy, by default, uses a system-defined timestamp property. 它基于时间同步时钟协议。It's based on the time-synchronization clock protocol. 若使用 SQL API,可以指定其他任何自定义数字属性(例如,你自己的时间戳概念)来解决冲突。If you use the SQL API, you can specify any other custom numerical property (e.g., your own notion of a timestamp) to be used for conflict resolution. 自定义数字属性又称为冲突解决路径。 A custom numerical property is also referred to as the conflict resolution path.

    如果在执行插入或替换操作时有两个或更多个项发生冲突,冲突解决路径值最大的项将成为优胜者。If two or more items conflict on insert or replace operations, the item with the highest value for the conflict resolution path becomes the winner. 如果多个项的冲突解决路径的数字值相同,则由系统确定优胜者。The system determines the winner if multiple items have the same numeric value for the conflict resolution path. 保证所有区域融合到单个优胜者,并且提交的项的版本最终相同。All regions are guaranteed to converge to a single winner and end up with the same version of the committed item. 当涉及到删除冲突时,已删除版本始终优先于插入或替换冲突。When delete conflicts are involved, the deleted version always wins over either insert or replace conflicts. 不管冲突解决路径的值如何,均会出现此结果。This outcome occurs no matter what the value of the conflict resolution path is.

    Note

    “最后写入者胜出”是默认冲突解决策略,对以下 API 使用时间戳 _ts:SQL、MongoDB、Cassandra、Gremlin、表。Last Write Wins is the default conflict resolution policy and uses timestamp _ts for the following APIs: SQL, MongoDB, Cassandra, Gremlin and Table. 自定义数值属性仅可用于 SQL API。Custom numerical property is available only for SQL API.

    有关详细信息,请参阅 LWW 冲突解决策略的用法示例To learn more, see examples that use LWW conflict resolution policies.

  • 自定义:此解决策略旨在使用应用程序定义的语义来调解冲突。Custom: This resolution policy is designed for application-defined semantics for reconciliation of conflicts. 在 Azure Cosmos 容器上设置此策略时,还需注册合并存储过程。 When you set this policy on your Azure Cosmos container, you also need to register a merge stored procedure. 在服务器的数据库事务下检测到冲突时,会自动调用此过程。This procedure is automatically invoked when conflicts are detected under a database transaction on the server. 在执行提交协议过程中,该系统可保证正好执行合并过程一次。The system provides exactly once guarantee for the execution of a merge procedure as part of the commitment protocol.

    如果为容器配置了自定义解决选项,但无法在容器中注册合并过程,或者合并过程在运行时引发异常,则冲突将写入到冲突源。 If you configure your container with the custom resolution option, and you fail to register a merge procedure on the container or the merge procedure throws an exception at runtime, the conflicts are written to the conflicts feed. 然后,应用程序需要自行解决冲突源中的冲突。Your application then needs to manually resolve the conflicts in the conflicts feed. 有关详细信息,请参阅自定义解决策略和冲突源的用法示例To learn more, see examples of how to use the custom resolution policy and how to use the conflicts feed.

    Note

    自定义冲突解决策略仅适用于 SQL API 帐户。Custom conflict resolution policy is available only for SQL API accounts.

后续步骤Next steps

了解如何配置冲突解决策略:Learn how to configure conflict resolution policies: