Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Would it be wrong to use a static object instead of a database?

This is essentially a design patterns question:

I was expecting to query a database to get a list of stocks(shares/securites whatever) that are most highly correlated for a given stock.

Instead I thought maybe I should create an object which has a static HashMap and store my data in there. Then "query" it every time I need to.

Would there be anything wrong with this approach, as I believe it would improve performance significantly over querying a database for the same data. The amount of data is relatively small and doesn't grow so that won't cause a problem. Might there be any issues that will bite me later?

like image 502
Ankur Avatar asked May 03 '09 09:05

Ankur


3 Answers

I would still use the database for backup reasons, but on the client use a caching api like oscache to store the data on the local filesystem for quick access, then if the system goes down restore the cache from the database and the carry on using the cache in your code

like image 171
ssmithstone Avatar answered Sep 29 '22 18:09

ssmithstone


If a read-only cache that does not get updated automatically is all you need for your application, then there's nothing wrong with this approacht.

There is one caveat though: If the amount of data (inlcuding the overhead of Java objects and the HashMap) becomes too big to be held entirely in RAM, performance will decrease quickly and drastically (by a factor of 10,000 or more). Database systems are designed for efficient access to on-disk data; a HashMap isn't.

like image 22
Michael Borgwardt Avatar answered Sep 29 '22 20:09

Michael Borgwardt


The issues that will get you are if the data does change and you need to update the hash and recompile every time.

That being said, you know the data, you can tell how much that is going to affect you.

One option could be that you keep the data in a database, but then cache it to a HashMap.

like image 44
Robin Day Avatar answered Sep 29 '22 19:09

Robin Day