Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

caching readonly data for java application

Tags:

java

caching

I have a database which has around 150K records of data with a primary key on the table. The data size for each record will take less than 1kB. The processing time for constructing a POJO from the DB record takes about 1-2 secs(there is some business logic that takes too much time). This is read-only data. Hence I'm planning to implement caching the data. What I'm thinking to do is. Load the data in subsets(200 records each time) and create a thread that'll construct the POJOs and keep them in a hashtable. While the cache is being loaded(when I start the application) the User will see a wait sign. For storing the data in HashTable is an issue I'll actually store the processed data in to another DB table(marshall the POJO to xml). I use a third party API to load the data from database. Once I load a record I'll have load the data I'll have to load associations for the loaded data and then associations for the association found at the top level. It's like loading a family tree.

  1. I can't use Hibernate or any ORM framework as I'm using a third party API to load the data which is shipped with the database it self(it's a product). More over I don't think loading data once is not a big issue.
  2. If there is a possibility to fine tune the business logic I wouldn't have asked this question here.

Caching the data on demand is an option, but I'm trying to see if I can do anything better.

Suggest me if there is a better idea that you are aware of. Thank you./

like image 672
Srini Kandula Avatar asked Jan 21 '23 03:01

Srini Kandula


2 Answers

Suggest me if there is a better idea that you are aware of.

Yes, fix the business logic so that it doesn't take 1 to 2 seconds per record. That's a ridiculously long time.

Before you do that, profile your application to make sure that it is really the business logic that is causing the slow record loading, and not something else. (For example, it could be a pathological data structure, or a database issue.)

Once you've fixed the root cause of the slow record loading, it is still a good idea to cache the read-only records, but you probably don't need to preload the cache. Instead, just load the records on demand.

like image 50
Stephen C Avatar answered Jan 23 '23 16:01

Stephen C


It sounds like you are reinventing the wheel. I'd be looking to use hibernate. Apart from simplifying the code to access the database, hibernate has built-in caching and lazy loading of data so it only creates objects as you request them. Ergo, a lot of what you describe above is already in place and you can concentrate on sorting out your business logic. I suspect that once you solve the business logic performance issue, there will be no need to do such as complicated caching system and hibernate defaults will be sufficient.

like image 42
drekka Avatar answered Jan 23 '23 17:01

drekka