Tutorial by Vinod Kumat

Apache Load Balancer Hardware and Software


In this tutorial, we'll discussed about (Apache Server) load balancer, hardware & software.

Let's discussed about software load balancer: 
First of all we need to understand the need of load balancer, as in web application, we can used either, web server or application server for deploying the our app. Ensuring our application are working with n-number of users load without failure. That will be done with help of Load balance. See the image below for high level flow:



















  • The client opens a connection to the system such as http://localhost:80/myapp thus creating a web request.
  • The load balancer dispatches the web request onto one of the nodes, depending on the load balancing strategy.
  • The transparent HTTP session failover makes sure that the sessions are replicated across the cluster. Session replication is indicated by dotted blue both side arrow in the figure.


  • Where as app/web server are configure in Node 1 as http://localhost:9001, Node 2 as http://localhost:9002 and Node 3 as http://localhost:9003. All app/web server are connecting with Database (DB).

    Note: port 9001, 9002 and 9003 are for app/web server port where our myapp deployed.

    Need to deploy the three tomcate instance in same machine or different, I did that in same machine with as suggested port.

    Assuming jdk have been installed and JAVA_HOME set.


    /apache-tomcat-6.0.43-9001/
    /apache-tomcat-6.0.43-9002/
    /apache-tomcat-6.0.43-9003/

    Change the port number in /apache-tomcat-6.0.43-900*/conf/server.xml
    /apache-tomcat-6.0.43-9001/
    <Server port="8001" shutdown="SHUTDOWN">
     <Connector port="9001" protocol="HTTP/1.1"

    /apache-tomcat-6.0.43-9002/
    <Server port="8002" shutdown="SHUTDOWN">
     <Connector port="9002" protocol="HTTP/1.1"

    /apache-tomcat-6.0.43-9003/
    <Server port="8003" shutdown="SHUTDOWN">
     <Connector port="9003" protocol="HTTP/1.1"

    We can set up our system to use either sticky sessions or non-sticky sessions.

    Sticky Sessions
    Here, all web requests of one HTTP session are served from the same cluster node.
    If a request of a HTTP session was first dispatched to Node 1 such as http://localhost:9001, all subsequent requests will be dispatched to Node 1, such as http://localhost:9001.

    Non-Sticky Sessions
    Here, web requests are dispatched to random nodes across the cluster, depending on the load balancing strategy.
    If a request of a HTTP session was first dispatched to Node 1 such as http://localhost:9001, subsequent requests might be dispatched to any nodes, such as http://localhost:9002 or http://localhost:9003.

    Without a session replication mechanism, we need to use sticky sessions: on a non-sticky session system, every individual node which has processed the session once will have an individual copy of the session. Take a session cart, for example: on a non-sticky session system, each time the session is processed by a different node, that node holds a representation of the cart at the time.

    Transparent HTTP Session details:
    This is the single time configuration, As soon as the infrastructure for load balancing and transparent HTTP session is set up, we can use the infrastructure for any number of app.

    Configure a load balancer. 
    Typically, in production environments, a hardware load balancer is used. However, for testing or development in project use, for example, a hardware load balancer might be too expensive or not be available. Instead of a hardware load balancer, we can use software load balancers, such as:
       •Apache http Server. 

    How To Use Apache HTTP Server as Load Balancer
    Apache Server is open sour http server and is available for many common operating systems. The Apache HTTP Server can be extended in functionality by integrating modules. 

    How to Use Apache HTTP Server as a Load Balancer:
    Adding the additional functionality by using modules (also called "mods")

    mod_proxy_balancer
    Required. Provides load balancing functionality. For details, see the Apache website: http://httpd.apache.org/.

    mod_headers
    Optional. Required for sticky sessions. For details, see the Apache website: http://httpd.apache.org/.
    How to enable the mods in Appache server ${APACHE_INSTALL}/conf/httpd.conf file and uncomment/add the lines that reference mods. For example, from

    ${APACHE_INSTALL}/conf/httpd.conf

    LoadModule headers_module modules/mod_headers.so
    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
    LoadModule proxy_connect_module modules/mod_proxy_connect.so
    LoadModule proxy_http_module modules/mod_proxy_http.so

    After configuration of mods, what we need to Configure Apache HTTP Server, sticky sessions or non sticky sessions.

    Apache HTTP Server's Configuration for sticky sessions:

    ${APACHE_INSTALL}/conf/httpd.conf

    Listen 80
    Header add Set-Cookie: "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
    <Proxy balancer://myclustername >
    BalancerMember http://localhost:9001 route=node1
    BalancerMember http://localhost:9002 route=node2
    BalancerMember http://localhost:9003 route=node3
    </Proxy>
    ProxyPass /myapp balancer://myclustername/ stickysession=ROUTEID
    ProxyPassReverse /myapp balancer://myclustername/

    <Location /balancer-manager>
    SetHandler balancer-manager

    Order Deny,Allow
    Deny from all
    Allow from all
    </Location>

    Load balancer port is set to 80 and application is visible as http://localhost:80/myapp

    Apache HTTP Server's Configuration for non sticky sessions:

    ${APACHE_INSTALL}/conf/httpd.conf

    Listen 80
    <Proxy balancer://myclustername >
    BalancerMember http://localhost:9001 loadfactor=2
    BalancerMember http://localhost:9002 loadfactor=2
    BalancerMember http://localhost:9003 loadfactor=1
    </Proxy>

    ProxyPass /myapp balancer://myclustername/myapp
    ProxyPassReverse /myapp balancer://myclustername/myapp

    <Location /balancer-manager>
    SetHandler balancer-manager

    Order Deny,Allow
    Deny from all
    Allow from all
    </Location>

    Load balancer port is set to 80 and application is visible as http://localhost:80/myapp

    Note: If Node 1 will down, it will redirect to next available Node 2 or Node 3. If Node 2 is down then it will redirect to Node 1 or Node 3.  If Node 3 is down then it will redirect to Node 1 or Node 2.

    Ref: http://www.apache.org/

    Let's discussed about hardware load balancer:

















    WIP:




    Final Words:
    Thanks! Tutorial/example load balancer. Like & share it. Put your input if any.
    Happy learning and implementation.

    2 comments:

    1. If we consider the Big data consulting services, then adaptive learning is an excellent way to make it successful.

      ReplyDelete
    2. Nice Blog, When I was read this blog, I learnt new things & it’s truly have well stuff related to developing technology, Thank you for sharing this blog. If Someone wants to know about Top Big Data Companies this is the Right place for you!

      ReplyDelete