All of my microservices need to access these databases.
To manage this, I created a shared npm package that handles MongoDB/Mongoose connections, and I import and call it in every service inside the index.ts file.
The problem is: every time a service starts, it creates multiple MongoDB connections, and after some time I can see a lot of open connections in MongoDB, which is not efficient.
I want to handle this in a professional way, like:
reusing the same connection (singleton)
preventing duplicate connections
handling reconnect logic properly
managing multiple DBs with one connection
Can someone guide me on the best practice for managing MongoDB/Mongoose connections in a microservice architecture?
Top comments (0)