Docs HomeMongoDB Manual

Replica Set Deployment Architectures副本集部署体系结构

The architecture of a replica set affects the set's capacity and capability. This document provides strategies for replica set deployments and describes common architectures.副本集的体系结构会影响该集的容量和功能。本文档提供了副本集部署的策略,并介绍了常见的体系结构。

The standard replica set deployment for a production system is a three-member replica set. 生产系统的标准复制副本集部署是一个由三个成员组成的复制副本集。These sets provide redundancy and fault tolerance. Avoid complexity when possible, but let your application requirements dictate the architecture.这些集合提供冗余和容错。尽可能避免复杂性,但让您的应用程序需求决定体系结构。

Strategies策略

Determine the Number of Members确定成员数量

Add members in a replica set according to these strategies.根据这些策略在副本集中添加成员。

Maximum Number of Voting Members有表决权成员的最大数量

A replica set can have up to 50 members, but only 7 voting members. 副本集最多可以有50个成员,但只有7个有投票权的成员If the replica set already has 7 voting members, additional members must be non-voting members.如果副本集已经有7个有投票权的成员,则其他成员必须是无投票权成员

Deploy an Odd Number of Members部署奇数个成员

Ensure that the replica set has an odd number of voting members. 请确保副本集的投票成员数为奇数。A replica set can have up to 7 voting members. 副本集最多可以有7个投票成员。If you have an even number of voting members, deploy another data bearing voting member or, if constraints prohibit against another data bearing voting member, an arbiter.如果您有偶数个投票成员,请部署另一个携带数据的投票成员,或者,如果限制禁止针对另一个带有数据的投票会员,则部署一个仲裁器

An arbiter does not store a copy of the data and requires fewer resources. 仲裁器不存储数据的副本,并且需要更少的资源。As a result, you may run an arbiter on an application server or other shared resource. 因此,您可以在应用程序服务器或其他共享资源上运行仲裁器。With no copy of the data, it may be possible to place an arbiter into environments that you would not place other members of the replica set. Consult your security policies.在没有数据副本的情况下,可以将仲裁器放置到不会放置副本集其他成员的环境中。请咨询您的安全策略。

For the following MongoDB versions, pv1 increases the likelihood of w:1 rollbacks compared to pv0 (no longer supported in MongoDB 4.0+) for replica sets with arbiters:对于以下MongoDB版本,与带有仲裁器的副本集的pv0(在MongoDB 4.0+中不再支持)相比,pv1增加了w:1回滚的可能性:

  • MongoDB 3.4.1
  • MongoDB 3.4.0
  • MongoDB 3.2.11 or earlier

See Replica Set Protocol Version.请参阅副本集协议版本

Warning

Avoid deploying more than one arbiter in a replica set. 避免在副本集中部署多个仲裁器See Concerns with Multiple Arbiters.请参阅与多个仲裁器有关的问题

To add an arbiter to an existing replica set:要将仲裁器添加到现有副本集,请执行以下操作:

You do not need to change the cluster wide write concern before starting a new replica set with an arbiter.在使用仲裁器启动新的副本集之前,不需要更改集群范围内的写入关注。

Consider Fault Tolerance考虑容错

Fault tolerance for a replica set is the number of members that can become unavailable and still leave enough members in the set to elect a primary. 副本集的容错是指可以变得不可用并且在集中仍保留足够成员以选择主成员的成员数。In other words, it is the difference between the number of members in the set and the majority of voting members needed to elect a primary. 换言之,这是选举初选所需的成员人数和majority投票成员之间的差额。Without a primary, a replica set cannot accept write operations. Fault tolerance is an effect of replica set size, but the relationship is not direct. See the following table:如果没有主副本,副本集就无法接受写操作。容错是副本集大小的影响,但这种关系并不直接。见下表:

Number of Members成员数量Majority Required to Elect a New Primary选举新的初选所需的多数票Fault Tolerance容错
321
431
532
642

Adding a member to the replica set does not always increase the fault tolerance. 向副本集中添加成员并不总是增加容错能力。However, in these cases, additional members can provide support for dedicated functions, such as backups or reporting.但是,在这些情况下,其他成员可以为备份或报告等专用功能提供支持。

Starting in version 4.2.1, rs.status() returns majorityVoteCount for the replica set.从4.2.1版本开始,rs.status()返回副本集的majorityVoteCount

Use Hidden and Delayed Members for Dedicated Functions将隐藏和延迟成员用于专用功能

Add hidden or delayed members to support dedicated functions, such as backup or reporting.添加隐藏延迟成员以支持专用功能,如备份或报告。

Read-Heavy Applications读取繁重的应用程序

A replica set is designed for high availability and redundancy. 复制副本集是为实现高可用性和冗余性而设计的。In most cases secondary members operate under similar loads as the primary. 在大多数情况下,次要构件在与主要构件相似的荷载下运行。You should not direct reads to secondaries.你不应该直接读到中学。

If you have a read-heavy application, consider using Cluster-to-Cluster Sync to replicate data to another cluster for reading.如果您有一个读取量很大的应用程序,请考虑使用群集到群集同步将数据复制到另一个群集进行读取。

For more information on secondary read modes, see: secondary and secondaryPreferred.有关辅助读取模式的更多信息,请参阅:secondarysecondaryPreferred

Add Capacity Ahead of Demand提前增加容量

The existing members of a replica set must have spare capacity to support adding a new member. 副本集的现有成员必须具有备用容量才能支持添加新成员。Always add new members before the current demand saturates the capacity of the set.总是在当前需求使集合容量饱和之前添加新成员。

Distribute Members Geographically按地域分配成员

To protect your data in case of a data center failure, keep at least one member in an alternate data center. 若要在数据中心出现故障时保护您的数据,请在备用数据中心中保留至少一个成员。If possible, use an odd number of data centers, and choose a distribution of members that maximizes the likelihood that even with a loss of a data center, the remaining replica set members can form a majority or at minimum, provide a copy of your data.如果可能,请使用奇数个数据中心,并选择一种成员分布,以最大限度地提高即使丢失数据中心,其余副本集成员也可以构成多数或至少提供数据副本的可能性。

Note

Distributing replica set members across two data centers provides benefit over a single data center. In a two data center distribution,将复制副本集成员分布在两个数据中心提供了优于单个数据中心的优势。在双数据中心分布中,

  • If one of the data centers goes down, the data is still available for reads unlike a single data center distribution.如果其中一个数据中心出现故障,则与单个数据中心分布不同,数据仍可用于读取。
  • If the data center with a minority of the members goes down, the replica set can still serve write operations as well as read operations.如果拥有少数成员的数据中心宕机,副本集仍然可以提供写操作和读操作。
  • However, if the data center with the majority of the members goes down, the replica set becomes read-only.但是,如果包含大多数成员的数据中心出现故障,则副本集将变为只读。

If possible, distribute members across at least three data centers. For config server replica sets (CSRS), the best practice is to distribute across three (or more depending on the number of members) centers. 如果可能,将成员分布在至少三个数据中心。对于配置服务器副本集(CSRS),最佳做法是分布在三个(或更多,具体取决于成员数量)中心。If the cost of the third data center is prohibitive, one distribution possibility is to evenly distribute the data bearing members across the two data centers and store the remaining member in the cloud if your company policy allows.如果第三个数据中心的成本过高,一种分发可能性是在两个数据中心之间平均分配数据承载成员,并在公司政策允许的情况下将剩余成员存储在云中。

To ensure that the members in your main data center be elected primary before the members in the alternate data center, set the members[n].priority of the members in the alternate data center to be lower than that of the members in the primary data center.要确保主数据中心中的成员先于备用数据中心中成员被选为主,请将备用数据中心成员的members[n].priority设置为低于主数据中心成员。

For more information, see Replica Sets Distributed Across Two or More Data Centers有关详细信息,请参阅分布在两个或多个数据中心的副本集

Target Operations with Tag Sets带有标记集的目标操作

Use replica set tag sets to target read operations to specific members or to customize write concern to request acknowledgement from specific members.使用副本集标记集可将读取操作定向到特定成员,或自定义写入关注以请求特定成员的确认。

Use Journaling to Protect Against Power Failures使用日志记录防止电源故障

MongoDB enables journaling by default. Journaling protects against data loss in the event of service interruptions, such as power failures and unexpected reboots.MongoDB默认启用日志记录。日志记录可在服务中断(如电源故障和意外重新启动)的情况下防止数据丢失。

Hostnames主机名

Important

To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. 若要避免由于IP地址更改而导致配置更新,请使用DNS主机名而不是IP地址。It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.在配置复制集成员或分片集群成员时,使用DNS主机名而不是IP地址尤为重要。

Use hostnames instead of IP addresses to configure clusters across a split network horizon. 使用主机名而不是IP地址来跨拆分的网络范围配置群集。Starting in MongoDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.从MongoDB 5.0开始,只配置了IP地址的节点将无法通过启动验证,也不会启动。

Replica Set Naming副本集命名

If your application connects to more than one replica set, each set must have a distinct name. Some drivers group replica set connections by replica set name.如果应用程序连接到多个复制副本集,则每个复制副本集都必须具有不同的名称。某些驱动程序按副本集名称对副本集连接进行分组。

Deployment Patterns部署模式

The following documents describe common replica set deployment patterns. 以下文档介绍了常见的复制副本集部署模式。Other patterns are possible and effective depending on the application's requirements. 根据应用程序的要求,其他模式也是可能的,也是有效的。If needed, combine features of each architecture in your own deployment:如果需要,在您自己的部署中结合每个体系结构的功能:

Three Member Replica Sets三个成员副本集
Three-member replica sets provide the minimum recommended architecture for a replica set.三个成员复制副本集提供了复制副本集的最低推荐体系结构。
Replica Sets Distributed Across Two or More Data Centers分布在两个或多个数据中心的副本集
Geographically distributed sets include members in multiple locations to protect against facility-specific failures, such as power outages.按地理位置分布的集合包括位于多个位置的成员,以防止设施特定的故障,如停电。