简体   繁体   中英

NodeJS mongodb: use global connection or local connection

I have a set of nodejs scripts that does a lot of queries, inserts and updates to mongodb. I'm facing a choice whether to use a global db connection object and pass it to different functions or in each functions get its own db connection object and close it upon completion.

  • Global db connection object. Advantage is that connection only need to be established once. Subsequent function calls will enjoy performance again by saving time in connecting to db. Problem is that it's difficult to determine when to close the connection. My scripts are complex and have a few levels of function calls. Some functions need to perform further tasks even after the callback function is triggered. Without closing the connection, the parent script won't know when to exit .

  • Establish a new db connection object in each individual function. The biggest worry of this approach is performance. I tested that each connection took about 60ms to be established. Adding this up in all function calls could result in a huge performance degradation.

I'm inclined to first approach but need to figure out a way to make the parent script exit upon completion of all tasks.

Adding pseudo code to illustrate my script high level structure.

//db is a global connection object. 
function entry_point(db) {
    task1(db, callback){
        loop {
            sub_task(db, callback2){
                dosomething
                callback2
                dosomeotherthings
            }
            callback
        }
        dosomethingagain
    }

    task2(db, callbac) //Similar call trees, maybe with more levels. 
    task3.... 
    ....
}

You can use async to run your asyncronous jobs... and finally to close global mongodb connection. Ex:

async.auto( {
        'mongo': [ function( callback ) {
            /// initiate mongodb and call: callback( null );
        } ],
        'task1': [ 'mongo', function( callback ) {
            // this task wait for 'mongo' to be initiated
            // run your functions/code and call: callback( null );
        } ],
        'task2': [ 'mongo', function( callback ) {
            /// same as task1
        } ],
        'task3': [ 'mongo', 'task1', function( callback ) {
            /// will run after 'mongo' and 'task1' has completed
        } ],
    },
    function( err, rets ) {
        /// all task are done
        /// now you can close your mongodb connection / process.exit( 0 );
    }
);

The proper way is a global connection that you reuse across your whole application.

The driver has a builtin connection pool (default size 5) that reuse existing collection.

Also your application, for HA reason will be connected to a replica-set (or multiple mongos) with multiple TCP sockets to support fail over, and as you probably know creating new socket is quite expensive. (So you do not want to "create new client")

You can find more information here:

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