Node.js + Cluster + Mongoose

1,809 views
Skip to first unread message

Jorgen B.

unread,
Apr 1, 2011, 4:36:35 AM4/1/11
to nodejs
Hello,

I'm learning Node.js by writing a simple HTTP server application using
Cluster plus MongoDB through Mongoose for sharing state between
different Cluster workers.

Is there a way to have one MongoDB connection per Cluster worker?
Currently I'm setting up a single connection using mongoose.connect()
and then using that connection in my HTTP server callback, which then
gets cluster()ed to multiple workers... but that doesn't seem like a
good solution to me -- a bottleneck if nothing else.

I'm really new to this and I have little idea how things work under
the hood with Node.js (or Cluster and MongoDB, for that matter), so
maybe I'm thinking the wrong way about this. Any suggestions?

Jorgen

tjholowaychuk

unread,
Apr 2, 2011, 1:43:47 AM4/2/11
to nodejs
You already would have one connection per worker.. you can't share the
connection, so you have already accomplished that :p

Ricardo Tomasi

unread,
Apr 2, 2011, 11:33:47 PM4/2/11
to nodejs

If you allow me to hijack the thread: say I'm pop()ing from a
collection in this setup, am I likely to have any racing conditions?

Ricardo Tomasi

unread,
Apr 2, 2011, 11:52:42 PM4/2/11
to nodejs
*race :)

I'm sharing a waiting list between processes and need to make sure an
item is retrieved only once. Might have more to do with how mongo
works, I'm not sure...

Jorgen B.

unread,
Apr 4, 2011, 7:29:00 AM4/4/11
to nodejs
Sorry, uh, I'm not sure if I understood. If I cluster() an HTTP server
that accesses MongoDB through a single Mongoose connection in its
callback, what exactly happens?

Marcello Bastéa-Forte

unread,
Apr 4, 2011, 11:11:44 AM4/4/11
to nod...@googlegroups.com, Jorgen B.
I'm guessing each node worker gets its own mongoose connection.

Marcello

--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.


Jonas Huckestein

unread,
Apr 4, 2011, 1:47:05 PM4/4/11
to nodejs
Sounds like you're having only one file server.js that looks something
like this:

-- snip --
cluster(http.createServer( ... do stuff ... )).listen();
-- snip --

Cluster will run server.js once for the master and then once for every
worker, which means that every worker has their own database
connection Alternatively (making things a little clearer), you can
pass cluster the path of a module that exports an http server. This
module will only get required in the workers. Check out the API docs
at http://learnboost.github.com/cluster/docs/api.html

Jorgen B.

unread,
Apr 5, 2011, 3:49:41 AM4/5/11
to nodejs
OK, yes, now I understand, thanks a lot.

Jorgen

On Apr 4, 8:47 pm, Jonas Huckestein <jonas.huckest...@gmail.com>
wrote:
> Sounds like you're having only one file server.js that looks something
> like this:
>
> -- snip --
> cluster(http.createServer( ... do stuff ... )).listen();
> -- snip --
>
> Cluster will run server.js once for the master and then once for every
> worker, which means that every worker has their own database
> connection Alternatively (making things a little clearer), you can
> pass cluster the path of a module that exports an http server. This
> module will only get required in the workers. Check out the API docs
> athttp://learnboost.github.com/cluster/docs/api.html
Reply all
Reply to author
Forward
0 new messages