Cyril F
Cyril F

Reputation: 1882

Sharing a DB connection on the entire app or connect to DB on each request?

I'm doing an API with Nodejs using Restify. For the DB I'm using Mongodb (with mongoose).

I was wondering, what the best solution between sharing a db connection to my entire app or connecting to the db on each request ?

For now, I'm using the second option of this answer : sharing db connection But I've seen a different pattern here : Node.js Web Application with Storage on MongoDB

I can't figure out, what is the best architecture ? A list of pros and cons could be a great help.

Upvotes: 0

Views: 242

Answers (1)

freakish
freakish

Reputation: 56467

Of course keeping one connection ( or pool of connections if mongoose supports it ) and reusing it is better, simply because creating connection on each request eats resources.

Upvotes: 2

Related Questions