How much memory needed by MongoDB?

Hi there,
I need to correctly size MongoDB dedicated memory in my architecture.

I have a 3 node Replica Set running on Docker Swarm with other services.

I started with no memory limits (containers see the total amount of RAM of the host) and I’ve found that MongoDB is eating up memory day by day (even more than 10GB in my case) because he usually use 50% of RAM for wiredTiger cache and seems not to release it (or release very slow). The 50% rule can probably be good if the machine is MongoDB dedicated but if you have other services (like me) this leads the machine to swap a lot in the long term (solved with reboots in last months).

Now I’m digging into it and I’ve found I can limit the memory on container via Docker and/or via --wiredTigerCacheSizeGB option directly from mongod.

I’m trying with 6GB limit on containers letting MongoDB managing the wiredTiger cache size (db.serverStatus().wiredTiger.cache['maximum bytes configured'] reports 2.5 GB).

Actual usage (from docker stats):

Percona MongoDB 8.0

Primary node mem usage 5.417GiB / 6GiB
Secondary node mem usage 2.595GiB / 6GiB
Secondary node mem usage 2.711GiB / 6GiB

Primary (db.serverStatus().wiredTiger.cache formatted)

{
  'maximum bytes configured': '2.50 GB',
  'bytes currently in the cache': '2.00 GB',
  'tracked dirty bytes in the cache': '0.01 GB'
}

Secondary (db.serverStatus().wiredTiger.cache formatted)

{
  'maximum bytes configured': '2.50 GB',
  'bytes currently in the cache': '2.00 GB',
  'tracked dirty bytes in the cache': '0.01 GB'
}

Secondary (db.serverStatus().wiredTiger.cache formatted)

{
  'maximum bytes configured': '2.50 GB',
  'bytes currently in the cache': '1.68 GB',
  'tracked dirty bytes in the cache': '0.01 GB'
}

Primary container is on 90% usage and the memory warning is often triggered.

Is this primary/secondary difference ok?

This means that the primary is using 3.417GiB (5.417GiB - 2.00 GB) of RAM that is not wiredTiger cache?

Do you know approximately how much RAM MongoDB needs, net of cache?
Is this proportional to the number/size of dbs to be managed?

I’m still studying so corrections and suggestions are appreciated :wink:

Thanks in advance for the patience :slight_smile:

Hi, memory difference between primary and secondaries is expected, as most of the time the usage patterns are different.
MongoDB leverages both OS caches and its own cache (WTcache) so typically you dedicate half your system memory to each.

Hi Ivan, thanks for your reply.

Still don’t get if I can predict someway how much RAM MongoDB primary needs.

I’m trying to increase it 1GB at a time but seems that after a couple of days it always reach 90% and our monitoring system trigger a memory warning marking the service as unhealthy.

Is this a false-positive?
Can I still consider the service as healthy?
In this case how can I recognise an unhealthy service if I cannot relay to mem usage?

Thanks in advance for the patience :slight_smile:

It is not straight forward to predict. Memory usage will be impacted by the size of your data but also number of client connections.
I would suggest using a different criteria for checking unhealthy service, for example you can have a script running a query and measure the response time/check for timeout.

Thanks for the hint :+1: