I
I
iNeedHelp2016-07-14 12:07:52
MongoDB
iNeedHelp, 2016-07-14 12:07:52

Is a request pool necessary?

Hello. I am working with mongo-native-driver for Node.js. This is how I start working with the database:

const Db = require('mongodb').Db,
          MongoClient = require('mongodb').MongoClient,
          Server = require('mongodb').Server;

const db = new Db('users', new Server('localhost', 27017));
db.open(function(err, connect) {
  let users = connect.collection('users');
}

Further, I use this variable users everywhere for any manipulation with the database. Actually, the questions are:
1) by default, my database has a poolSize = 5 property. Do I understand correctly that if five users send a request to the database at the same time, then everything will be fine, if more, then there will be problems? That is the standard pool of requests is provided?
2) Is it correct to remember the users variable, and then use it everywhere? Or is it necessary to remember only the connect variable, and perform connect.collection('users') for each action with the database?
thank you all for your help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
napa3um, 2016-07-14
@iNeedHelp

1) The connection pool actually means how many concurrent (simultaneous) requests to the database can be sent from a given node instance (in the script of which you describe the connection). But this number is not the only limitation, the mongo itself has a setting in the config, how many connections it can accept, plus not all requests can be processed at the same time (if they claim shared resources - https://docs.mongodb.com/manual/faq/ concurrency/ ). Plus, you can run several node instances in a cluster (then the pool size, in fact, will be multiplied by the number of node instances - https://nodejs.org/api/cluster.html). Therefore, it is not customary to rely on mongo parallelism, it is customary to architecture the application so that user requests do not require "powerful" operations from mongo ("powerful" operations are usually queued, immediately "letting go" of the client). The default value of 5 per node instance is fine for most cases.
2) If the application architecture allows - use the reference to the collection cached in the variable to execute queries. This will slightly (slightly) speed up accessing the collection by avoiding a few simple checks, and also potentially systematize the code a little.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question