Write Concern for Replica Sets副本集的写入关注
On this page本页内容
Write concern for replica sets describe the number of data-bearing members (i.e. the primary and secondaries, but not arbiters) that must acknowledge a write operation before the operation returns as successful. 副本集的写入关注描述了在操作返回为成功之前必须确认写操作的数据承载成员(即主和辅助,但不是仲裁器)的数量。A member can only acknowledge a write operation after it has received and applied the write successfully.成员只有在成功接收并应用写入操作后才能确认写入操作。
For replica sets:对于副本集:
A write concern of写入关注w: "majority"
requires acknowledgement that the write operations have been durably committed to a calculated majority of the data-bearing voting members.w: "majority"
需要确认写入操作已持久地致力于计算出的大多数有数据的投票成员。For most replica set configurations,对于大多数复制副本集配置,w:"majority"
is the default write concern.w:"majority"
是默认的写入关注。A write concern ofw: 1
only requires acknowledgment from the primary replica set member before returning write concern acknowledgment.w: 1
的写入关注在返回写入关注确认之前只需要来自主副本集成员的确认。A write concern with a numeric value greater than数值大于1
requires acknowledgment from the primary and as many secondaries as needed to meet the specified value.1
的写入关注需要来自主设备和所需数量的辅助设备的确认,以满足指定的值。The secondaries do not need to be voting members to meet the write concern threshold.中学不需要是有投票权的成员就可以达到书面关注的门槛。The specified write concern value cannot be greater than the total number of data-bearing members in the replica set.指定的写入关注值不能大于副本集中数据承载成员的总数。
For complete documentation on write acknowledgment behavior, see Acknowledgment Behavior.有关写入确认行为的完整文档,请参阅确认行为。
An application that issues a write operation that requires write concern acknowledgment waits until the primary receives acknowledgment from the required number of members for the specified write concern. 发出需要写入关注确认的写操作的应用程序将等待,直到主应用程序接收到来自指定写入关注所需数量的成员的确认。For write concern of 对于w
greater than 1 or w : "majority"
, the primary waits until the required number of secondaries acknowledge the write before returning write concern acknowledgment. w
大于1或w : "majority"
的写入关注,主系统等待,直到所需数量的辅助系统确认写入,然后返回写入关注确认。For write concern of 对于w: 1
, the primary can return write concern acknowledgment as soon as it locally applies the write since it is eligible for contributing to the requested write concern.w: 1
的写入关注,主系统可以在本地应用写操作后立即返回写入关注确认,因为它有资格参与请求的写入关注。
The more members that acknowledge a write, the less likely the written data could roll back if the primary fails. 确认写入的成员越多,如果主操作失败,写入的数据回滚的可能性就越小。However, specifying a high write concern can increase latency as the client must wait until it receives the requested level of write concern acknowledgment.但是,指定高写入关注可能会增加延迟,因为客户端必须等待,直到收到请求级别的写入关注确认。
Selecting the ideal write concern for any given write operation depends on your application's performance goals and data durability requirements. 为任何给定的写操作选择理想的写操作取决于应用程序的性能目标和数据持久性要求。For more guidance on configuring write concern to prevent rollbacks, see Avoid Replica Set Rollbacks.有关配置写入关注以防止回滚的更多指导,请参阅避免副本集回滚。
Verify Write Operations to Replica Sets验证对副本集的写入操作
The following operation includes the 以下操作包括writeConcern
option for the insertOne()
method. insertOne()
方法的writeConcern
选项。The operation specifies:
- the 该操作指定:-"majority"
write concern, and - a 5 second timeout."majority"
写入关注,以及-5
秒超时。
The wtimeout
write concern parameter ensures that the operation does not block indefinitely.wtimeout
写入关注参数确保操作不会无限期地阻塞。
db.products.insertOne(
{ item: "envelopes", qty : 100, type: "Clasp" },
{ writeConcern: { w: "majority" , wtimeout: 5000 } }
)
The application waits until the primary returns write concern acknowledgment, indicating that a calculated majority of the data-bearing voting members acknowledged the write operation. 应用程序将等待,直到主应用程序返回写入关注确认,这表明经过计算的大多数数据承载投票成员确认了写入操作。For example, in a 3-member replica set (P-S-S), the operation would require acknowledgment from 2 out of the 3 members. 例如,在3个成员的复制集(P-S-S)中,操作将需要来自3个成员中的2个成员的确认。If the replica set was later scaled to include two additional voting secondary members, the same operation would require acknowledgment from 3 out of the 5 replica set members. 如果复制集后来被扩展为包括两个额外的投票辅助成员,则相同的操作将需要来自5个复制集成员中的3个的确认。If the primary does not return write concern acknowledgment within the 如果主系统在wtimeout
limit, the write operation fails with a write concern error.wtimeout
限制内没有返回写入关注确认,则写操作将失败,并出现写入关注错误。
A write operation that times out waiting for the specified write concern only indicates that the required number of replica set members did not acknowledge the write operation within the 等待指定写入关注超时的写入操作仅表示所需数量的副本集成员没有在wtimeout
time period. wtimeout
时间段内确认写入操作。It does not necessarily indicate that the primary failed to apply the write. 它并不一定表示主应用写入失败。The data may exist on a subset of replica set nodes at the time of the write concern error, and can continue replicating until all nodes in the cluster have that data. 在发生写入关注错误时,数据可能存在于副本集节点的子集上,并且可以继续复制,直到群集中的所有节点都具有该数据为止。Applications should take into account the potential availability of written data regardless of the state of write concern acknowledgment.应用程序应考虑写入数据的潜在可用性,而不管写入关注确认的状态如何。
The exact syntax for specifying write concern depends on the write operation. 指定写入关注的确切语法取决于写入操作。Refer to the documentation for the write operation for instructions on write concern support and syntax. 有关写入关注支持和语法的说明,请参阅有关写操作的文档。For complete documentation on write concern, see Write Concern.有关写入关注的完整文档,请参阅写入关注。
See also: 另请参阅:
Modify Default Write Concern修改默认写入关注
You can modify the default write concern for a replica set by issuing the 您可以通过发出setDefaultRWConcern
command.setDefaultRWConcern
命令来修改复制副本集的默认写入关注。
If you issue a write operation with a specific write concern, the write operation uses its own write concern instead of the default.如果您发出具有特定写入关注的写操作,则写操作将使用其自己的写入关注,而不是默认的写入关注。
See also: 另请参阅:
Custom Write Concerns自定义写入关注
You can tag the members of replica sets and use the resulting tag sets to create custom write concerns. 您可以标记复制副本集的成员,并使用生成的标记集创建自定义写入关注。See Configure Replica Set Tag Sets for information on configuring custom write concerns using tag sets.有关使用标记集配置自定义写入关注的信息,请参阅配置副本集标记集。