Page 1 of 1

Simple One Time PHP Load Balancing Using Random Redirects???

Posted: Wed Oct 28, 2009 10:38 am
by icon512
Hello all!

I got this through Google searching for PHP load balancing: http://www.daniweb.com/forums/thread171839.html

I am trying to do something similar, but what I'm trying to do is a one time, one day, quick and cheap load balancing solution using PHP.

The idea is to use a simple php script to randomly redirect users to several other servers with one static page. I am just wondering if this would be sufficient if I were to expect an influx of users for only one day (may reach up to 50,000 users within a one hour period). Below is what I have come up with using only limited php knowledge. The server running this script will be a basic dedicated virtual server by MediaTemple which should suffice as a temporary load balancer, and will be redirected to multiple GridServer accounts from MediaTemple. Do you see any problems with this?

Code: Select all

 
<?php 
$servers = array("www.foo1.com", 
              "www.foo2.com", 
              "www.foo3.com"); 
$server = $servers[array_rand($servers)]; 
header("Location: http://$server"); 
?>
 
Thank you for taking your time to look at this! I really appreciate it!

Re: Simple One Time PHP Load Balancing Using Random Redirects???

Posted: Sat Oct 31, 2009 9:46 am
by kaisellgren
Yeah you can do that, but it does not help a lot. It should be non-PHP solution to be efficient. I think there are some Apache modules that could do this kind of thing much faster, because at the moment you launch PHP to parse and interpret stuff you are already slowing things down.

You didn't tell us what you are actually balancing. Bandwidth? CPU usage? Bandwidth can be dragged down by the use of CDN's. For CPU usage, PHP as the load balancer is not really the best thing to do, besides, there are plenty of low-level load balanced network solutions in web hosting world.

Re: Simple One Time PHP Load Balancing Using Random Redirects???

Posted: Sat Oct 31, 2009 11:44 am
by icon512
kaisellgren wrote:Yeah you can do that, but it does not help a lot. It should be non-PHP solution to be efficient. I think there are some Apache modules that could do this kind of thing much faster, because at the moment you launch PHP to parse and interpret stuff you are already slowing things down.

You didn't tell us what you are actually balancing. Bandwidth? CPU usage? Bandwidth can be dragged down by the use of CDN's. For CPU usage, PHP as the load balancer is not really the best thing to do, besides, there are plenty of low-level load balanced network solutions in web hosting world.
Would you be able to tell me which Apache modules I should use?

Well, I think I just want to try and balance anything so nothing bad happens during this spike. And I am on a very tight budget so I am trying to do the most efficient and cost effective way.

Re: Simple One Time PHP Load Balancing Using Random Redirects???

Posted: Sat Oct 31, 2009 1:54 pm
by markusn00b
I had a glance at this, although it may be a little too much. If you'd like something a little simple, have a look at the Rewrite Map directive for mod_rewrite - more specifically, the section on 'randomized plain text'.

Mark.

Re: Simple One Time PHP Load Balancing Using Random Redirects???

Posted: Sat Oct 31, 2009 5:45 pm
by josh
Nice find Mark. You could also see if http://www.squid-cache.org/ could solve your load problems
"[The Squid systems] are currently running at a hit-rate of approximately 75%, effectively quadrupling the capacity of the Apache servers behind them. This is particularly noticeable when a large surge of traffic arrives directed to a particular page via a web link from another site, as the caching efficiency for that page will be nearly 100%. " - Wikimedia Deployment Information.