简体   繁体   中英

how does mongodb replica set work with nodejs-mongoose?

Techstack used nodejs,mongoose,mongodb

i'm working on product that handles many DBrequests. During beginning of every month the db requests are high due to high read/write requests (bulk data processing). The number of records in each collection's targeted for serving these read/write requests are quite high. Read is high but write is not that high.

So the cpu utilization on the instance in which mongodb is running reaches the dangerzone(above 90%) during these times. The only thing that gets me through these times is HOPE (yes, hoping that instance will not crash).

Rather than scaling vertically, i'm looking for solutions to scale horizontally (not a revolutionary thought). i looked at replicaset and sharding . This question is only related to replicaSet.

i went through documents and i feel like the understanding i have on replicaset is not really the way it might work.

i have configured my replicaset with below configuration. i simply want to add one more instance because as per the understanding i have right now, if i add one more instance then my database can handle more read requests by distributing the load which could minimize the cpuUtilization by atleast 30% on primaryNode. is this understanding correct or wrong? Please share your thoughts

var configuration = {
    _id : "testReplicaDB",
    members:[
        {_id:0,host:"localhost:12017"},
        {_id:1,host:"localhost:12018",arbiterOnly:true,buildIndexes:false},
        {_id:2,host:"localhost:12019"}
    ]
}

When i broughtup the replicaset with above config and ran my nodejs-mongoose code, i ran into this issue . Resolution they are proposing is to change the above config into

var configuration = {
    _id : "testReplicaDB",
    members:[
        {_id:0,host:"validdomain.com:12017"},
        {_id:1,host:"validdomain.com:12018",arbiterOnly:true,buildIndexes:false},
        {_id:2,host:"validdomain.com:12019"}
    ]
}

Question 1 (related to the coding written in nodejsproject with mongoose library(for handling db) which connects to the replicaSet)

const URI = mongodb://167.99.21.9:12017,167.99.21.9:12019/${DB};

i have to specify both uri's of my mongodb instances in mongoose connection URI String .

When i look at my nodejs-mongoose code that will connect to the replicaSet, i have many doubts on how it might handle the multipleNode.

How does mongoose know which ip is the primaryNode?

Lets assume 167.99.21.9:12019 is primaryNode and rs.slaveOk(false) on secondaryReplica, so secondaryNode cannot serve readRequests.

In this situation, does mongoose trigger to the first uri( 167.99.21.9:12017 ) and this instance would redirect to the primaryNode or will the request comeback to mongoose and then mongoose will trigger another request to the 167.99.21.9:12019 ?

Question 2

This docLink mention's that data redundancy enables to handle high read requests. Lets assume, read is enabled for secondaryNode, and

  • Lets assume the case when mongoose triggers a request to primaryNode and primaryNode was getting bombarded at that time with read/write requests but secondaryNode is free(doing nothing), then will mongodb automatically redirect the request to secondaryNode or will this request fail and redirect back to mongoose, so that the burden will be on mongoose to trigger another request to the next available Node?
  • can mongoose automatically know which Node in the replicaSet is free?

Question 3

Assuming both 167.99.21.9:12017 & 167.99.21.9:12019 instances are available for read requests with ReadPreference.SecondaryPreferred or ReadPreference.nearest , will the load get distributed when secondaryNode gets bombarded with readRequests and primaryNode is like 20% utilization? is this the case? or is my understanding wrong? Can the replicaSet act as a loadbalancer? if not, how to make it balance the load?

Question 4

var configuration = {
    _id : "testReplicaDB",
    members:[
        {_id:0,host:"validdomain.com:12017"},
        {_id:1,host:"validdomain.com:12018",arbiterOnly:true,buildIndexes:false},
        {_id:2,host:"validdomain.com:12019"}
    ]
}

You can see the DNS name in the configuration, does this mean that when primaryNode redirects a request to secondaryNode, DNS resolution will happen and then using that IP which corresponds to secondaryNode, the request will be redirected to secondaryNode? is my understanding correct or wrong? (if my understanding is correct, this is going to fireup another set of questions)

:|

i could've missed many details while reading the docs. This is my last hope of getting answers. So please share if you know the answers to any of these.

if this is the case, then how does mongoose know which ip is the primaryReplicaset?

There is no "primary replica set", there can be however a primary in a replica set.

Each MongoDB driver queries all of the hosts specified in the connection string to discover the members of the replica set (in case one or more of the hosts is unavailable for whatever reason). When any member of the replica set responds, it does so with the full list of current members of the replica set. The driver then knows what the replica set members are, and which of them is currently primary (if any).

secondaryReplica cannot serve readRequests

This is not at all true. Any data-bearing node can fulfill read requests, IF the application provided a suitable read preference .

In this situation, does mongoose trigger to the first uri(167.99.21.9:12017) and this instance would redirect to the primaryReplicaset or will the request comeback to mongoose and then mongoose will trigger another request to the 167.99.21.9:12019?

mongoose does not directly talk to the database. It uses the driver (node driver for MongoDB) to do so. The driver has connections to all replica set members, and sends the requests to the appropriate node.

For example, if you specified a primary read preference, the driver would send that query to the primary if one exists. If you specified a secondary read preference, the driver would send that query to a secondary if one exists.

i'm assuming that when both 167.99.21.9:12017 & 167.99.21.9:12019 instances are available for read requests with ReadPreference.SecondaryPreferred or ReadPreference.nearest

Correct, any node can fulfill those.

the load could get distributed across

Yes and no. In general replicas may have stale data. If you require current data, you must read from the primary. If you do not require current data, you may read from secondaries.

how to make it balance the load?

You can make your application balance the load by using secondary or nearest reads, assuming it is OK for your application to receive stale data.

if mongoose triggers a request to primaryReplica and primaryReplica is bombarded with read/write requests and secondaryReplica is free(doing nothing), then will mongodb automatically redirect the request to secondaryReplica?

No, a primary read will not be changed to a secondary read.

Especially in the scenario you are describing, the secondary is likely to be stale, thus a secondary read is likely to produce wrong results.

can mongoose automatically know which replica is free?

mongoose does not track deployment state, the driver is responsible for this. There is limited support in drivers for choosing a "less loaded" node, although this is measured based on network latency and not CPU/memory/disk load and only applies to the nearest read preference .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM