13

I'm trying to code a load balancing with node.js and http-proxy. I want a loadBalancer that shares incoming request treatment between 2 servers.

var http = require('http'),
httpProxy = require('http-proxy');

var servers =  [{host :'127.0.0.1', port :3000}, {host : 'remote_adr',port :3000}];

httpProxy.createServer(function (req, res, proxy) {
    var target = servers.shift();
    proxy.proxyRequest(req, res, target);
    servers.push(target);
}).listen(8000);

I thought that doing this would have made a loadBalancer which sends requests alternately to serv1 and to serv2.

However, when I try it out, it seems to request the 2 servers in no particular order. In addition, most of the requests are sent to my localhost node server ( 127.0.0.1:3000 )

Is somebody able to explain that behavior?

ivanjermakov
  • 1,131
  • 13
  • 24
Florent
  • 241
  • 2
  • 7

1 Answers1

7

That should be doing a round robin as you requested, however it's a very naive round robin. What you may be seeing is that your favicon.ico requests are putting a higher than normal share of requests on a single server. For instance.

Server 1: Actual Requests
Server 2: favicon.ico
Server 1: Another Request
Server 2: favicon.ico
Server 1: Final Request
Server 2: favicon.ico

So depending on how you're logging, it will appear that server 1 is getting all of the requests, when technically each server is getting the same number of requests. Also keep in mind that your assets will be included in the round robin as well if you're serving them from node. So each image or file request will also be handed off to a different node instance.

Timothy Strimple
  • 22,920
  • 6
  • 69
  • 76
  • awesome explanation. favicon.ico is not hit be node.js? if yes, how come the request handled is not shown? – Amareswar Feb 26 '13 at 01:56
  • 1
    @Amareswar Not sure what you mean by 'favicon.ico is not hit by node.js'. Node.js can certainly handle favicon requests, but if you're just logging explicit routes in express for instance, you may not see a favicon.ico request show up in your logs. – Timothy Strimple Feb 26 '13 at 02:25
  • got it. That was my doubt. – Amareswar Feb 26 '13 at 22:22