Too many connections to mongodb

I am using mongodb atlas as database hosting. There is a connection limit at atlas. So I am facing difficulties because of huge mongodb open connections.
I tried to optimize my nodejs application as much as possible. But I can not reduce too many connections of mongodb.

Currently I am not aware of your codebase which you are referring to Sir. In general when we are using a resources like database we usually use those using some container better known as ConnectionPooling.

https://www.mongodb.com/docs/manual/administration/connection-pool-overview/

Idea over here is you create x number of active connections and connectionmanager will handle everything under the hoods.

When ever application wants to connect to DB our code will get connection object from this pooled connection and close the connection once done with DB operations (More like recalling connection object in pool so that it can be reused by some other code which need access of DB)

Let me give you a scenario:

NON connection pooled (Assuming you dont close connections)-

code snippet which need db 1 -> db connection no. 1

code snippet which need db 2 -> db connection no. 2

code snippet which need db 3 -> db connection no. 3

connection pooled

code snippet which need db 1 -> db connection no. 1

code snippet which need db 2 -> db connection no. 1

code snippet which need db -> db connection no. 2 (maybe)

Idea here is we are reusing the connection for DB which makes it very efficient in terms of actual number of connections which are visible to server.

Hope this helps.

Leave a Comment