mongodbwiredtiger

Cache only high-usage keys in MongoDB


Suppose we have a simple blog with many posts. And we regularly execute a query to get list of post's title and date from the posts collection.

So, What will cache inside of the Ram in this scenario(except indexes)? the whole document or only _ids, titles and dates?

The documentation doesn't clear this:

MongoDB keeps most recently used data in RAM. If you have created indexes for your queries and your working data set fits in RAM, MongoDB serves all queries from memory

DB version is 4.2.8.


Solution

  • It works as below:

    Let's say you have 4 collections. Each contains 2Gig data. And each collection contain 500Mb of data in the index.

    Totally: 8Gb data, 2Gb index. You query on one collection frequently and one particular query on that collection. You can assume that it keeps the data related to the query which you execute frequently in the cache. It includes data in the index and actual data related to it from the desk.

    It doesn't keep the result in the index. So it keeps the whole document and the index data for the frequent query in the cache.