Skip to content
Advertisement

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

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?

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement