title: Is this a good server's load balancing system?
body: <p>I'm new to this concept and I'm thinking on how to horizontally scale my <a href="https://github.com/xeryan/xepler" rel="nofollow">Xepler</a> Node.js framework.</p>
  
  <p>So, the main app on the master server will proxy the request to the first cluster in a queue (maybe retrieved using a shared-memory with redis). Each X requests (depending on the server capabilities, X is decided by me, maybe using a map), the cluster will be moved to the last place in this queue. In this way, all the clusters will receive only a reduced number of requests.</p>
  
  <p>Another app, on another server, each X seconds will make a request to all the clusters in order to check if someone is failed, removing it from the queue (this queue will be on redis?)</p>
  
  <p>All the clusters normally will run an instance of my web framework.</p>
  
  <p>Is for you this a good system of load balancing, or have I completely misunderstanding how works? Thank you guys</p>
  
  <p>edit: that's what I mean (only an example):    </p>
  
  <pre><code>var http = require('http'),
       https = require('https'),
      httpProxy = require('http-proxy'),
      proxy = httpProxy.createProxyServer({}),
  
       clusters = [
          {
              id: 1,
              host: "localhost",
              port: 8080,
              dead : false,
              deadTime : undefined
          },      
          {
              id: 2,
              host: "localhost",
              port: 8081,
              dead : false,
              deadTime : undefined
          }
       ];
  
  http.createServer(function(req, res) {
      var target = getAvailableCluster();
  
      if (target != -1) {
          proxy.web(req, res, { target: 'http://' + target.host + ':' + target.port });
  
          res.setTimeout(1e3 * 20, function() {
              target.dead = true;
              target.deadTime = new Date().getTime();
              console.log("Cluster " + target.id + " is dead");
          });
      }   
  }).listen(80, function() {
      console.log('Proxy listening on port 80..');
  });
  
  proxy.on('error', function (error, req, res) {
      var json;
      console.log('proxy error', error);
  
      if (!res.headersSent)
          res.writeHead(500, { 'content-type': 'application/json' });
  
      json = { error: 'proxy_error', reason: error.message };
      res.end(JSON.stringify(json));
  });
  
  setInterval(function() {
      var cluster,
          currentTime = new Date().getTime();
  
      for (var i=0; i&lt;clusters.length; i++) {
          cluster = clusters[i];
  
          if (cluster.dead &amp;&amp; (currentTime - cluster.deadTime) &gt; 1000) {
              cluster.dead = false;
              console.log("Cluster " + cluster.id + " is now alive");
          }
      }   
  }, 5000);
  
  function getAvailableCluster() {
      var cluster;
  
      for (var i=0; i&lt;clusters.length; i++) {
          cluster = clusters.shift();
          clusters.push(cluster);
  
          if (!cluster.dead)      
              return cluster;
      }
  
      return -1;
  }
  </code></pre>