feiroox
feiroox

Reputation: 3199

Hold most of the object in cache/memory insted of database?

It just occurred to me why not to have most of the objects in a cache(memory) when an application start.

if it's not that large web application. Or to have a settings for how much I want to put in the cache/memory.

I just guess it could require to have something like below 1 GB RAM or a lot less.

Everything in order to speed up the application even more by not querying database.

Is it good idea?

Upvotes: 3

Views: 807

Answers (6)

John Smith
John Smith

Reputation: 351

The details are beyond the scope of an answer here, but we have had good experience of using ehCache ( http://ehcache.org/ )

The combination of support for distributed caches, and overflow to disk has allowed us to keep large numbers of computationally heavy, but fairly unchanging pages in the cache for a site being served from multiple tomcats.

Distribution addresses the question of staleness (if you invalidate your items correctly) and the disk overflow allows us to basically cache everything which was just not feasible with an in-memory cache.

Of course the implementation is not trivial for a real world application, but it improved our performance significantly once the caches were bubbling.

Upvotes: 0

Uri
Uri

Reputation: 89749

Caching is definitely a good idea.

Databases are also not a catch-all solution, though you have to be careful about consistency between runs of your program. What if you change the data but your program crashes before you update it to the database?

There are also lightweight memory resident databases that can let you keep your current queries for now, but run much stuff from memory. Using an ORM tool instead of SQL is particularly effective for this since the switch is almost transparent.

Upvotes: 0

George
George

Reputation: 8378

You don't persist objects to database. What you persist is object's state. So that you can have exactly the same state even after your app stops/closes/restarts. If you want to keep states of your objects persisted, you have no choice, but to use db (or anything else, that allows you to write data to file system).

Upvotes: 0

Gala101
Gala101

Reputation: 464

You can maintain a cache of Frequently Used objects in the memory, just don't forget to add methods to refresh the cache when the underlying database state changes.

Eg: If you have a user's table and you need user names in many many pages, then load the entire table in cache at time of Application Startup, just make sure to update the cache when you are adding new users online or modifying / deleting entries from user table

Upvotes: 0

Ryan Elkins
Ryan Elkins

Reputation: 5797

Caching is definitely a good idea and is widely used, but it has to be implemented correctly. There are plenty of pitfalls if done incorrectly. Try looking into one of the big proven systems, like memcached.

Upvotes: 0

Alexander Pogrebnyak
Alexander Pogrebnyak

Reputation: 45576

Quickly becomes Not so good idea, when some other node starts updating database.

In that case your cache will be holding stale data.

Upvotes: 1

Related Questions