What's the expensive part of a MongoDB connection?

Hi,

what’s the most expensive part in terms of time consumed to prepare a connection to issue a command/query against a MongoDB database collection - speaking of the Node.js driver?

Is it the MongoClient.connect() call, the client.db('dbName') or the db.collection('collectionName') part?

Thanks,

Alex

the connect one as some network traffic is involve. the other 2 do simple local house keeping to establish the name space for other commands.

Welcome to the community, Alex! Glad to have you here.

Great question.

The most expensive would be the connect call like Steeve mentioned. And, the reason it’s expensive is, clients connect to the database with a regular TCP connection which requires the three-way handshake to establish a new connection. On top of that, each connection need to be authenticated and authorized, adding to the delay. In order to avoid this, most applications tend to reuse existing open connections to decrease the latency. Mongo drivers achieve this via connection pooling to help use the resources efficiently.

Now, about your other two calls. They should be less expensive assuming they will piggyback on an existing open connection. Hope this helps.

Thanks,
Mahi

P.S. I’ve oversimplified the process without accounting for optimizations in TCP and other aspects, but overall, initial setup is expensive compared to reuse.

Thanks @steevej @mahisatya

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.