MongoDB preloads documents into RAM to improve performance

I want MongoDB to store query results in RAM for a longer period of time (say, 30 minutes if memory is available). Is it possible? OR is there any way so that I can make sure that the data is pre-loaded into RAM before subsequent requests on it.

I actually wonder about the simple performance of query results from MongoDB. I have a dedicated server with 10 GB of RAM, and my db.stats ():

db.stats(); { "db": "test", "collections":16, "objects":625690, "avgObjSize":68.90, "dataSize":43061996, "storageSize":1121402888, "numExtents":74, "indexes":25, "indexSize":28207200, "fileSize":469762048, "nsSizeMB":16, "ok":1 } 

Now when I request a separate document ( as indicated here ), it loads from the web service in 1.3 seconds. Subsequent calls to the same requests give a response of 400 ms, and then after a few seconds it starts to take 1.3 seconds again. It seems that MongoDB has lost the previous requested document from memory, where there are no other requests requesting data displayed in RAM.

Please explain this and let me know how to respond more quickly to subsequent requests.

+4
source share
1 answer

Your observed performance issue on initial request is probably one of the following issues (in a rough order of probability):

1) Your application / web service has some overhead for initializing on the first request (i.e. allocating memory, setting up connection pools, resolving DNS, ...).

2) The requested indexes or data has not yet been stored in memory, so they must be loaded.

3) The query optimizer may take a little longer for the first request because it compares the execution of the plan for the query template.

It would be very useful to test the request using the mongo shell and isolate whether the service data is associated with MongoDB or your web service (and not with the synchronization of both, as you did).

The following are some notes related to MongoDB.

Caching

MongoDB does not have "caching" time for documents in memory. It uses memory mapped files for disk I / O, and documents in memory are based on your active requests (documents / indexes you recently downloaded), as well as available memory. The operating system virtual memory manager is responsible for caching and will usually follow the least used algorithm (LRU) to decide which pages will be replaced from memory.

Memory usage

The expected behavior is that over time, MongoDB will grow to use all free memory to store the active working dataset.

Looking at your provided db.stats() numbers (and assuming this is your only database), it looks like your database is about 1 GB in size, so you should be able to store everything in your total 10Gb if:

  • there are other processes competing for memory
  • you rebooted the mongod server and these documents / indexes have not yet been requested.

In MongoDB 2.2, there is a new touch command that you can use to load indexes or documents into memory after restarting the server. This should be used only at initial start-up to โ€œwarm upโ€ the server, since otherwise you could uselessly force the actual โ€œactiveโ€ data out of memory.

On a Linux system, for example, you can use the top command and you should see that:

  • virtual bytes / VSIZE will be the size of the entire database
  • If the server does not have other running processes, the resident bytes / RSIZE will be the shared memory of the machine (including the contents of the file system cache).
  • mongod should not use swap (since files are mapped to memory)

You can use the mongostat tool to get a quick view of mongod activity .. or itโ€™s more useful to use a service like MMS to track metrics over time.

Query optimizer

MongoDB The query optimizer compares the execution of the plan for the query template every 1000 writes, and then caches the "winning" query plan until the next optimizer starts .. or you explicitly call explain() in this query.

This should be simple to check: run your request in the mongo shell using .explain() and look at ms timeouts, as well as the number of index entries and documents to check. Timing for explanation () is not the actual time it will take for the requests to complete, as it includes the costs of comparing plans. Typical execution will be much faster .. and you can search for slow queries in your mongod .

By default, MongoDB will log all requests more slowly than 100 ms, so this provides a good starting point for finding queries for optimization. You can adjust the slow ms value with the --slowms option or with the Database Profiler commands.

Further reading in the MongoDB documentation:

+7
source

Source: https://habr.com/ru/post/1445933/


All Articles