Php help!

Discussion in 'OT Technology' started by o2, Sep 29, 2006.

  1. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    12
    Location:
    Toronto
    One of my sites requires multiple media servers, since 1 can no longer handle all the load, so I setup a very simple traffic splitter, which randomly picks 1 of 3 media server URLs.

    PHP:
    <? $item rand(13); if ($item == 1) { echo $ftp_url; } elseif ($item == 2) { echo $ftp_url2; } elseif ($item == 3) { echo $ftp_url3; } ?>
    Ive been wanting to make this a little more flexible, and so I can specify priority, of how much traffic I want to get to each server. For example, first server handles 1/2 the load, and the other 2 handle the other half.

    I have no clue of where to begin.....

    Any ideas?
     
  2. RaginBajin

    RaginBajin Have you punched a donkey today?

    Joined:
    Dec 24, 2001
    Messages:
    8,740
    Likes Received:
    0
    Location:
    NoVA
    This is something I found on google. I'm sure you already looked into it.
    Take a look at this.. This seems to fit what you need.

    http://www.harrysufehmi.com/phpwiki/index.php/PhpLoadBalancer


    My other thought would be to write a php class that would look at a table and request and determine where it needs to go and spit out the appropriate url for you to use. The simple version would be to just have a table where you have an id, url, status, and counter. Everytime you use that url, you update that row and increase the counter by 1. You select which url you are going to be using by finding the min(counter) and giving you the url for that value.

    I'm just thinking out loud here. So...

    If you want it to do weight as well, then you have a another column called weight. You divide the counter by weight and that will give you the id that you are going to use because you are still searching for the min value.

    Example of the table
    id, url, status, weight, counter
    1,joe.com, 1,2,50
    2,bob.com,1,1,50
    3,o2.com,1,1,50

    you will then:

    select id, url from stat where counter/weight = (select min(counter/weight) from stat where status = 1);

    And that will give you out the lowest value to use. Its probably not the most efficent but it is definitely a start.
     
  3. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    12
    Location:
    Toronto
    Thanks, but updating a mysql database everytime is just a waste of cpu power, and DB space. Im just looking for a simple fix where I can have the weights in my settings.php file, instead of querying the DB every single time.
     
  4. RaginBajin

    RaginBajin Have you punched a donkey today?

    Joined:
    Dec 24, 2001
    Messages:
    8,740
    Likes Received:
    0
    Location:
    NoVA
    hmm.

    You can do everything but the counter in a file. The counter is the peice that is going to let you move from one host to another. I'm not sure if you will be able to do something with weight and move back and forth based on usage the way you want by using a static file.

    It was just an idea, I had. I didn't know that a DB was not something you want to use. Even big load balances like Big IP F5's have a little DB when it comes to traffic, weight, and load balancing.
     

Share This Page