File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Performance and the fly likes caching and load balancing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "caching and load balancing" Watch "caching and load balancing" New topic

caching and load balancing

Raj Bhandari
Ranch Hand

Joined: Oct 19, 2005
Posts: 97
Hi All,

Can you suggest me how to solve this issue ?
I have a singleton class and within that class I have a static hashtable which acts as a cache by containing all my dropdown list values.
I also have a button in my app which reloads this hashtable incase any new dropdown values are added to the database.
The problem is that now when my application is being deployed.. there are going to be 2 webservers and a load balancer inbetween. My question is if I click on the reload data button mentioned above, that will only reload data in the hash on one webserver and not the other. How should I resolve this issue and what is the general practice ?
Tks a lot!

steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
There are distributed cache products. I think a pay one is by Tangasol. There may be open source ones.

There are a few roll your own approaches too.

1) an admin screen in both web apps that you click on 'refresh cache'.
2) Check a database table on a timer. If the table has a 'refresh cache' date after the last time the cache was refreshed then refresh the cache.
3) Have an ejb or web service that you call and this service calls 'refresh' cache on both of your web apps. This could be a webservice or submission of an http page. - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
There are several ways to broadcast messages like "clear your cache now" among the cluster. JMS in pub-sub mode would seem the most natural. The "reload" button wouldn't clear a cache at all, but send a "clear cache" message. Each server in the cluster - including the one that sent the message - would get the message and clear the cache.

I worked with a vendor framework that sent messages across the cluster through a proprietary socket scheme. Their standard messages included startup, shutdown, refresh, and query status. They allowed other messages with a String argument so I sent messages like "/clearcache/cachename/". The receiver looks up "clearcache" in a map to get a classname, creates an instance and calls execute( commandString ).

Have you worked with JMS? There's a bit of a learning curve but it's not bad and this kind of thing may be very useful over the life of the system.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
rajesh bala
Ranch Hand

Joined: Jan 14, 2003
Posts: 66
If you just have 2 servers and not much worried about exact timing, you can follow this approach.

1. Have a servlet deployed in both the servers.
2. Whenever the hashtable is refreshed, send an intimation to the other servlet (ObjectOutputStream with hastable values or just some event to get it refreshed).
3. Servlet takes care of either updating the cache or refreshing it from DB.

Raj Bhandari
Ranch Hand

Joined: Oct 19, 2005
Posts: 97
Thank you all for the reply.
Sorry, if I was not clear. Let me explain again.
I have one web app deployed on 2 servers.. so 2 copies of the web app on the 2 servers.
There is a reload button in my admin page which calls a servlet which contains the code to reload the hashmap. Now my issue is this servlet will execute on whatever server the loadbalancer sends the request to.. what should I do about the other server ? How do I call the servlet there ?
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
A number of the responses answered your question. jms/web service/ejb/db/open source cache product/commercial cache product could all do what you want.
Raj Bhandari
Ranch Hand

Joined: Oct 19, 2005
Posts: 97
On second thoughts, I agree with you Steve. Tks!
It is sorta covered in the JavaRanch Style Guide.
subject: caching and load balancing
Similar Threads
Using Properties instead of HashMap when dealing with Strings
Actionform not populating JSP
How to set a radio button selected from a JSP page, with the help of values extracted from database?
Handling Browser Back Button & Multiple Tabs
reload collections that initialized on startup?