Yaron Naveh
Yaron Naveh

Reputation: 24406

mongodb in node.js connections today

When using the native mongo.db driver for node, should I open 1 connection per application, per page "serve", or open and close it whenever I need?

I've seen a few older answers but I know the project is always developing so I want to know what it the status today.

Upvotes: 1

Views: 633

Answers (1)

Stennie
Stennie

Reputation: 65343

This isn't a situation that will change; opening a new connection to a server will be less performant than using an established connection.

Note: this is the general case for server applications, and not specific to MongoDB.

Typical overhead includes:

  • resolving server names to IPs
  • establishing network connection to server
  • per connection memory allocated on the server

For MongoDB in particular:

  • opening a new connection means a new socket connection and thread on the server
  • each connection (as of MongoDB 2.0) allocates 1Mb of RAM on the server (see also: Checking Memory Usage)
  • there is a per process limit on open files/connections (see also: Too Many Open Files)

For the MongoDB Node.js driver you can take advantage of connection pooling by setting the poolSize in the constructor. A blog post with an example of using this: Node.js: Connection Pools and MongoDB.

Upvotes: 2

Related Questions