Page 1 of 1

load balancing

Posted: Sat Jan 22, 2005 9:29 pm
by froth
I have a very power-hungry dynamic sig in a very popular forum. My host gives me 4 gigs transfer a month and I've used 400 megs in a couple of hours. So I want to register four or five paralell hosts that all have the same files... then have a single, fast host that handles load balancing. Is there an easy way to do this in PHP, perhaps code that would cycle through hosts as they're used? Thanks in advance.

Posted: Sat Jan 22, 2005 9:43 pm
by feyd
maybe you should fix your sig so it's not so power-hungry.

Switching hosts to a larger bandwidth may be cheaper and better. Load balancing is best done in a closed environment, where the servers don't exist on the internet, but server through a gateway that does the load-balancing.

Although you can, technically load balance across multiple domains.. it's not practical most of the time.

There are many many hosts that offer 100GB or more of bandwidth for very low sums. But first, I'd suggest you fix your sig. You may need to switch to a slightly different model of creation where you only update the actual image every 10 minutes, in-between the script just tosses the last one it created via storing it on the server in some fashion.

re

Posted: Sat Jan 22, 2005 10:05 pm
by froth
The problem is that the sig has to query several quickly-updating pages across the internet... and the content in the sig (which is a gd-generated image) usually changes every second or two because of the updates in the source. So basically just what I'm looking for is code that will redirect to server 1 on the first request, server 2 on the second request, server 3 on the third request, server 1 on the foruth request, server 2 on the fifth request, and so on... merely to keep bandwidth use down. Purchasing more bandwith is not an option (i get multiple accounts free) so I need a software solution.

Posted: Sat Jan 22, 2005 10:15 pm
by feyd
why is it so important the image is absolutely up to date?

You can locally save the last created file, and only run out and get the data every minute or so.. at least then, you'll use a LOT less bandwidth because you're constantly going out of your server and fetching remote information.. all of that external communication costs time and bandwidth.

As I said, consider getting the script under control.

Posted: Sat Jan 22, 2005 10:33 pm
by froth
because the whole point of the sig is to provide to-the-second info. but it's really no problem to register multiple accounts; please just tell me how to do load balancing the way i described.

Posted: Sat Jan 22, 2005 10:49 pm
by feyd
store the last "server" tossed at in a database, or some other record.. toss to the next available one.. I'd suggest storing the list of urls in a database, along with the last one tossed to.. it'll make managment of them easier..

You should then be able to return a header() HTTP redirect to that server.

Posted: Sat Jan 22, 2005 11:04 pm
by froth
so i make a single-cell database that contains a short integer, say, 1 through 5. and the script reads the cell, goes to that number server, and increments/resets the number. sounds easy enough.

trouble

Posted: Sat Jan 22, 2005 11:46 pm
by froth
I'm having some trouble writing the code... i'm not very good with mysql. I've set up a database named frother_load and a one-field table called lastbounce. the field is called num and is a tiny int. this is what i have so far:

Code: Select all

$conn = mysql_connect("thehost.com","frother_load","xxxxxx");
mysql_select_db("frother_load",$conn);
mysql_query("update lastbounce set num = num+1",$conn);
I really don't know how to read the contents of num (a single number: 1 2 3 4 or 5) to a php variable.

Posted: Sun Jan 23, 2005 12:28 am
by feyd

Code: Select all

SELECT num FROM lastbounce LIMIT 1
and

Code: Select all

UPDATE lastbounce SET num = ((num + 1) % 5)

reading

Posted: Sun Jan 23, 2005 4:10 pm
by froth
OK but that only increments it. How do I check the current status of the number? When I use this:

Code: Select all

$conn = mysql_connect("thehost.com","frother_load","xxxxxx");
mysql_select_db("frother_load",$conn);
$curnum = mysql_query("SELECT num FROM lastbounce LIMIT 1",$conn);
print $curnum;
mysql_query("UPDATE lastbounce SET num = ((num + 1) % 5)",$conn);
it prints "Resource id #2". How do I see what the number is at the time of script running?

Posted: Sun Jan 23, 2005 4:12 pm
by feyd
mysql_fetch_*

;)

Code: Select all

$result = mysql_query(...);
$record = mysql_fetch_assoc($result);

print_r($record);

...

Posted: Sun Jan 23, 2005 4:17 pm
by froth
I'm sorry, I really don't know which fetch to use if I just want a single number. Can you give me a line of code that would do it? Thanks.

Posted: Sun Jan 23, 2005 4:23 pm
by feyd
look at my example.

thanks

Posted: Sun Jan 23, 2005 4:29 pm
by froth
thank you, it is now working perfectly :)