Apache Server Load Balancer Sticky Non Sticky Sessions


In this tutorial, we'll discuses 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/


    /etc/httpd/conf/httpd.conf

    ################################################################
    ### Non Stickey Session
    ################################################################

    #<Proxy balancer://mycluster>
    #BalancerMember http://localhost:9001 loadfactor=2
    #BalancerMember http://localhost:9002 loadfactor=1
    #</Proxy>

    #ProxyPass /examples balancer://mycluster/examples
    #ProxyPassReverse /examples balancer://mycluster/examples

    #<Location /balancer-manager>
    #SetHandler balancer-manager

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

    ###############################################################

    ################################################################
    ### Stickey Session
    ################################################################
    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
    </Proxy>
    ProxyPass /examples balancer://myclustername/examples stickysession=ROUTEID
    ProxyPassReverse /examples balancer://myclustername/examples

    <Location /balancer-manager>
    SetHandler balancer-manager

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

    server.xml 9001
        <Connector port="9001" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8442" />

    server.xml 9002
        <Connector port="9002" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8442" />

    Tomcat
    Default examples refer for this located under, if you want to deploy make sure deploy the same war/ear file in all nodes/tomcat instance:
    /tomcat9001/apache-tomcat-6.0.43/webapps/examples
    /tomcat9001/apache-tomcat-6.0.43/webapps/examples

    Put your question I'll more than happy to help!!!

    11 comments:

    1. Good post about load balancer spl for sticky and non sticky session....keep posting on...

      ReplyDelete
    2. Nice post about Non Sticky Sessions......

      ReplyDelete
    3. How to work with different server's with IP?

      ReplyDelete
      Replies
      1. Simply change localhost to your actual IP's address for # Non Stickey Session:

        BalancerMember http://IP'sAddress1:9001 loadfactor=2
        BalancerMember http://IP'sAddress2:9002 loadfactor=1


        ProxyPass /examples balancer://mycluster/examples
        ProxyPassReverse /examples balancer://mycluster/examples


        SetHandler balancer-manager

        Order Deny,Allow
        Deny from all
        Allow from all


        Note: IPaddress1 and IPaddress2 will actual IP address, e.g: 10.100.100.100

        For Stickey Session

        Header add Set-Cookie: "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED

        BalancerMember http://IPaddress1:9001 route=node1
        BalancerMember http://IPaddress2:9002 route=node2

        ProxyPass /examples balancer://myclustername/examples stickysession=ROUTEID
        ProxyPassReverse /examples balancer://myclustername/examples


        SetHandler balancer-manager

        Order Deny,Allow
        Deny from all
        Allow from all

        Delete
    4. any idea how load balancer will work for web services...in sticky session??

      ReplyDelete
      Replies
      1. Pass the jsession id or any unique no ans store it for single communication....till success and failure..

        Delete
    5. Good example on sticky session & non sticky session in load balancer...Video is very help full.

      ReplyDelete
    6. If, you want to run an e-shop successfully, then it will be necessary to stick with server configuration parameters to be able to create an error-free website with complete ease.

      ReplyDelete
    7. Dear,
      If node1 is hanged but the port is in listening state, will LB redirect to node2 and node3? Would you please help to know?

      ReplyDelete
      Replies
      1. The Answered for above Question is that, until your Apache Server Load Balancer Sticky Non Sticky Sessions will hit particular port and reachable it will redirect the traffic. if unreachable then it will be redirect to other available nodes...

        Delete
    8. Good explanation and worked for us Apache Server Load Balancer Sticky Non Sticky Sessions. Thx

      ReplyDelete