People tend to use in-memory caches for faster response times and avoiding the re-computation of what could be prevented for the time being.
A simple in memory cache looks like:
const cache = {};
cache["id"] = {title: "...", score: "2", computations: {...}}
This is usually how I have seen people use the in memory cache. Mostly it would be a map
or an array
or any other data structure declared globally and accessible to the complete application)
But, here is my experience with the in-memory cache for a very data heavy application. Whenever I have started storing lots of data in the in-memory-cache, it starts to give "heap out of memory error". I understand this, but what then is the effective way to use an in-memory cache?
If I understand this correctly, everything (the object or the cache) resides inside the heap. And since this is a finite resource, pumping in more data will eventually give errors like these. But I have no idea on how to effectively use the in-memory cache. Are there already best practices established? Should there be a routine that continuously checks for the "size" of the cache object and free if necessary?
What if I need to cache data around 10GB? I know (as already pointed in the link above), that I could always increase the heap size, but is this the only thing I need to know when working with in-memory cache or the heap? (Continuously increasing the heap size using node --max-old-space-size=xMB yourFile.js) does not seem right.
I have always imagined an in-memory cache to be a very powerful tool, but have never been able to use it effectively. (As at some point, I always had to face heap out of memory error)