skip to content

When a MongoDB deployment outgrows its WiredTiger cache, how do you choose between raising the cache, adding memory and sharding, and should two mongods ever share a host?

level: principalimportance: should knowfreq 26%

answer

  1. read-bound or write-bound first
  2. the manual warns against one option
  3. what each gigabyte is taken from
  4. which option is hardest to undo
  5. defaults assume one per machine

basics

~20 s

Raise the cache only when misses dominate and RAM is truly spare; add memory when the working set has outgrown the host; shard when one node's writes or data no longer fit. By default, run one mongod per host.

solid answer

~50 s

I would first separate read pressure from write pressure, because they lead to different answers. Sustained `pages read into cache` with the cache near its 80% target suggests the working set does not fit. Hitting the dirty trigger with saturated storage is a write-throughput problem that more RAM eases only slightly. Raising the cache is cheap, but the manual advises against going above the default, and each gigabyte comes out of the filesystem cache. Adding memory keeps the same ratio without a design change, so it is my usual first move for reads. Sharding is what I reach for when one node's write rate or data can't fit, since it adds lasting operational weight. On co-hosting, the default assumes one `mongod` per machine, so two defaults overcommit. In production I would say no unless there's a written exception with sized caches and separate devices.

go deeper

for a junior

Recall that the default assumes one mongod per machine, and that the manual advises against raising the cache above its default.

for a middle

Explain where a larger cache's memory comes from, and why the filesystem cache's compressed blocks make that a real trade rather than free capacity.

for a senior

Bring evidence to the decision: separate read and write pressure with the cache statistics, and try any change on one secondary before rolling it out.

for a principal

Weigh reversibility and operating cost across the options, set a standing rule for co-hosting with written exceptions, and say when you would break your own rule.

## Start by naming the pressure The WiredTiger internal cache, the storage engine's own memory for collection and index pages, can run short in different ways, and each calls for a different answer. Before debating options, establish which of these you have: - **Read pressure:** sustained `pages read into cache` with usage held near the 80% `eviction_target`, and latency tied to storage reads. The working set does not fit. - **Write pressure:** `tracked dirty bytes in the cache` reaching the 20% `eviction_dirty_trigger`, with the storage saturated. The drain rate is the limit. - **Not the cache at all:** no trigger counters moving, in which case any of these options spends money and attention on the wrong thing. Also check the workload itself. A missing index or an unbounded scan creates memory pressure that no capacity decision should paper over. ## The three options | | Raise the cache | Add memory | Shard | |---|---|---|---| | Helps when | RAM is genuinely spare and misses dominate | the working set outgrew the host | one node's write rate or data cannot fit | | Costs | filesystem cache space, and a restart | a larger host, rolled through the members | routers, config servers, a shard key, more operational work | | Does little for | sustained writes to saturated storage | sustained writes to saturated storage | a poorly chosen key, which concentrates load | | Undoing it | easy | easy | costly | ## Raise the cache, or add memory? The manual's guidance is to **avoid raising the WiredTiger cache above its default**, and where a use case requires it, to specify a share of up to 80% of available memory. That is advice, not a hard limit: a `cacheSizeGB` above 80% starts with a warning. The reason is the trade. Each gigabyte moved to the internal cache comes out of the filesystem cache, and that layer holds compressed blocks, so it can often keep more data per gigabyte. Serving a miss from there costs decompression rather than a disk read. - **Raising** can pay off when misses cost a lot of repeated decompression, and memory on the host is really unused. - **Raising** hurts when the host has no spare RAM, because misses that used to be served from memory begin to reach the disk. - **Adding memory** grows both layers under the same formula and needs no decision about the split, though a fixed `cacheSizeGB` has to be edited as well. I treat it as the default answer for read pressure. Neither one fixes a write-bound system. More cache raises the dirty trigger in bytes and absorbs a bigger burst, but the sustained rate is set by the storage. ## When to shard Sharding splits both the working set and the write load across nodes. It also adds routers and config servers, a shard key that is costly to change, and more failure modes to operate. I would reach for it when the largest sensible host no longer fits the data or write rate, or when failure or maintenance on one huge node has become its own risk, and not as a response to a single cache graph. ## Should two mongods share a host? The manual states that the default cache size assumes a single `mongod` per machine, and that with several you should decrease the setting. So it is allowed, with conditions. The objections are real: - **Memory arithmetic.** Two defaults on a 64 GB host each compute about 31.5 GB, or 63 GB of planned cache, leaving almost nothing for the filesystem cache and other process memory. - **Shared filesystem cache.** It has no per-instance share, so one instance's scan can push out the other's blocks. - **Shared storage.** One instance's dirty eviction can slow the other's on the same device. - **Failure domain.** Two members of one replica set on the same host give up the redundancy that the set exists to provide. Container limits help with memory, but not with storage contention, and they carry their own sizing checks. ## A policy a lead can defend 1. **Production:** one `mongod` per host, by default. 2. **Exceptions:** written down, with explicit `cacheSizeGB` values, separate devices, and never two members of the same replica set together. 3. **Development and test:** co-hosting is fine, provided caches are sized explicitly. 4. **Capacity reviews:** look at read and write pressure separately, and at sustained trigger rates. 5. **Any cache override:** has an owner and a reason, so it gets revisited when hosts change. There is no universally right answer here. A smaller team may prefer a large host and a simple topology well past the point where another team would shard, and that is a defensible choice as long as someone has decided it on purpose.

  • What evidence would make you raise the WiredTiger cache rather than buy memory?
    A host with memory that stays free or nearly unused under peak load, sustained `pages read into cache`, and a workload where repeated decompression shows up as CPU cost. I would make the change on one secondary first and compare read latency and storage reads, since the cost is a smaller filesystem cache.
  • Is co-hosting safer inside containers with memory limits?
    Safer for memory, not for storage. A limit stops one instance taking the other's RAM if each cache is set below its limit, which the manual tells you to do. But both still share the device and its write capacity, so dirty eviction in one can raise latency in the other, and the host stays a shared failure domain.

  • When latency rises, raising the WiredTiger cache is always the right first step.
  • Adding RAM cures any dirty-trigger stall, whatever the storage can do.
  • Two default mongods on one host share a single 31.5 GB WiredTiger cache.
  • A cacheSizeGB increase costs nothing, because the extra memory was unused.
  • Sharding should follow as soon as the cache reaches its 80% target.