Node.js – Mongoose Connection Pool


I noticed in the Mongoose docs that there is support for a connection pool.

Considering that node is single threaded why is there a connection pool?
What's the lifecycle of connections in the pool?

Best Solution

Connection pools don't have anything to do with async vs sync -- it just works like so:

  • You can specify an amount of open connections to maintain to your database (let's say 10).
  • Each time your Node JS code makes a query, if possible, it'll use one of the already-open 10 connections to make this request -- this way you can avoid the overhead of opening a new database connection for each query.

Maintaining a connection pool is essentially maintaining an array of db connection objects, and picking unused ones for every query. It's not actually effecting threads or processes at all =)