Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose: Read on ReplicaSet

I have a mongodb replica set from which I want to read data from primary and secondary db.

I have used this command to connect to the db:

mongoose.connect('mongodb://user:[email protected],user:[email protected],user:[email protected]/PanPanDB?replicaSet=rs0&readPreference=nearest');

It doesn't work.. My application continues to read from the primary.. Any suggestion please?

like image 923
Michele Spina Avatar asked Dec 21 '13 01:12

Michele Spina


People also ask

How do I read secondary data in MongoDB?

Reading from secondary (up-to-date-data) Method 1: Don't want to read stale data, keep all your secondary up-to-date. To achieve this we have to get write concern acknowledged by all the secondaries i.e write-concern = total number of replicaset including primary.

What is ReplicaSet in MongoDB?

In simple terms, MongoDB replication is the process of creating a copy of the same data set in more than one MongoDB server. This can be achieved by using a Replica Set. A replica set is a group of MongoDB instances that maintain the same data set and pertain to any mongod process.

What is useUnifiedTopology in mongoose?

serverSelectionTimeoutMS - With useUnifiedTopology , the MongoDB driver will try to find a server to send any given operation to, and keep retrying for serverSelectionTimeoutMS milliseconds. If not set, the MongoDB driver defaults to using 30000 (30 seconds).

Is Mongoose Connect async?

The connect() method provided by the Mongoose supports both JavaScript promises and async-await syntax.


2 Answers

If you want to read from a secondary, you should set your read preference to either of:

  • secondaryPreferred - In most situations, operations read from secondary members but if no secondary members are available, operations read from the primary.

  • secondary - All operations read from the secondary members of the replica set.

Reading from nearest as per your example will select the nearest member by ping time (which could be either the primary or a secondary).

Caveats

When using any read preference other than primary, you need to be aware of potential issues with eventual consistency that may affect your application logic. For example, if you are reading from a secondary there may be changes on the primary that have not replicated to that secondary yet.

If you are concerned about stronger consistency when reading from secondaries you should review the Write Concern for Replica Sets documentation.

Since secondaries have to write the same data as the primary, reading from secondaries may not improve performance unless your application is very read heavy or is fine with eventual consistency.

like image 185
Stennie Avatar answered Sep 17 '22 16:09

Stennie


Following the documentation found on MongoDB website and on Mongoose web site, you can add this instruction for configuring the ReadPreference on Mongoose:

var opts = { replSet: {readPreference: 'ReadPreference.NEAREST'} };

mongoose.connect('mongodb://###:###@###:###/###', opts);

This has been tested using Mongoose version 3.8.9

like image 22
emas Avatar answered Sep 17 '22 16:09

emas