Hey, Would you like to work at Home ?? Just click here No need to pay, just register free and activate your account and get data Entry Work at your Home.

Tuesday, February 5, 2008

Load Balancing


Suppose we want to load balance the traffic to www.foo.com over www[0-5].foo.com (a total of 6 servers). How can this be done?


There are a lot of possible solutions for this problem. We will discuss first a commonly known DNS-based variant and then the special one with mod_rewrite:

  1. DNS Round-Robin

    The simplest method for load-balancing is to use the DNS round-robin feature of BIND. Here you just configure www[0-9].foo.com as usual in your DNS with A(address) records, e.g.

    www0   IN  A  www1   IN  A  www2   IN  A  www3   IN  A  www4   IN  A  www5   IN  A  

    Then you additionally add the following entry:

    www    IN  CNAME   www0.foo.com.         IN  CNAME   www1.foo.com.         IN  CNAME   www2.foo.com.         IN  CNAME   www3.foo.com.         IN  CNAME   www4.foo.com.         IN  CNAME   www5.foo.com.         IN  CNAME   www6.foo.com.  

    Notice that this seems wrong, but is actually an intended feature of BIND and can be used in this way. However, now when www.foo.com gets resolved, BIND gives out www0-www6 - but in a slightly permutated/rotated order every time. This way the clients are spread over the various servers. But notice that this not a perfect load balancing scheme, because DNS resolve information gets cached by the other nameservers on the net, so once a client has resolved www.foo.com to a particular wwwN.foo.com, all subsequent requests also go to this particular name wwwN.foo.com. But the final result is ok, because the total sum of the requests are really spread over the various webservers.

  2. DNS Load-Balancing

    A sophisticated DNS-based method for load-balancing is to use the program lbnamed which can be found at http://www.stanford.edu/~schemers/docs/lbnamed/lbnamed.html. It is a Perl 5 program in conjunction with auxilliary tools which provides a real load-balancing for DNS.

  3. Proxy Throughput Round-Robin

    In this variant we use mod_rewrite and its proxy throughput feature. First we dedicate www0.foo.com to be actually www.foo.com by using a single

    www    IN  CNAME   www0.foo.com.  

    entry in the DNS. Then we convert www0.foo.com to a proxy-only server, i.e. we configure this machine so all arriving URLs are just pushed through the internal proxy to one of the 5 other servers (www1-www5). To accomplish this we first establish a ruleset which contacts a load balancing script lb.pl for all URLs.

    RewriteEngine on  RewriteMap    lb      prg:/path/to/lb.pl  RewriteRule   ^/(.+)$ ${lb:$1}           [P,L]  

    Then we write lb.pl:

    #!/path/to/perl  ##  ##  lb.pl -- load balancing script  ##    $| = 1;    $name   = "www";     # the hostname base  $first  = 1;         # the first server (not 0 here, because 0 is myself)  $last   = 5;         # the last server in the round-robin  $domain = "foo.dom"; # the domainname    $cnt = 0;  while (<STDIN>) {      $cnt = (($cnt+1) % ($last+1-$first));      $server = sprintf("%s%d.%s", $name, $cnt+$first, $domain);      print "http://$server/$_";  }    ##EOF##  

    A last notice: Why is this useful? Seems like www0.foo.com still is overloaded? The answer is yes, it is overloaded, but with plain proxy throughput requests, only! All SSI, CGI, ePerl, etc. processing is completely done on the other machines. This is the essential point.

  4. Hardware/TCP Round-Robin

    There is a hardware solution available, too. Cisco has a beast called LocalDirector which does a load balancing at the TCP/IP level. Actually this is some sort of a circuit level gateway in front of a webcluster. If you have enough money and really need a solution with high performance, use this one.

No comments:

Your Ad Here