9

I am using node.js & MongoDB, and I have asynchronous code (async.queue) to update MongoDB.

When all the tasks in the queue is finished, I call

db.close()   // db is a Mongo client instance

hence all connections used by the tasks are closed. However, in rare condition, I found there are open connections that are never closed seen in mongoDB logs. So after a few weeks, there will be hundred of connections never closed.

I researched and found that maybe I should set this option maxIdleTimeMS, but this option is not supported by all drivers (node.js driver does not support this option).

Even if I can fine tune my code to make sure there is no such condition that close() is not called. I am still wondering what if an app does not call db.close() for some reason (or as an extra insurance), is there any way to prevent hanging connections in MongoDB?

Joe Huang
  • 6,296
  • 7
  • 48
  • 81
  • Are you doing `db.close()` on the `drain()` callback? – Rahat Mahbub Apr 27 '16 at 02:24
  • @RahatMahbub yes, on the `drain()` callback, but my code relies two `drain()`s, so sometimes I guess the coordination of my code is not done perfectly, so `db.close()` is not called. – Joe Huang Apr 27 '16 at 03:19
  • 1
    Generally, MongoDB connections are kept because there is a high overhead on establishing new connections. If you have a fixed pool of connections, you will be able to perform your async.queue concurrently and just keep them forever by not doing a `db.close()`. When you call your async.queue function again, only connections from these pool will be reused and no new connections will be created. This way you save new connection time overhead and not have a 100 idle connection after a week. – Rahat Mahbub Apr 27 '16 at 08:41
  • @RahatMahbub understood, my question is ... as an extra insurance, is there any way to make sure connections will be closed even if the app forgets to call `db.close()`? – Joe Huang Apr 27 '16 at 08:48
  • maxIdleTimeMS is managed on client side. So, no unless your driver supports it. – Rahat Mahbub Apr 27 '16 at 08:53
  • Another thing you could do is keep a count of your actions completed and then close the connection based on that. – Rahat Mahbub Apr 27 '16 at 08:58
  • On server side you can limit the maximum number of connection using parameter in config file net.maxIncomingConnections, this will not solve your problem but restricts number of open connections – achuth Apr 28 '16 at 06:38
  • Why not just call `MongoClient.connect` once during start up and don't even call `db.close()`? That's the typical pattern. – JohnnyHK Apr 30 '16 at 16:23
  • @JohnnyHK understood, but isn't it weird that if there is no method to make sure there will be no hanging connections due to coding mistakes? – Joe Huang Jul 05 '16 at 06:23

1 Answers1

3

nodejs mongodb driver drops connection when idle

Handling MongoDB disconnect/reconnects from Node

looks like after some time of idle, connection is closed.

It is not clear which driver is used, but in one of that post there is link to docs:

http://mongodb.github.io/node-mongodb-native/api-generated/server.html#server

Set keepAlive to 0, and your connection will be terminated...

Community
  • 1
  • 1
Michał Zaborowski
  • 3,911
  • 2
  • 19
  • 39
  • I tried your suggestion by setting 0 to keepAlive, but connections still hangs if I don't close them properly. – Joe Huang Jul 05 '16 at 06:21
  • 1
    @JoeHuang That is expected. Try with connection timeout. From server's point of view it is hard to tell what client is going to do. Killing connection just like that - is something rude - I think. :) – Michał Zaborowski Jul 05 '16 at 15:15