<p>We run MongoDB mongod processes inside Docker containers in Kubernetes with clear memory limits.</p>

<p>I am trying to configure the mongod processes correctly for the imposed memory limits.</p>

<p>These are the information I could collect from the docs:</p>

<ul>
<li>The memory usage of MongoDB is correlated to the WiredTiger cache size. Its is calculated using the formula 50% of (RAM - 1 GB) or a minimum of 256 MB <a href="https://docs.mongodb.com/manual/core/wiredtiger/#memory-use" rel="nofollow noreferrer">https://docs.mongodb.com/manual/core/wiredtiger/#memory-use</a></li>
<li>RAM is the total amount of ram available on the system. In the case of containerized nodes, it is the available memory to the container (since MongoDB 4.0.9) <a href="https://docs.mongodb.com/manual/faq/diagnostics/#must-my-working-set-size-fit-ram" rel="nofollow noreferrer">https://docs.mongodb.com/manual/faq/diagnostics/#must-my-working-set-size-fit-ram</a></li>
<li>“If you run mongod in a container (e.g. lxc, cgroups, Docker, etc.) that does not have access to all of the RAM available in a system, you must set storage.wiredTiger.engineConfig.cacheSizeGB to a value less than the amount of RAM available in the container.” <a href="https://docs.mongodb.com/manual/faq/diagnostics/#must-my-working-set-size-fit-ram" rel="nofollow noreferrer">https://docs.mongodb.com/manual/faq/diagnostics/#must-my-working-set-size-fit-ram</a></li>
<li>The docs state that increasing the WiredTiger cache size above the default should be avoided.  <a href="https://docs.mongodb.com/manual/faq/diagnostics/#must-my-working-set-size-fit-ram" rel="nofollow noreferrer">https://docs.mongodb.com/manual/faq/diagnostics/#must-my-working-set-size-fit-ram</a></li>
</ul>

<p>This information is a little unclear. </p>

<p>Do I leave the default values of the WiredTiger cache size or do I set it to  "a value less than the amount of RAM available in the container"? How much lower should that value be? (a higher value than the default would also contradict the advice to not increase it above the default value)</p>

<p>The default is to allow the WiredTiger cache to use slightly less than half of the total RAM on the system.</p>

<p>The process normally determines the total RAM  automatically by querying the underlying operating system.</p>

<p>In the case of a Docker container which has been allocated 16GB of RAM but is running on a host machine that has 128GB RAM, the system call will report 128GB.  The default in this case would be 63GB, which obviously would cause a problem.</p>

<p>In general:</p>

<ul>
<li>Use the default in situations where the system call reports the true memory available in the environment.  This includes bare metal, most VMs, cloud providers, etc.</li>
<li>In containers where the amount of memory reported by the system call does not reflect the total amount available to the container, manually make the calculation for what the default would have been if it did, and use that value instead.</li>
</ul>
