On this page本页内容
MongoDB drivers use a Server Selection algorithm to choose which replica set member to use or, when connected to multiple mongos instances, which mongos instance to use.
Server selection occurs once per operation.
Note
Multi-document transactions that contain read operations must use read preference primary. All operations in a given transaction must route to the same member.
Server selection occurs once per operation and is governed by the read preference and localThresholdMS settings to determine member eligibility for reads. The read preference is re-evaluated for each operation.
| Read Preference Mode | Selection Process |
|---|---|
primary (Default) |
|
secondary |
|
nearest |
|
primaryPreferred |
|
secondaryPreferred |
|
If there is more than one mongos instance in the connection seed list, the driver determines which mongos is the “closest” (i.e. the member with the lowest average network round-trip-time) and calculates the latency window by adding the average round-trip-time of this “closest” mongos instance and the localThresholdMS. The driver will load balance randomly across the mongos instances that fall within the latency window.
For sharded clusters that have replica set shards, mongos applies the read preference when reading from the shards. Server selection is governed by the read preference and replication.localPingThresholdMs settings. The read preference is re-evaluated for each operation.
Starting in version 4.4, mongos supports hedged reads for non-primary read preferences modes. That is, mongos can send an additional read to another member, if available, to hedge the read operation if using non-primary read preferences. The additional read sent to hedge the read operation uses the maxTimeMS value of maxTimeMSForHedgedReads.
Hedged reads are supported for the following operations:
To use hedged reads:
mongos must have enabled support for hedged reads (Default). See the readHedgingMode parameter.primary read preferences must enable use of hedged reads.| Read Preference Mode | Selection Process |
|---|---|
primary (Default) |
|
secondary |
|
nearest |
|
primaryPreferred |
|
secondaryPreferred |
|
| [1] | (1, 2) The default threshold value is 15 milliseconds. |