Question

Suppose there is a replica set of three nodes: 1 primary P and 2 secondaries S1 and S2. Suppose also that P and S1 are located in one data center DC1 and S2 in another data center DC2.

Suppose a client is located in DC1 (i.e. in the same data center as P and S1) and S1 is down. Where will the client queries go ? P or S2 ?

Was it helpful?

Solution

secondaryPreferred will choose the secondary, even if it's further away (I'm assuming that S2 has a lower priority because it probably shouldn't be primary, so in your scenario S2 is the secondary and that it's further away in the latency sense).

You can instead use tag sets for data-center awareness / geographically distributed members or the nearest read-preference, which will select the server that is closer, irrespective of whether it's a primary or secondary.

OTHER TIPS

From the MongoDB documentation:

secondaryPreferred

In most situations, operations read from secondary members, but in situations where the set consists of a single primary (and no other members), the read operation will use the set’s primary.

When the read preference includes a tag set, the client attempts to find a secondary member that matches the specified tag set and directs reads to a random secondary from among the nearest group. If no secondaries have matching tags, the client ignores tags and reads from the primary.

Read operations using the secondaryPreferred mode may return stale data.


My guess is that you would get the data from S2, since that is what you have asked for with the preferred suffix. The secondary option excludes the primary entirely but while the secondaryPreferred allows for primary to be used in case of no secondaries.

Though by using tags for your servers in the same data center you can make the driver read from primary even though a secondary is present in the rest of the replica set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top