Node.js cluster and with mongodb as store


I've got a problem to set up clustered node.js / app, with store, based not on Redis but on MongoDB. I need to mention that my app uses Mongo to store data. So I don't want to setup another DB just to make works across multiple process.

I found an option -, but it looks like I can't make this work with node clusters. I'm using node: 0.10.10, with 0.9
I'm looking for any solution that will let me use MongoDB as a store for that runs on separate processes.

Counting on you, thanks.

Problem courtesy of: Aleksey Anatskiy


Any of clustered worker can easily access MongoDB using mongodb or mongoose in order to get required data. As well it can store that data locally as a cache.
Once client is connected it will be bound to one of workers and will not swap workers during execution.
In order to share sessions you need to create session middleware for that will ask for session details from MongoDB from one of the workers. Check this answer which explains how to access session data in from shared session details. And in order to make it clustered, instead of MemoryStorage use connect-mongo that allows to store session data within mongo and it will make session details shared over workers.

Solution courtesy of: moka


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.