Loadbalancing using PHP? Is this effective?












-1















Server 1:



<?php 
$servers = array("server2",
"server3",
"server4");
$server = $servers[array_rand($servers)];
header("Location: http://$server");
?>


Server 2:



Cloned Website



Server 3:



Cloned Website



Server 4:



Cloned Website



Server 5:



Database server, serving all servers



The DNS will be set to server 1 and then the script will redirect them to a random server in order to lower the load.



I could load up multiple server 1's using roundrobin DNS for example.



Furthermore I could improve the script on server 1 to check the health status every so often in order to make sure it doesn't redirect to a server that has gone down.



This could surely balance an unlimited amount of servers, I could just keep spinning up more loadbalancing servers via DNS if the load is super high? I don't see a problem with this, but clearly there must be as nobody uses this method



What I wanted to know is that would this be a good approach to loadbalancing, if not how exactly should it be done? Is there any problems with this approach?










share|improve this question

























  • Why not use nginx as a load balancer?

    – Harvey Fletcher
    Nov 15 '18 at 9:35











  • I know, but I just want to know if there is hypothetically any issue with doing this instead of using NGINX, as this is a much simpler method to implement.

    – Destroyer
    Nov 15 '18 at 9:36






  • 2





    Using a scripting language as a load balancer is going to be a lot slower, and is probably one of the most serious reinventing-the-wheel cases I can think of. But if you're doing it as a learning experiment, then good luck - there's no fundamental reason it can't be done.

    – iainn
    Nov 15 '18 at 9:37













  • @iainn What would be the effect of this if I am already using it in a fairly large website that get's tens of thousands of hits an hour?

    – Destroyer
    Nov 15 '18 at 9:40








  • 1





    @Destroyer Well if you're using an actual HTTP redirect rather than proxying the requests then the obvious drawback is that you won't be able to use anything other than GET requests. If you're using this for anything more complicated than serving basic static content then that might be a show-stopper straight away. I really, really wouldn't recommend using anything like this in production, let alone for a large website. Apache, nginx, haproxy, even DNS round-robin are going to be far more reliable.

    – iainn
    Nov 15 '18 at 9:41


















-1















Server 1:



<?php 
$servers = array("server2",
"server3",
"server4");
$server = $servers[array_rand($servers)];
header("Location: http://$server");
?>


Server 2:



Cloned Website



Server 3:



Cloned Website



Server 4:



Cloned Website



Server 5:



Database server, serving all servers



The DNS will be set to server 1 and then the script will redirect them to a random server in order to lower the load.



I could load up multiple server 1's using roundrobin DNS for example.



Furthermore I could improve the script on server 1 to check the health status every so often in order to make sure it doesn't redirect to a server that has gone down.



This could surely balance an unlimited amount of servers, I could just keep spinning up more loadbalancing servers via DNS if the load is super high? I don't see a problem with this, but clearly there must be as nobody uses this method



What I wanted to know is that would this be a good approach to loadbalancing, if not how exactly should it be done? Is there any problems with this approach?










share|improve this question

























  • Why not use nginx as a load balancer?

    – Harvey Fletcher
    Nov 15 '18 at 9:35











  • I know, but I just want to know if there is hypothetically any issue with doing this instead of using NGINX, as this is a much simpler method to implement.

    – Destroyer
    Nov 15 '18 at 9:36






  • 2





    Using a scripting language as a load balancer is going to be a lot slower, and is probably one of the most serious reinventing-the-wheel cases I can think of. But if you're doing it as a learning experiment, then good luck - there's no fundamental reason it can't be done.

    – iainn
    Nov 15 '18 at 9:37













  • @iainn What would be the effect of this if I am already using it in a fairly large website that get's tens of thousands of hits an hour?

    – Destroyer
    Nov 15 '18 at 9:40








  • 1





    @Destroyer Well if you're using an actual HTTP redirect rather than proxying the requests then the obvious drawback is that you won't be able to use anything other than GET requests. If you're using this for anything more complicated than serving basic static content then that might be a show-stopper straight away. I really, really wouldn't recommend using anything like this in production, let alone for a large website. Apache, nginx, haproxy, even DNS round-robin are going to be far more reliable.

    – iainn
    Nov 15 '18 at 9:41
















-1












-1








-1








Server 1:



<?php 
$servers = array("server2",
"server3",
"server4");
$server = $servers[array_rand($servers)];
header("Location: http://$server");
?>


Server 2:



Cloned Website



Server 3:



Cloned Website



Server 4:



Cloned Website



Server 5:



Database server, serving all servers



The DNS will be set to server 1 and then the script will redirect them to a random server in order to lower the load.



I could load up multiple server 1's using roundrobin DNS for example.



Furthermore I could improve the script on server 1 to check the health status every so often in order to make sure it doesn't redirect to a server that has gone down.



This could surely balance an unlimited amount of servers, I could just keep spinning up more loadbalancing servers via DNS if the load is super high? I don't see a problem with this, but clearly there must be as nobody uses this method



What I wanted to know is that would this be a good approach to loadbalancing, if not how exactly should it be done? Is there any problems with this approach?










share|improve this question
















Server 1:



<?php 
$servers = array("server2",
"server3",
"server4");
$server = $servers[array_rand($servers)];
header("Location: http://$server");
?>


Server 2:



Cloned Website



Server 3:



Cloned Website



Server 4:



Cloned Website



Server 5:



Database server, serving all servers



The DNS will be set to server 1 and then the script will redirect them to a random server in order to lower the load.



I could load up multiple server 1's using roundrobin DNS for example.



Furthermore I could improve the script on server 1 to check the health status every so often in order to make sure it doesn't redirect to a server that has gone down.



This could surely balance an unlimited amount of servers, I could just keep spinning up more loadbalancing servers via DNS if the load is super high? I don't see a problem with this, but clearly there must be as nobody uses this method



What I wanted to know is that would this be a good approach to loadbalancing, if not how exactly should it be done? Is there any problems with this approach?







php dns load-balancing reverse-proxy haproxy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 9:36







Destroyer

















asked Nov 15 '18 at 9:32









DestroyerDestroyer

42




42













  • Why not use nginx as a load balancer?

    – Harvey Fletcher
    Nov 15 '18 at 9:35











  • I know, but I just want to know if there is hypothetically any issue with doing this instead of using NGINX, as this is a much simpler method to implement.

    – Destroyer
    Nov 15 '18 at 9:36






  • 2





    Using a scripting language as a load balancer is going to be a lot slower, and is probably one of the most serious reinventing-the-wheel cases I can think of. But if you're doing it as a learning experiment, then good luck - there's no fundamental reason it can't be done.

    – iainn
    Nov 15 '18 at 9:37













  • @iainn What would be the effect of this if I am already using it in a fairly large website that get's tens of thousands of hits an hour?

    – Destroyer
    Nov 15 '18 at 9:40








  • 1





    @Destroyer Well if you're using an actual HTTP redirect rather than proxying the requests then the obvious drawback is that you won't be able to use anything other than GET requests. If you're using this for anything more complicated than serving basic static content then that might be a show-stopper straight away. I really, really wouldn't recommend using anything like this in production, let alone for a large website. Apache, nginx, haproxy, even DNS round-robin are going to be far more reliable.

    – iainn
    Nov 15 '18 at 9:41





















  • Why not use nginx as a load balancer?

    – Harvey Fletcher
    Nov 15 '18 at 9:35











  • I know, but I just want to know if there is hypothetically any issue with doing this instead of using NGINX, as this is a much simpler method to implement.

    – Destroyer
    Nov 15 '18 at 9:36






  • 2





    Using a scripting language as a load balancer is going to be a lot slower, and is probably one of the most serious reinventing-the-wheel cases I can think of. But if you're doing it as a learning experiment, then good luck - there's no fundamental reason it can't be done.

    – iainn
    Nov 15 '18 at 9:37













  • @iainn What would be the effect of this if I am already using it in a fairly large website that get's tens of thousands of hits an hour?

    – Destroyer
    Nov 15 '18 at 9:40








  • 1





    @Destroyer Well if you're using an actual HTTP redirect rather than proxying the requests then the obvious drawback is that you won't be able to use anything other than GET requests. If you're using this for anything more complicated than serving basic static content then that might be a show-stopper straight away. I really, really wouldn't recommend using anything like this in production, let alone for a large website. Apache, nginx, haproxy, even DNS round-robin are going to be far more reliable.

    – iainn
    Nov 15 '18 at 9:41



















Why not use nginx as a load balancer?

– Harvey Fletcher
Nov 15 '18 at 9:35





Why not use nginx as a load balancer?

– Harvey Fletcher
Nov 15 '18 at 9:35













I know, but I just want to know if there is hypothetically any issue with doing this instead of using NGINX, as this is a much simpler method to implement.

– Destroyer
Nov 15 '18 at 9:36





I know, but I just want to know if there is hypothetically any issue with doing this instead of using NGINX, as this is a much simpler method to implement.

– Destroyer
Nov 15 '18 at 9:36




2




2





Using a scripting language as a load balancer is going to be a lot slower, and is probably one of the most serious reinventing-the-wheel cases I can think of. But if you're doing it as a learning experiment, then good luck - there's no fundamental reason it can't be done.

– iainn
Nov 15 '18 at 9:37







Using a scripting language as a load balancer is going to be a lot slower, and is probably one of the most serious reinventing-the-wheel cases I can think of. But if you're doing it as a learning experiment, then good luck - there's no fundamental reason it can't be done.

– iainn
Nov 15 '18 at 9:37















@iainn What would be the effect of this if I am already using it in a fairly large website that get's tens of thousands of hits an hour?

– Destroyer
Nov 15 '18 at 9:40







@iainn What would be the effect of this if I am already using it in a fairly large website that get's tens of thousands of hits an hour?

– Destroyer
Nov 15 '18 at 9:40






1




1





@Destroyer Well if you're using an actual HTTP redirect rather than proxying the requests then the obvious drawback is that you won't be able to use anything other than GET requests. If you're using this for anything more complicated than serving basic static content then that might be a show-stopper straight away. I really, really wouldn't recommend using anything like this in production, let alone for a large website. Apache, nginx, haproxy, even DNS round-robin are going to be far more reliable.

– iainn
Nov 15 '18 at 9:41







@Destroyer Well if you're using an actual HTTP redirect rather than proxying the requests then the obvious drawback is that you won't be able to use anything other than GET requests. If you're using this for anything more complicated than serving basic static content then that might be a show-stopper straight away. I really, really wouldn't recommend using anything like this in production, let alone for a large website. Apache, nginx, haproxy, even DNS round-robin are going to be far more reliable.

– iainn
Nov 15 '18 at 9:41














1 Answer
1






active

oldest

votes


















0














At the first place most of the server load rely on Sql and heavy query,
So instead of making multiple servers to handle request. You can do this




  1. Make Sql replica (Slave server) use it for reading and user master for writing.

  2. Use Memcache or Redis to prevent getting common data from database all the time.

  3. Prevent Unnecessary load/query.

  4. Use CDN






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316289%2floadbalancing-using-php-is-this-effective%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    At the first place most of the server load rely on Sql and heavy query,
    So instead of making multiple servers to handle request. You can do this




    1. Make Sql replica (Slave server) use it for reading and user master for writing.

    2. Use Memcache or Redis to prevent getting common data from database all the time.

    3. Prevent Unnecessary load/query.

    4. Use CDN






    share|improve this answer




























      0














      At the first place most of the server load rely on Sql and heavy query,
      So instead of making multiple servers to handle request. You can do this




      1. Make Sql replica (Slave server) use it for reading and user master for writing.

      2. Use Memcache or Redis to prevent getting common data from database all the time.

      3. Prevent Unnecessary load/query.

      4. Use CDN






      share|improve this answer


























        0












        0








        0







        At the first place most of the server load rely on Sql and heavy query,
        So instead of making multiple servers to handle request. You can do this




        1. Make Sql replica (Slave server) use it for reading and user master for writing.

        2. Use Memcache or Redis to prevent getting common data from database all the time.

        3. Prevent Unnecessary load/query.

        4. Use CDN






        share|improve this answer













        At the first place most of the server load rely on Sql and heavy query,
        So instead of making multiple servers to handle request. You can do this




        1. Make Sql replica (Slave server) use it for reading and user master for writing.

        2. Use Memcache or Redis to prevent getting common data from database all the time.

        3. Prevent Unnecessary load/query.

        4. Use CDN







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 9:41









        Suraj TiwariSuraj Tiwari

        1017




        1017
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316289%2floadbalancing-using-php-is-this-effective%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly