Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know if MongoDB needs more CPU/RAM? [closed]

Tags:

linux

mongodb

I have a MongoDB server running on an inexpensive Linux VPS (1 x 2.0GHz CPU and 1GB RAM).

It's been about a day now and the output of db.stats() looks reasonable.

db.stats()
{
        "db" : "app",
        "collections" : 11,
        "objects" : 2067,
        "avgObjSize" : 238.20416061925496,
        "dataSize" : 492368,
        "storageSize" : 1007616,
        "numExtents" : 18,
        "indexes" : 9,
        "indexSize" : 138992,
        "fileSize" : 16777216,
        "nsSizeMB" : 16,
        "dataFileVersion" : {
                "major" : 4,
                "minor" : 5
        },
        "extentFreeList" : {
                "num" : 5,
                "totalSize" : 286720
        },
        "ok" : 1
}

I can project the number of days until I exceed my disk space.

I know (from the FAQs) "MongoDB automatically uses all free memory on the machine as its cache."

  1. How should I determine whether I need more RAM or CPU capacity?

  2. Although it's application dependent, is either RAM or CPU generally more important?

  3. Is there anything returned by db.runCommand( { serverStatus: 1, workingSet: 1 } ) that I should pay particular attention to?

like image 766
okoboko Avatar asked Jun 14 '14 06:06

okoboko


People also ask

Does MongoDB need lots of RAM?

MongoDB requires approximately 1 GB of RAM per 100.000 assets. If the system has to start swapping memory to disk, this will have a severely negative impact on performance and should be avoided.

How can you check the amount of mapped memory that MongoDB is using?

You can inspect mem. mapped to check the amount of mapped memory that mongod is using. If this value is greater than the amount of system memory, some operations will require a page faults to read data from disk.

Is MongoDB NUMA aware?

NUMA (Non-Uniform Memory Access) Architecture Non-Uniform Memory Access is a recent memory architecture that takes into account the locality of caches and CPUs for lower latency. Unfortunately, MongoDB is not “NUMA-aware” and leaving NUMA setup in the default behavior can cause severe memory in-balance.

Is MongoDB resource intensive?

Indexes are resource-intensive: even with compression in the MongoDB WiredTiger storage engine, they consume RAM and disk. As fields are updated, associated indexes must be maintained, incurring additional CPU and disk I/O overhead.


1 Answers

How should I determine whether I need more RAM or CPU capacity?

It's very important to have enough memory so your indexes and the data can fit in the memory to achieve good performance. If your workingSet doesn't fit in the memory you will have a lot of page faults. Page faults occur when MongoDB requires data that is not loaded in the physical memory and it must read from virtual memory (i.e. disk). Accessing disk instead of memory will degrade your performance significantly as accessing the disk is orders of magnitude slower than accessing data in the memory. SSD disks are good, but they are still much slower than RAM.

People often forget that open connections also require memory. On Linux, memory overhead for each connection is about 10 MB (on older versions like v1.8). Since v1.9 this overhead was changed to be around 1MB according to this JIRA ticket. So if you have 100 open connections, that will that will roughly translate to 1GB (on older) and 100MB (on newer versions) of memory usage (this number could have changed with most recent MongoDB versions, so YMMV). You can check the connections in the output of your serverStatus command to see info about the current / available number ​of connections.

I recommend that you read the MongoDB diagnostics.

Although it's application dependent, is either RAM or CPU generally more important?

Having a better CPU is important for tasks that are CPU intensive like: scanning and sorting, updating and rebalancing indexes, map-reduce, aggregation framework commands and server-side JavaScript. Having a better CPU will help with those tasks, but if you don't have enough memory and you're constantly reading from the disk, your performance will be degraded.

Is there anything returned by db.runCommand( { serverStatus: 1, workingSet: 1 } ) that I should pay particular attention to?

Command serverStatus is a very useful tool to collect the statistics about your server and analyze your server stats.

In output of the serverStatus command you should pay attention to:

  • mem contains info about the current memory usage (virtual and phyisical i.e. resident)
  • workingSet section contains values useful for estimating the size of the working set, which is the amount of data that MongoDB uses actively.
  • extra_info - especially the page_faults counter
like image 160
Christian P Avatar answered Oct 05 '22 13:10

Christian P