File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Performance and the fly likes Data Caching Best Practices ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Data Caching Best Practices ?" Watch "Data Caching Best Practices ?" New topic
Author

Data Caching Best Practices ?

Prasath Thirumoorthy
Ranch Hand

Joined: Jul 23, 2004
Posts: 65
Dear Friends,

Currently In our application we have the performance issue due to data loading while the application starts.

Currently we load all the data from the tables say n which is needed when the applications starts.

for eg: loading the Country combobox to choose specific country of the customer.

we load all the data from tables and load it in the hashmap when the applications and its retrieved from it wherever is needed.

Since the data is large now its becomes an performance issue.

Please help me by providing some ideas or links to do best practices of data caching mechanism or loading these data in a dedicated separate remote server to increase the performance of the application server.

Thanks & Regards,
Prasath


Thanks,
Prasath

SCJP1.4, SCWCD
Virag Saksena
Ranch Hand

Joined: Nov 27, 2005
Posts: 71
It is okay to cache static lookup data which does not change or grow. So the country lookups would be a good candidate.

However you mentioned that the cache data is growing, which means you are starting to cache transactional data. Review the list of tables you are caching. A rule of thumb I have used is ... if the information is below 100 rows, adding it to a cache is a no-brainer. Above that you need to examine on a case by case basis.

Most of the times cache becomes a crutch for untuned DB queries. Ideally most of your transactional queries should return in 10-20 ms or so 95% of the time. If that is not happening, you need to fix that problem, rather than trying to cache data in Java.


<a href="http://www.auptyma.com" target="_blank" rel="nofollow">The Peak of Performance</a>
Prasath Thirumoorthy
Ranch Hand

Joined: Jul 23, 2004
Posts: 65
Hi,

Thanks for your kind reply.

Sorry for that I have not clearly pictured the problem which made it you to misunderstood the problem.
I am not caching the transactional data.Caching only the static data which can be changed through the application.

Static data is keep growing in the sense that for eg say initially we are showing 100 products in the combobox in the application.After some time there are some new products coming as the requirement enhancements.
So what we do means again we reloading the static data i.e the product table with say 200 new products.Now finally we show 300 products in the same combobox like this same scenario we have around 10 tables where we are loading the static data when the application starts.

Now its becomes an performance issue in the cache due increase in size of the static data.No problem with transactional data.

Could you provide me with some optimized (mechanism)solution for the same.
Provide me with some links where i can find some enough information.

Thanks & Regards,
Prasath
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
Now its becomes an performance issue in the cache due increase in size of the static data.


Exactly what is the performance problem? Memory use? Response time?
Sending the markup for a larger combobox is just naturally going to take longer than sending the markup for a small combobox.

How much processing time is used in formatting the cached data into the HTML combobox markup? Are you caching raw data or HTML text?

Bill
Prasath Thirumoorthy
Ranch Hand

Joined: Jul 23, 2004
Posts: 65
Hi William,

Thanks for your kind reply.

The performance issue i mentioned is because of the large memory occupied in the server. as a result , the server response is slow and as per the end user , the application is something near to HANG..So can you please provide me some data caching solutions or using some proxy design solution and the implementation of that..


Thanks in Advance ,
Prasath
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
The performance issue i mentioned is because of the large memory occupied in the server. as a result , the server response is slow and as per the end user , the application is something near to HANG.


I can't see why large memory use would make the server response slow UNLESS your server has small memory and the operating system is pageing memory in and out.

Before jumping to conclusions lets find out what the computer is doing during these slow responses. On a windows system I would be looking at the Task Manager statistics. Are there any database operations going on while a page is being formatted?

You did not respond to my question about how the data is used. Are you storing raw data that takes some computation for each request before the combo box HTML can be sent or are you storing Strings as close as possible to what you need to create the combo box?

If you want to start playing with caching, the WhirlyCache project is active and well supported. But I suggest more detective work to locate the real cause of the slow response.

Bill
Prasath Thirumoorthy
Ranch Hand

Joined: Jul 23, 2004
Posts: 65
Hi William,

Yes,I am storing the raw data. i.e I am just retriving the data from database and put it in the hashmap and from hashmap I retrive it and load the combobox.

The hashmap which contains the table name as keys and their rows as values
It retrives all the values say 10 tables their records more than 300 approximately and load the hashmap when the server starts.

Whether we can use other than hashmap for cache implementation which occupies low memory than hashmap ?

Thanks & Regards,
Prasath
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1850
Hi

We have done the same in many projects where in data is stored in the application context of a web application.

The "common" data like country, languages etc should not take that much of a memory.

You perhaps need to check what exactly are you loading and if so if you need to cache it at all.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
Yes,I am storing the raw data. i.e I am just retriving the data from database and put it in the hashmap and from hashmap I retrive it and load the combobox.


If the data in the database/hashmap does not change, consider generating the HTML String that creates the combo box directly from the data as retrieved from the database and storing that String. You can then write that String into the HTML without having to do any extra computation.

Bill
Ganpi Srinivasan
Ranch Hand

Joined: Aug 17, 2005
Posts: 160
Originally posted by Prasath Thirumoorthy:
Hi William,

Yes,I am storing the raw data. i.e I am just retriving the data from database and put it in the hashmap and from hashmap I retrive it and load the combobox.

The hashmap which contains the table name as keys and their rows as values
It retrives all the values say 10 tables their records more than 300 approximately and load the hashmap when the server starts.

Whether we can use other than hashmap for cache implementation which occupies low memory than hashmap ?

Thanks & Regards,
Prasath


Do you guys have enough JVM allocation on the server? If not, try increasing the JVM max size. Though this could be a work-around, you might want to re-visit the design. If data like country etc. does not change, try giving a singleton implementation - this way, there is only one collection per JVM holding this information.

Also try running your application through a profiler. This could give you an idea on the memory consumption (even on a per-object level) and tell you if there are any memory leaks. If there are memory leaks, do a code review and see if you can dereference objects when not in use.

Good Luck!
Prasath Thirumoorthy
Ranch Hand

Joined: Jul 23, 2004
Posts: 65
Hi Friends,

Thank you very much for your replies.
JVM memory is already increased to a optimized size.

Actually we are not loading a simple combobox contains which contains list of countries.In Banking application scenario we are loading the costdetails, product details,severity details to calculate the economic capital.

Application is developed using Java and Swings.So the data retrieved from the database not only used to display in the page and also used to do some calculations which is needed in the background.

while starting the Server these details all retrieved from the database and stored in a HashMap.In future wherever the data is needed its retrieved from this HashMap, this is implemented using singleton pattern.

Because of this huge data get stored in hashmap, its taking time for loading while the server starts and occupies JVM Memory.

I thought to have a remote server for loading these cost details inorder to reduce the load in the main server.

I gone through some documentation of Apache Java Caching System using Remote Cache.

Whether this suitable for my problem.Please guide me

Thanks & Regards,
Prasath
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
The Java Caching System certainly sounds like it fits your problem if your network has a separate computer that can manage the remote cache and free up your server.
It could also avoid the big delay while the server is restarted since the cache can continue running.

The trade-off will of course be the extra network activity.

Please keep us up to date with your experiments in this area, caching questions come up alot in this forum.

Bill
Prasath Thirumoorthy
Ranch Hand

Joined: Jul 23, 2004
Posts: 65
Hi William,

Thanks for your kind reply.
Ya sure I will keep update about my work.

Just started digging deep and exploring about JCS.
How to plug-in into my current application.

Thanks & Regards,
Prasath
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Nothing is free so this idea does not come without a cost ... You could defer loading the cache until somebody asks for data. Then the first user who needs value X will take a hit to read it and store it in the cache, but everybody afterwards will get the benefit. The benefit is greatly reduced startup time, the cost is that first user.

If you keep your current load-at-startup design you might look into multiple threads to load the cache. If they spend a lot of time waiting for database queries they might interleave nicely.

I like the off-boarded cache idea and will have to read up on that, myself.

I'm always sure to have a way to purge the cache and force a reload so we don't have to bounce servers to pick up new values. You might want an admin tool to purge specific keys if not the whole thing. With the deferred lazy-load scheme you already have the code to reload on demand.


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
Prasath Thirumoorthy
Ranch Hand

Joined: Jul 23, 2004
Posts: 65
Hi William,

While I was exploring about Java Caching System (JCS), I came across lot of open source and commercial Object Caching frameworks,

Open Source
1.Java Caching System (JCS) from Jakarta (part of the Turbine project)
2.OSCache
3.Commons Collections (another Jakarta project)
4.JCache API (SourceForge.net)

Commercial
1.SpiritCache (from SpiritSoft).
2.Coherence (Tangosol)
3.Javlin (eXcelon)
4.Object Caching Service for Java (Oracle)

Anyway I prefer Open Source only, among those open source frameworks, I hope OSCache sounds better in terms of clustering, serilzation and other caching elements.

Could you please tell your suggestion on the same.Now I am in a bit confusion which to choose ?

Thanks & Regards,
Prasath
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Data Caching Best Practices ?