Threads and Load Balance

Discussion in 'Java' started by Bob Rivers, Dec 15, 2003.

  1. Bob Rivers

    Bob Rivers Guest

    Hi,

    We have a web application and due to the heavy traffic, my company
    decided to put it under a load balance structure.

    We are thinking about the usage of a "standard" load balance
    structure:

    Load Balancer
    |
    ----------
    | |
    Server1 Server 2

    I was doing some research, and I found some interesting solucions,
    like the one described at
    http://jakarta.apache.org/tomcat/tomcat-5.0-doc/cluster-howto.html.

    But none of these solutions talk about threads. Into the jakarta
    solution, they describe session replication. And what about threads?

    My problem is that I have a thread that continuosly monitors a
    database. If this monitor finds something (ie, the due date of an
    order) it sends an email.

    The problem is that this thread is (I think) attached to the web
    server. If I have 2 (or more) servers, this thread will be started
    twice, and someone will receive two emails....

    So, how do I do threads under a load balance schema?

    I am using simple servlets (with J2SE 1.3.1). The server that we are
    using is tomcat 4.1.29.

    TIA,

    Bob
     
    Bob Rivers, Dec 15, 2003
    #1
    1. Advertising

  2. Bob Rivers

    Andy Fish Guest

    obvious solutions would be:

    (a) have the emailer thread run in a separate JRE not on the web server.

    (b) use some kind of locking mechanism to ensure that only 1 web server is
    trying to send emails e.g. have a table in the database which both servlets
    will try to insert the same key value into. whoever succeeds gets the right
    to send emails. The problem with this one is that you need to coordinate
    startup/shutdown and possibly timeouts if one server crashes.

    (c) have a status on the order table that says whether an email has been
    sent. that way, whichever thread picks up the order first will send the
    email. This is quite resilient but may mean extra work in your application.

    "Bob Rivers" <> wrote in message
    news:...
    > Hi,
    >
    > We have a web application and due to the heavy traffic, my company
    > decided to put it under a load balance structure.
    >
    > We are thinking about the usage of a "standard" load balance
    > structure:
    >
    > Load Balancer
    > |
    > ----------
    > | |
    > Server1 Server 2
    >
    > I was doing some research, and I found some interesting solucions,
    > like the one described at
    > http://jakarta.apache.org/tomcat/tomcat-5.0-doc/cluster-howto.html.
    >
    > But none of these solutions talk about threads. Into the jakarta
    > solution, they describe session replication. And what about threads?
    >
    > My problem is that I have a thread that continuosly monitors a
    > database. If this monitor finds something (ie, the due date of an
    > order) it sends an email.
    >
    > The problem is that this thread is (I think) attached to the web
    > server. If I have 2 (or more) servers, this thread will be started
    > twice, and someone will receive two emails....
    >
    > So, how do I do threads under a load balance schema?
    >
    > I am using simple servlets (with J2SE 1.3.1). The server that we are
    > using is tomcat 4.1.29.
    >
    > TIA,
    >
    > Bob
     
    Andy Fish, Dec 15, 2003
    #2
    1. Advertising

  3. Bob Rivers

    Hylander Guest

    (Bob Rivers) wrote in message news:<>...
    > Hi,
    >
    > We have a web application and due to the heavy traffic, my company
    > decided to put it under a load balance structure.
    >
    > We are thinking about the usage of a "standard" load balance
    > structure:
    >
    > Load Balancer
    > |
    > ----------
    > | |
    > Server1 Server 2
    >
    > I was doing some research, and I found some interesting solucions,
    > like the one described at
    > http://jakarta.apache.org/tomcat/tomcat-5.0-doc/cluster-howto.html.
    >
    > But none of these solutions talk about threads. Into the jakarta
    > solution, they describe session replication. And what about threads?
    >
    > My problem is that I have a thread that continuosly monitors a
    > database. If this monitor finds something (ie, the due date of an
    > order) it sends an email.
    >
    > The problem is that this thread is (I think) attached to the web
    > server. If I have 2 (or more) servers, this thread will be started
    > twice, and someone will receive two emails....
    >
    > So, how do I do threads under a load balance schema?


    Probably programmatically use messages between servers to control
    threads/start/check on threads. Perhaps designate one server as a
    master in terms of thread control. You'll have a few cases to deal
    with. ie: force one thread to take over if a failover situation
    arises. Start up, (if one is down, what to do), if both are up, decide
    which one starts and which one sleeps.

    Messages of course can be done in a variety of ways....ie: plain
    sockets, rmi, java's messaging APIs/message driven ejbs, etc etc. the
    other post had some good ideas too. I'd try something simple and then
    go from there. Anyone see anything wrong with this approach? (doing
    your own threading is not kosher in EJBs btw but it doesn't mean you
    can't hack it given you know your implementation and don't mind the
    potential drawbacks...some might be serious enough to avoid such
    things. you could try to do something more along the lines of a plain
    message driven system. Your requirements might vary from what I'm
    thinking too....some people want preemptive abilities/realtiming....in
    which case, you might want to check if your OS supports it or has a
    modified kernel available.)

    > I am using simple servlets (with J2SE 1.3.1). The server that we are
    > using is tomcat 4.1.29.
    >
    > TIA,
    >
    > Bob
     
    Hylander, Dec 16, 2003
    #3
  4. "Andy Fish" <> wrote in message news:<uZoDb.6503$>...
    > obvious solutions would be:
    >
    > (a) have the emailer thread run in a separate JRE not on the web server.
    >
    > (b) use some kind of locking mechanism to ensure that only 1 web server is
    > trying to send emails e.g. have a table in the database which both servlets
    > will try to insert the same key value into. whoever succeeds gets the right
    > to send emails. The problem with this one is that you need to coordinate
    > startup/shutdown and possibly timeouts if one server crashes.
    >
    > (c) have a status on the order table that says whether an email has been
    > sent. that way, whichever thread picks up the order first will send the
    > email. This is quite resilient but may mean extra work in your application.



    No matter what, you're going to have to separate this process from
    your servlet. If you stop and think about it, it really doesn't make
    sense for a servlet to be more concerned about changes to a database
    than incoming web traffic.

    Solutions (b) and (c) above will both sort-of work, except leave the
    door wide open to a classic race condition. The simplest solution is
    to simply go with (a) and write something else to handle
    notifications.

    I assume a servlet is responsible for changes to the database. If
    certain page requests always preceed the database change you're
    looking for, have the servlet check to see if an email is required as
    the last thing. The request will only go to one server so you don't
    have to worry about duplication in this case.

    If you are using a database that supports triggers, then use them.
    Rather than poll your database server continuiously, let it notify you
    when something important happens.

    ---
    Jared Dykstra
    http://www.bork.org/~jared
     
    Jared Dykstra, Dec 18, 2003
    #4
  5. Bob Rivers

    Roedy Green Guest

    On 18 Dec 2003 00:20:07 -0800, (Jared Dykstra)
    wrote or quoted :

    >Jared Dykstra


    By any chance are you related to THE Edsger Dykstra?

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Dec 18, 2003
    #5
  6. Roedy Green <> wrote in message news:<>...
    > On 18 Dec 2003 00:20:07 -0800, (Jared Dykstra)
    > wrote or quoted :
    >
    > >Jared Dykstra

    >
    > By any chance are you related to THE Edsger Dykstra?


    LOL, I didn't know my message was *that* profound ;-)

    ---
    Jared Dykstra
    http://www.bork.org/~jared
     
    Jared Dykstra, Dec 18, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Max
    Replies:
    0
    Views:
    397
  2. Kevin C
    Replies:
    1
    Views:
    2,309
    Kevin C
    Oct 20, 2004
  3. Replies:
    6
    Views:
    3,792
    Thomas Weidenfeller
    Jun 8, 2005
  4. Paul
    Replies:
    1
    Views:
    348
    bruce barker \(sqlwork.com\)
    Jun 29, 2006
  5. EAI
    Replies:
    0
    Views:
    343
Loading...

Share This Page