MongoDB Cache how does it work?

Hi @Florian_Silbereisen,

It’s impossible to guarantee that the data didn’t change between your first and second query. If you cache the data, you might just show stale data to your second user.

You could totally add a cache layer in your Node app if this is the expected behaviour you want. But it’s probably not what I would do.

That being said, MongoDB is already using RAM to “cache” the most recently used document in RAM. So next time you ask for these documents, mongod doesn’t have to fetch them from disk, as they are already available in RAM. This concept is called the “working set”.

For this to work efficiently, you have to make sure that you have enough RAM for your indexes + working set + query execution (evil sort in-memory, etc), and you have to make sure that you don’t evict these documents from RAM too quickly. Else this might be a sign that you need more RAM.

Cheers,
Maxime.

3 Likes