15

I am using MongoDB via its driver for node.js

I typically open a connection (via the connect() method) any time I need to perform an operation and close it (via the close() method) as soon as I am finished. In my programs, as natural, I need to perform many operations against the MongoDB and therefore it happens that I open and close many times the connection.

I am wondering whether this is a good practice or whether it would be better to open the connection as the first operation is executed, store it in a variable and use the already opened connections for the following operations closing it when the program ends.

Any advice is very much appreciated.

Picci
  • 16,775
  • 13
  • 70
  • 113

1 Answers1

20

It is best practice to open the connection once, store it in a variable and close it at the end. MongoDB explicitly recommends this. This is the reason why opening and closing a connection is part of the MongoDB API rather than have it happen automatically for each query.

Opening and closing connections for each query will introduce a significant overhead both in terms of performance (CPU + latency), network traffic, memory management (creating and deleting objects), not only for the client but also for the server itself, which also impacts other clients.

About the terminology of connection: in some drivers like Java, what is actually created and stored in a variable is not a physical connection, but a MongoClient instance. It looks like a connection from an abstract (API) perspective, but it actually encapsulates the actual physical connection(s) and hides complexity from the user.

Creating the MongoClient instance only once, for the drivers that support this, will also allow you to benefit from connection pooling where the driver maintains active connections in parallel for you, so that you also only need to create one MongoClient instance across multiple threads.

Ghislain Fourny
  • 6,971
  • 1
  • 30
  • 37
  • 1
    Thanks for your answer. Still the last point is not totally clear to me. I am using the node.js driver which I think supports connection pooling. My understanding is that connection pooling allows programs to ask for connections anytime they need, while the driver is responsible of maintaining the pool. If I need to store the connection within my program it seems to me that I am not using the connection pooling concept. Could you pls explain me. Thanks in advance – Picci Aug 22 '16 at 09:36
  • Happy to help! Unfortunately, I am not familiar with node.js apart that I heard it's single-threaded. Connection pooling may happen if it still "pipelines" queries in some way. In any case, opening and closing the connection only once should give you a significant performance improvement even in a single thread and even with a single connection. If the API was designed right, it should simply "just work". – Ghislain Fourny Aug 22 '16 at 09:49
  • 1
    Maybe this should help remove some confusion: what is stored in the variable (at least in Java) is not a connection in itself, it's the `MongoClient` object that encapsulates the entire connection and connection pool machinery. – Ghislain Fourny Aug 22 '16 at 10:00