A
A
Alexander N++2016-09-16 09:51:38
MongoDB
Alexander N++, 2016-09-16 09:51:38

Nodejs + mongodb connection at application start or many when needed, dilemma how to be?

Good afternoon, I have logging in my mongo, on average there are 300-1000 calls per minute, each time I create a connection, write down the log and cut myself off.
Should I use one connection at program start?
When I used mysql, there were pool in the mongo pool of connections, as there are no connections in the mysql library.
here's the dilemma...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander N++, 2016-09-22
@sanchezzzhak

Standard mongodb is all included out of the box.
I took the connection to the start of the program, what we have: we increased the upload speed, the load on the hdd decreased by 15%. (total load was 22-23% )

async.parallel([
    function(callback){
        console.log('mongodb callback run');
        var mongodb_url = 'mongodb://localhost:27017/database';
        var options = { server: { auto_reconnect:true, poolSize: 20, socketOptions: { connectTimeoutMS: 2000 } } };
        MongoClient.connect(mongodb_url, options ,function(err, db) {
            assert.equal(null, err);
            mongodb = db;
            callback(null,{event:'init_mongo', reason: 'mongodb connection success'});
        });
    },
  // ....
],function(err,result){
     app.run()  //sertver listen
});

It is beneficial if you constantly receive calls 24/7

N
napa3um, 2016-09-16
@napa3um

The logic of working with MongoDB in the server on Node.JS
(Also, consider using Mongo via mongoose.js, it is even more convenient.)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question