8

Im curious about horizontal scalling in node.js is possible to load balance acrross multiple virtual servers like rackspace cloud servers? I read about cluster plugin but I think it is only for a single server with a multi core cpu.

Christian
  • 329
  • 1
  • 4
  • 12
  • 2
    nginx? => http://www.shinstudio.com/blog/backend-tech/setting-up-node-js-in-nginx/ – Alfred Jul 05 '11 at 17:30
  • 1
    Easy, write a node load balancer proxy in node. – Raynos Jul 05 '11 at 17:49
  • 1
    but what about instance connections, for example multiple chat rooms how route users to the specific server, what if the users are spread across the servers. – Christian Nov 09 '11 at 17:17

1 Answers1

10

Try roundrobin.js for node-http-proxy:

var httpProxy = require('http-proxy');
//
// A simple round-robin load balancing strategy.
//
// First, list the servers you want to use in your rotation.
//
var addresses = [
  {
    host: 'ws1.0.0.0',
    port: 80
  },
  {
    host: 'ws2.0.0.0',
    port: 80
  }
];

httpProxy.createServer(function (req, res, proxy) {
  //
  // On each request, get the first location from the list...
  //
  var target = addresses.shift();

  //
  // ...then proxy to the server whose 'turn' it is...
  //
  proxy.proxyRequest(req, res, target);

  //
  // ...and then the server you just used becomes the last item in the list.
  //
  addresses.push(target);
});

// Rinse; repeat; enjoy.
psema4
  • 3,007
  • 1
  • 17
  • 22
  • 5
    Does this implementation proxy the connection or does the client connection get handed off to the server that happened to be next? I'm wondering in the context of using websockets and have a persistent connection. If all of the connections are funneled through the load balancer then you still have a bottleneck because the load balancer will get saturated with the connections. – Jeff LaFay May 10 '12 at 12:29