newb
newb

Reputation: 21

Read from mongodb secondary node when no connection with primary

We have mongodb replica set with 3 instances, where Primary is in datacenter D1, and secondary nodes on datacenter D2. We dont need any failover option in our setup, and configured it as described at https://docs.mongodb.com/manual/tutorial/configure-secondary-only-replica-set-member/ .

An application "A" running on top of secondary nodes within the same datacenter D2 is using mongoose, and we specified (with options "nearest" or "secondary") to read the data from the secondary nodes.

We are facing these problems:

1) Is it possible to make "A" to read from a specific secondary nodes, without specifying in the connection configurations where is the Primary mongoDB node?

2) How can we make the "A" application still read from the secondary node, if the connection between the datacenters D1 and D2 is lost, so the mongoDB Primary node is not reachable/visible anymore? As far as I understand it does not work because even the "A" is configured to read from a secondary, the mongoDB still needs to perform a kind of ping/arbitraging between primary and secondaries before the actual read operation can be performed.

3) Is it possible/recommended to have an application in datacenter D2, which will perform write operations to the mongodb replica-set Primary instance directly as a standalone instance, and not as a part of replica-set?

Versions: mongodb 3.2.9, mongoose 4.5.9

Upvotes: 1

Views: 902

Answers (1)

JJussi
JJussi

Reputation: 1580

  1. Yes, read preference with tags

  2. With network partition with D1 and D2, primary is automatically transferred to one of those D2 nodes, that one what is most "up-to-date" against primary. With "priority settings" you can steer this selection. As long as 2 nodes out of 3 are up, one of those 2 is voted as primary.

Let's imagine you have three nodes:

  • D1.N0
  • D2.N1
  • D2.N2

You should prioritise the nodes:

  • D1.N0.priority = 3
  • D2.N1.priority = 2
  • D2.N2.priority = 1

Now, as long as D1 is reachable then D1.N0 is primary. When we have network partition between D1 and D2, D2.N1 becomes primary and will D2.N2 stay as secondary. When the network partition is over, D1.N0 will "catch up" missing data by reading D2.N1 opLog and will become primary again and D2.N1 will be secondary.

  1. If you have a replica set, you must always write to replica set and NEVER directly to individual node!

Upvotes: 1

Related Questions