Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS cluster and database connection

I have been experimenting clustering in NodeJS (https://nodejs.org/api/cluster.html) and was able to cluster my server.

I have a mongodb connection which gets established when server starts

Earlier this was not a problem since it gets established once (without cluster).

I think, with clustering it establishes once for every cluster (I am unclear about this).

So, should the connection be done once for the entire server (or) once for every worker (or) only for master process?

Which is the recommended way and how can it be achieved?

Thanks in advance.

like image 681
Vignesh T.V. Avatar asked Oct 30 '22 14:10

Vignesh T.V.


1 Answers

Connection should be done once for every process. The important thing is however not to mix sessions. I would recommend connect-mongo for sessions and once client establishes session on one of the workers, it should not use any other instance for operations related to this client. This way you can also keep cache for clients on their respectable server instances.

like image 128
Sulejman Sarajlija Avatar answered Nov 15 '22 06:11

Sulejman Sarajlija