Distribute Express app across servers

54 views
Skip to first unread message

Kevin Ingwersen (Ingwie Phoenix)

unread,
Apr 10, 2015, 1:54:23 PM4/10/15
to nod...@googlegroups.com
Hey.

I would like to ask how I can turn a single-process express app into one that uzilizes multiple processes. Imagine the following structure:

main (pid 1001)
|- express1 (pid 1002)
|- express2 (pid 1003)
|- …

My goal is to improve performance by spanning my http server across multiple processes. I also will add a Varnish server infront, so all that my Express app is doing is handling the actual action, which can be quite heavy on a single process.

Any ideas, recommendations or such? :)

Aria Stewart

unread,
Apr 10, 2015, 2:13:18 PM4/10/15
to nod...@googlegroups.com

Step 1: Make sure your requests share state by communicating out of process, not by sharing state in variables, if requests interact with each other at all.

Step 2: use the cluster module. That's exactly what it's for. It's better designed in iojs and node 0.12 than node 0.10, but the general idea is the same.

Aria

Nathan White

unread,
Apr 10, 2015, 2:26:39 PM4/10/15
to nod...@googlegroups.com

If your using varnish you can setup round robin for your backend.

The native cluster module does exactly this. I recommend checking out pm2 which does a great job of making this easy to manage.
> --
> Job board: http://jobs.nodejs.org/
> New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
> Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> ---
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
> To post to this group, send email to nod...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/2F1E12DD-6655-4E2B-A6DE-BC39992AB7CA%40googlemail.com.
> For more options, visit https://groups.google.com/d/optout.

Kevin Ingwersen (Ingwie Phoenix)

unread,
Apr 10, 2015, 3:20:24 PM4/10/15
to nod...@googlegroups.com
On Fr. Apr. 10 2015 20:12:50 Aria Stewart wrote:
On Friday, April 10, 2015 at 1:54:23 PM UTC-4, Ingwie Phoenix wrote:
Hey.

I would like to ask how I can turn a single-process express app into one that uzilizes multiple processes. Imagine the following structure:

        main (pid 1001)
        |- express1 (pid 1002)
        |- express2 (pid 1003)
        |- …

My goal is to improve performance by spanning my http server across multiple processes. I also will add a Varnish server infront, so all that my Express app is doing is handling the actual action, which can be quite heavy on a single process.

Step 1: Make sure your requests share state by communicating out of process, not by sharing state in variables, if requests interact with each other at all.
I use Redis to establish an RPC/RMI layer. It is used to distribute events to trigger backups and the like.

Step 2: use the cluster module. That's exactly what it's for. It's better designed in iojs and node 0.12 than node 0.10, but the general idea is the same.
Do I listen on the port on the master or child process?

I looked into pm2, but I got a bit confused with it. I only saw CLI usage. What is a basic in-script usage?

Kind regards,
Ingwie

Aria Stewart

unread,
Apr 10, 2015, 3:24:56 PM4/10/15
to nod...@googlegroups.com


On Friday, April 10, 2015 at 3:20:24 PM UTC-4, Ingwie Phoenix wrote:

Step 1: Make sure your requests share state by communicating out of process, not by sharing state in variables, if requests interact with each other at all.
I use Redis to establish an RPC/RMI layer. It is used to distribute events to trigger backups and the like.

 
Good. That works reasonably well!
 
Step 2: use the cluster module. That's exactly what it's for. It's better designed in iojs and node 0.12 than node 0.10, but the general idea is the same.
Do I listen on the port on the master or child process?

In the child. Counterintuitive, but that's how the magic happens.

I looked into pm2, but I got a bit confused with it. I only saw CLI usage. What is a basic in-script usage?


CLI is the normal way -- it wraps your process and starts it, injecting its magic.   Weird, but works pretty well.

Mind the license though.

Kevin Ingwersen (Ingwie Phoenix)

unread,
Apr 10, 2015, 9:38:05 PM4/10/15
to nod...@googlegroups.com
On Fr. Apr. 10 2015 21:24:31 Aria Stewart wrote:
On Friday, April 10, 2015 at 3:20:24 PM UTC-4, Ingwie Phoenix wrote: 
Step 2: use the cluster module. That's exactly what it's for. It's better designed in iojs and node 0.12 than node 0.10, but the general idea is the same.
Do I listen on the port on the master or child process?

In the child. Counterintuitive, but that's how the magic happens.
Well if it works, it works. :)

I looked into pm2, but I got a bit confused with it. I only saw CLI usage. What is a basic in-script usage?


CLI is the normal way -- it wraps your process and starts it, injecting its magic.   Weird, but works pretty well.
Mm…I prefer to keep things self-contained. Workerman is the same for PHP, really. I have a main script and then call that instead of a pre-installed program. Gives me more control of the used versions and alike. I also looked at forever - but neither pm2 or forever seem to allow to simply be require()’d and then set up… well, looks like I’ll have to make my own cluster initializer then.

Mind the license though.
What exactly do you mean? I am not a native english, so reading licenses usually gives me grande headaches...

Aria Stewart

unread,
Apr 10, 2015, 9:44:49 PM4/10/15
to nod...@googlegroups.com


On Friday, April 10, 2015 at 9:38:05 PM UTC-4, Ingwie Phoenix wrote:

I looked into pm2, but I got a bit confused with it. I only saw CLI usage. What is a basic in-script usage?


CLI is the normal way -- it wraps your process and starts it, injecting its magic.   Weird, but works pretty well.
Mm…I prefer to keep things self-contained. Workerman is the same for PHP, really. I have a main script and then call that instead of a pre-installed program. Gives me more control of the used versions and alike. I also looked at forever - but neither pm2 or forever seem to allow to simply be require()’d and then set up… well, looks like I’ll have to make my own cluster initializer then.

I hear you -- though you may consider adding pm2 or forever as a dependency in package.json, and adding an npm start script that starts it. Still self contained by some definition, at least self-specifying its dependencies.
 
Mind the license though.
What exactly do you mean? I am not a native english, so reading licenses usually gives me grande headaches...

It's AGPL -- the most 'you must share' of the GPL style licenses. Older versions are under less restrictive licenses.

Aria

Alexander Behrens

unread,
Apr 11, 2015, 7:40:17 AM4/11/15
to nod...@googlegroups.com
I agree with Nathan White, let Varnish, Nginx or whatever reverse proxy you plugged in front of your node apps handle the load balancing. Do not do it on your own.

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages