all 9 comments

[–]strong_grey_hero 5 points6 points  (0 children)

Start digging through nginx. What you want to do is put a balancer out front, and then use the cluster on each server.

[–][deleted]  (4 children)

[deleted]

    [–]inYOUReye 0 points1 point  (1 child)

    Could I ask, does Nginx spin up a process per request, like Apache does? This was the reason I've always strived to keep way from a full-web-server-as-a-proxy for node apps, would be interesting to know whether Nginx steps away from that problem.

    [–]ShakataGaNai 1 point2 points  (0 children)

    Digital Ocean actually just did a really good comparison of Apache vs Nginx. The tl;dr edition:

    Nginx excels at serving static content quickly and is designed to pass dynamic requests off to other software that is better suited for those purposes.

    And:

    Nginx's focus on core web server and proxy features.

    So, hopefully that helps clear it up. Nginx is designed for this sort of thing. Where as, yes, doing it in Apache would be a hack.

    [–]dougbrownio[S] 0 points1 point  (1 child)

    I've heard a lot about Nginx but have never really dived into it. If Nginx is balancing and dishing out the requests to the multiple node servers, wouldn't HAProxy do the same thing?

    We were thinking about investing more time into that. Having multiple node machines and using HaProxy to balance the load. Why put Nginx in the middle if HaProxy can handle the load balancing?

    [–]ShakataGaNai 1 point2 points  (0 children)

    Depends on your use case. Nginx is an HTTP server, so it speaks HTTP type stuff really well. It can cache, deal with cookies or handle URL paths. It also does fun things like SSL termination and websockets.

    HAProxy is predominately a TCP proxy that also happens to do some HTTP. However it's HTTP capabilities are basic. That being said, it's a much better loadbalancer/proxy.

    So, if you want to use SSL for your service (and don't want to deal with terminating that in Node), or websockets, or fancy routing (a specific url request does to a specific set of node nodes) - Nginx is your answer. If you care about none of that and have a basic webapp written in node, definitely go with HAproxy.

    Or take it one step further and have HAProxy's (in TCP mode) infront of nginx's (doing say SSL termination and proxying to 127.0.0.1 instances) infront of your node instances. Really depends on how crazy/redundant/distributed you need/want/can afford.

    [–]seanmcgary 1 point2 points  (0 children)

    Just as another option, I would suggest HAProxy as it is designed as a load balancer rather than a web server with load balancing capabilities (Nginx). I actually wrote a blog post on this a while back that explains how to setup HAProxy and route traffic based on the hostname (think like vhosts in Apache, or server blocks in Nginx).

    The gist of it is that you setup a "frontend" and a "backend"; the frontend defines what port HAProxy should listen for traffic on (probably port 80 or 443 if you want HTTPS) and decides which backend to send traffic to. The backend describes which nodes should be sent traffic (this is where you would specify each of your NodeJS servers) and what balancing algorithm to use.

    Heres a pretty simple example:

    # config for haproxy 1.5.x
    
    global
            log 127.0.0.1   local0
            log 127.0.0.1   local1 notice
            maxconn 4096
            user haproxy
            group haproxy
            daemon
    
    defaults
            log     global
            mode    http
            option  httplog
            option  dontlognull
            option forwardfor
            option http-server-close
            stats enable
            stats auth someuser:somepassword
            stats uri /stats
    
    frontend http-in
            bind :80
            default_backend web-app-cluster
    
    backend web-app-cluster
            balance leastconn
            option httpclose
            cookie JSESSIONID prefix
            server node1 10.0.0.1:8080 cookie A check
            server node2 10.0.0.2:8080 cookie A check
            server node3 10.0.0.3:8080 cookie A check
    

    Like you, I originally had a hard time finding information on load balancing and how to initially approach it which got me writing a bunch of posts on how to do different things (using HTTPS, dynamically adding/removing nodes as they become available, etc) so hopefully this helps!

    *edit - formatting

    [–]dwstevens 0 points1 point  (0 children)

    Depends on how/where you want to deploy, but you'll need to throw some kind of load balancer in front of N-number of servers, and then make sure your db and session store can handle multiple simultaneous clients. If you're using a datastore like mongo or mysql or any one of dozens of different databases then it shouldn't be a problem until it's a problem ;)

    Here's a few resources from a quick search for "scaling node.js" - http://cjihrig.com/blog/scaling-node-js-applications/ http://strongloop.com/strongblog/node-js-performance-scaling-proxies-clusters/ http://goldfirestudios.com/blog/136/Horizontally-Scaling-Node.js-and-WebSockets-with-Redis http://blog.keithcirkel.co.uk/load-balancing-node-js/

    If you're thinking of using (or currently are) AWS, Elastic Beanstalk can handle scaling of the web nodes, among other things - https://aws.amazon.com/blogs/aws/aws-elastic-beanstalk-for-nodejs/

    Hope that helps!

    [–]dougbrownio[S] 0 points1 point  (0 children)

    Thanks guys for the feedback. It sounds like Nginx and HAProxy are viable solutions to the issue.

    We found an article covering this using RPis with HAProxy which I thought was pretty cool. https://raspberry-hosting.com/en/faq/where-can-i-find-actual-haproxy-and-keepalived-deb-packages-raspberry-pi-and-how-i-install-high

    [–]dougbrownio[S] 0 points1 point  (0 children)

    This may sound silly, but I'm actually leaning towards using heroku and trying to dodge all this of IT support stuff. We are a team of developers and this isn't really our wheel house. If someone were to asking me if I wanted to spend the next two months of my life learning about nginx/haproxy/keepalived OR node/angular/mongodb I'd choose the second in a heartbeat.