load balancing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
froth
Forum Commoner
Posts: 31
Joined: Sat Jan 22, 2005 9:26 pm

load balancing

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
froth
Forum Commoner
Posts: 31
Joined: Sat Jan 22, 2005 9:26 pm

re

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
froth
Forum Commoner
Posts: 31
Joined: Sat Jan 22, 2005 9:26 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
froth
Forum Commoner
Posts: 31
Joined: Sat Jan 22, 2005 9:26 pm

Post 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.
froth
Forum Commoner
Posts: 31
Joined: Sat Jan 22, 2005 9:26 pm

trouble

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

SELECT num FROM lastbounce LIMIT 1
and

Code: Select all

UPDATE lastbounce SET num = ((num + 1) % 5)
froth
Forum Commoner
Posts: 31
Joined: Sat Jan 22, 2005 9:26 pm

reading

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysql_fetch_*

;)

Code: Select all

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

print_r($record);
froth
Forum Commoner
Posts: 31
Joined: Sat Jan 22, 2005 9:26 pm

...

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

look at my example.
froth
Forum Commoner
Posts: 31
Joined: Sat Jan 22, 2005 9:26 pm

thanks

Post by froth »

thank you, it is now working perfectly :)
Post Reply